Skip to content

Commit c6cf2ff

Browse files
authored
Merge pull request #9 from MadBrains/google_parameters
[2.0.4] Add more Google Parameters
2 parents de6846b + 37624e7 commit c6cf2ff

24 files changed

+2073
-304
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
## 2.0.4
2+
3+
* Change proto structure
4+
* [Android] Add more Google Parameters
5+
16
## 2.0.3
27

38
* export `CardAuthMethods`

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ Easy integration with Google Pay and Apple Pay for your flutter app.
1717
Add this to your package's pubspec.yaml file:
1818
```yaml
1919
dependencies:
20-
mad_pay: 2.0.3
20+
mad_pay: 2.0.4
2121
```
2222
2323
For Android: set `minSdkVersion` to 21

android/src/main/kotlin/ru/madbrains/mad_pay/MadPayPlugin.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,8 +156,8 @@ class MadPayPlugin : FlutterPlugin, MethodCallHandler, PluginRegistry.ActivityRe
156156
return
157157
}
158158

159-
val paymentRequestJson = RawMethods.getPaymentMethod(totalPrice, arguments.allowedPaymentNetworksList,
160-
arguments.google, arguments.google.emailRequired, arguments.currencyCode, arguments.countryCode)
159+
val paymentRequestJson = RawMethods.getPaymentMethod(arguments.google, arguments.allowedPaymentNetworksList, totalPrice,
160+
arguments.currencyCode, arguments.countryCode, arguments.google.emailRequired)
161161

162162
val paymentDataRequest = PaymentDataRequest.fromJson(paymentRequestJson.toString(4))
163163

android/src/main/kotlin/ru/madbrains/mad_pay/PaymentHelpers.kt

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
package ru.madbrains.mad_pay
22

3-
import google.Google.CardAuthMethods
43
import ru.madbrains.mad_pay.MadPay.PaymentNetwork
4+
import ru.madbrains.mad_pay.Google.*
55

66
class PaymentHelpers {
77
companion object {
@@ -30,6 +30,23 @@ class PaymentHelpers {
3030
}
3131
}
3232

33+
fun decodeTotalPriceStatus(totalPriceStatus: TotalPriceStatus): String? {
34+
return when (totalPriceStatus) {
35+
TotalPriceStatus.NOT_CURRENTLY_KNOWN -> "NOT_CURRENTLY_KNOWN"
36+
TotalPriceStatus.ESTIMATED -> "ESTIMATED"
37+
TotalPriceStatus.FINAL -> "FINAL"
38+
else -> "FINAL"
39+
}
40+
}
41+
42+
fun decodeCheckoutOption(checkoutOption: CheckoutOption): String? {
43+
return when (checkoutOption) {
44+
CheckoutOption.DEFAULT -> "DEFAULT"
45+
CheckoutOption.COMPLETE_IMMEDIATE_PURCHASE -> "COMPLETE_IMMEDIATE_PURCHASE"
46+
else -> "DEFAULT"
47+
}
48+
}
49+
3350
fun getPaymentNetwork(paymentNetworks: List<PaymentNetwork>): List<String> {
3451
return when {
3552
!paymentNetworks.isNullOrEmpty() ->

android/src/main/kotlin/ru/madbrains/mad_pay/RawMethods.kt

Lines changed: 95 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -5,37 +5,84 @@ import org.json.JSONObject
55

66
class 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
}

example/lib/main.dart

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,23 @@ class _MyAppState extends State<MyApp> {
7272
google: GoogleParameters(
7373
gatewayName: 'example',
7474
gatewayMerchantId: 'example_id',
75+
merchantName: 'Test',
76+
cardParameters: CardParameters(
77+
billingAddressRequired: true,
78+
billingAddressParameters: BillingAddressParameters(
79+
billingFormat: BillingFormat.full,
80+
phoneNumberRequired: true,
81+
),
82+
),
83+
transactionInfo: TransactionInfo(
84+
totalPriceLabel: 'Test',
85+
checkoutOption:
86+
CheckoutOption.completeImmediatePurchase,
87+
),
88+
shippingAddressRequired: true,
89+
shippingAddressParameters: ShippingAddressParameters(
90+
phoneNumberRequired: true,
91+
),
7592
),
7693
apple: AppleParameters(
7794
merchantIdentifier: 'example_id',

example/pubspec.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ packages:
7373
path: ".."
7474
relative: true
7575
source: path
76-
version: "2.0.3"
76+
version: "2.0.4"
7777
matcher:
7878
dependency: transitive
7979
description:

ios/Classes/protos/apple.pb.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,7 @@ extension Apple_PersonNameComponents: SwiftProtobuf.Message, SwiftProtobuf._Mess
533533
4: .standard(proto: "family_name"),
534534
5: .standard(proto: "name_suffix"),
535535
6: .same(proto: "nickname"),
536-
7: .same(proto: "phoneticRepresentation"),
536+
7: .standard(proto: "phonetic_representation"),
537537
]
538538

539539
fileprivate class _StorageClass {

0 commit comments

Comments
 (0)