@@ -5,37 +5,84 @@ import org.json.JSONObject
55
66class RawMethods {
77 companion object {
8+
89 private fun getBaseRequest (): JSONObject {
9- return JSONObject ()
10- .put(" apiVersion" , 2 )
11- .put(" apiVersionMinor" , 0 )
10+ return JSONObject ().apply {
11+ put(" apiVersion" , 2 )
12+ put(" apiVersionMinor" , 0 )
13+ }
1214 }
1315
14- private fun getGatewayJsonTokenizationType (gatewayName : String , gatewayMerchantID : String ): JSONObject {
15- return JSONObject ().put(" type" , " PAYMENT_GATEWAY" )
16- .put(" parameters" , JSONObject ()
17- .put(" gateway" , gatewayName)
18- .put(" gatewayMerchantId" , gatewayMerchantID))
16+ private fun getAllowedPaymentMethods (google : Google .GoogleParameters , allowedPaymentNetworks : List <MadPay .PaymentNetwork >? ): JSONObject {
17+ return JSONObject ().apply {
18+ put(" type" , " CARD" )
19+ put(" parameters" , getPaymentParameters(google, allowedPaymentNetworks))
20+ put(" tokenizationSpecification" , getTokenizationSpecification(google))
21+ }
1922 }
2023
21- private fun getTransactionInfo (totalPrice : Double , currencyCode : String , countryCode : String ): JSONObject {
22- return JSONObject ()
23- .put(" totalPrice" , totalPrice.toString())
24- .put(" totalPriceStatus" , " FINAL" )
25- .put(" countryCode" , countryCode)
26- .put(" currencyCode" , currencyCode)
24+ private fun getPaymentParameters (google : Google .GoogleParameters , allowedPaymentNetworks : List <MadPay .PaymentNetwork >? ): JSONObject {
25+ val allowedAuthMethods: JSONArray = when (google.cardParameters.allowedCardsMethodsList) {
26+ null -> JSONArray (PaymentHelpers .availableAllowedAuthMethods)
27+ else -> JSONArray (PaymentHelpers .getAuthMethods(google.cardParameters.allowedCardsMethodsList))
28+ }
29+
30+ val allowedCardNetworks: JSONArray = when (allowedPaymentNetworks) {
31+ null -> JSONArray (PaymentHelpers .availableAllowedPaymentNetworks)
32+ else -> JSONArray (PaymentHelpers .getPaymentNetwork(allowedPaymentNetworks))
33+ }
34+
35+ return JSONObject ().apply {
36+ put(" allowedAuthMethods" , allowedAuthMethods)
37+ put(" allowedCardNetworks" , allowedCardNetworks)
38+ if (google.hasCardParameters()) {
39+ put(" allowPrepaidCards" , google.cardParameters.allowPrepaidCards)
40+ put(" allowCreditCards" , google.cardParameters.allowCreditCards)
41+ put(" assuranceDetailsRequired" , google.cardParameters.assuranceDetailsRequired)
42+ put(" billingAddressRequired" , google.cardParameters.billingAddressRequired)
43+ if (google.cardParameters.hasBillingAddressParameters()) {
44+ put(" billingAddressParameters" , JSONObject ().apply {
45+ put(" format" , google.cardParameters.billingAddressParameters.billingFormat)
46+ put(" phoneNumberRequired" , google.cardParameters.billingAddressParameters.phoneNumberRequired)
47+ })
48+ }
49+ }
50+ }
2751 }
2852
29- private fun getCardPaymentMethod (gatewayName : String , gatewayMerchantID : String , allowedPaymentNetworks : List <String >? = null, allowedAuthMethods : List <String >? = null): JSONObject {
30- val cardPaymentMethod = getBaseCardPaymentMethod(allowedPaymentNetworks, allowedAuthMethods)
31- val tokenizationOptions = getGatewayJsonTokenizationType(gatewayName, gatewayMerchantID)
32- cardPaymentMethod.put(" tokenizationSpecification" , tokenizationOptions)
33- return cardPaymentMethod
53+ private fun getTokenizationSpecification (google : Google .GoogleParameters ): JSONObject {
54+ return JSONObject ().apply {
55+ put(" type" , " PAYMENT_GATEWAY" )
56+ put(" parameters" , JSONObject ().apply {
57+ put(" gateway" , google.gatewayName)
58+ put(" gatewayMerchantId" , google.gatewayMerchantId)
59+ })
60+ }
3461 }
3562
36- private fun getBaseCardPaymentMethod (allowedPaymentNetworks : List <String >? = null, allowedAuthMethods : List <String >? = null): JSONObject {
37- val cardPaymentMethod = JSONObject ().put(" type" , " CARD" )
63+ private fun getTransactionInfo (google : Google .GoogleParameters , totalPrice : Double , currencyCode : String , countryCode : String ): JSONObject {
64+ return JSONObject ().apply {
65+ put(" currencyCode" , currencyCode)
66+ put(" countryCode" , countryCode)
67+ put(" totalPrice" , totalPrice.toString())
68+ put(" totalPriceStatus" , PaymentHelpers .decodeTotalPriceStatus(google.transactionInfo.totalPriceStatus))
69+ if (google.hasTransactionInfo()) {
70+ put(" transactionId" , google.transactionInfo.transactionId)
71+ put(" totalPriceLabel" , google.transactionInfo.totalPriceLabel)
72+ put(" checkoutOption" , PaymentHelpers .decodeCheckoutOption(google.transactionInfo.checkoutOption))
73+ }
74+ }
75+ }
3876
77+ private fun getShippingAddressParameters (shippingAddressParameters : Google .ShippingAddressParameters ): JSONObject {
78+ return JSONObject ().apply {
79+ put(" allowedCountryCodes" , JSONArray (shippingAddressParameters.allowedCountryCodesList))
80+ put(" phoneNumberRequired" , shippingAddressParameters.phoneNumberRequired)
81+ }
82+ }
83+
84+
85+ private fun getCheckPaymentMethod (allowedPaymentNetworks : List <String >? = null, allowedAuthMethods : List <String >? = null): JSONObject {
3986 val cardNetworks: JSONArray = when (allowedPaymentNetworks) {
4087 null -> JSONArray (PaymentHelpers .availableAllowedPaymentNetworks)
4188 else -> JSONArray (allowedPaymentNetworks)
@@ -46,49 +93,49 @@ class RawMethods {
4693 else -> JSONArray (allowedAuthMethods)
4794 }
4895
49- val params = JSONObject ()
50- .put(" allowedAuthMethods" , authMethods)
51- .put(" allowedCardNetworks" , cardNetworks)
52-
53- cardPaymentMethod.put(" parameters" , params)
54- return cardPaymentMethod
96+ return JSONObject ().apply {
97+ put(" type" , " CARD" )
98+ put(" parameters" , JSONObject ().apply {
99+ put(" allowedAuthMethods" , authMethods)
100+ put(" allowedCardNetworks" , cardNetworks)
101+ })
102+ }
55103 }
56104
57105 fun getCheckMethod (): JSONObject {
58- val baseRequest = getBaseRequest()
59- baseRequest. put(" allowedPaymentMethods" , JSONArray ().put(getBaseCardPaymentMethod ()))
60- return baseRequest
106+ return getBaseRequest(). apply {
107+ put(" allowedPaymentMethods" , JSONArray ().put(getCheckPaymentMethod ()))
108+ }
61109 }
62110
63111 fun getCheckActiveCardMethod (allowedPaymentNetworks : List <MadPay .PaymentNetwork >): JSONObject {
64112 val paymentNetworks = PaymentHelpers .getPaymentNetwork(allowedPaymentNetworks)
65113
66- val baseRequest = getBaseRequest()
67- baseRequest.put(" allowedPaymentMethods" , JSONArray ().put(getBaseCardPaymentMethod(paymentNetworks)))
68- baseRequest.put(" existingPaymentMethodRequired" , false )
69-
70- return baseRequest
114+ return getBaseRequest().apply {
115+ put(" allowedPaymentMethods" , JSONArray ().put(getCheckPaymentMethod(paymentNetworks)))
116+ put(" existingPaymentMethodRequired" , false )
117+ }
71118 }
72119
73- fun getPaymentMethod (totalPrice : Double , allowedPaymentNetworks : List <MadPay .PaymentNetwork >,
74- google : google.Google .GoogleParameters ,
75- emailRequired : Boolean , currencyCode : String , countryCode : String ): JSONObject {
76-
77- val paymentNetworks = PaymentHelpers .getPaymentNetwork(allowedPaymentNetworks)
78- val authMethods = PaymentHelpers .getAuthMethods(google.allowedCardsMethodsList)
79-
120+ fun getPaymentMethod (google : Google .GoogleParameters , allowedPaymentNetworks : List <MadPay .PaymentNetwork >, totalPrice : Double ,
121+ currencyCode : String , countryCode : String , emailRequired : Boolean ): JSONObject {
80122 val merchantInfo = when {
81123 google.merchantName.isNotEmpty() -> JSONObject ()
82124 .putOpt(" merchantName" , google.merchantName)
83125 else -> null
84126 }
85127
86- return getBaseRequest()
87- .putOpt(" merchantInfo" , merchantInfo)
88- .put(" emailRequired" , emailRequired)
89- .put(" transactionInfo" , getTransactionInfo(totalPrice, currencyCode, countryCode))
90- .put(" allowedPaymentMethods" , JSONArray ().put(getCardPaymentMethod(google.gatewayName,
91- google.gatewayMerchantId, paymentNetworks, authMethods)))
128+ return getBaseRequest().apply {
129+ putOpt(" merchantInfo" , merchantInfo)
130+ put(" allowedPaymentMethods" , JSONArray ().put(getAllowedPaymentMethods(google, allowedPaymentNetworks)))
131+ put(" transactionInfo" , getTransactionInfo(google, totalPrice, currencyCode, countryCode))
132+ put(" emailRequired" , emailRequired)
133+ put(" shippingAddressRequired" , google.shippingAddressRequired)
134+ if (google.hasShippingAddressParameters()) {
135+ put(" shippingAddressParameters" , getShippingAddressParameters(google.shippingAddressParameters))
136+ }
137+ }
92138 }
139+
93140 }
94141}
0 commit comments