Skip to content

Commit

Permalink
Merge pull request #324 from rgarciadelongoria/feature/allowed-countr…
Browse files Browse the repository at this point in the history
…ies-for-shipping-address

feat: add logic to allow countries and block others
  • Loading branch information
rdlabo authored Jan 30, 2024
2 parents da5dff3 + f487ea8 commit 2fbf0c1
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
17 changes: 17 additions & 0 deletions packages/payment/ios/Plugin/ApplePay/ApplePayExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ class ApplePayExecutor: NSObject, ApplePayContextDelegate {
var appleClientSecret: String = ""
private var payCallId: String?
private var paymentRequest: PKPaymentRequest?
private var allowedCountries: [String] = []
private var allowedCountriesErrorDescription: String = ""

func isApplePayAvailable(_ call: CAPPluginCall) {
if !StripeAPI.deviceSupportsApplePay() {
Expand Down Expand Up @@ -47,6 +49,9 @@ class ApplePayExecutor: NSObject, ApplePayContextDelegate {

let merchantIdentifier = call.getString("merchantIdentifier") ?? ""
let requiredShippingContactFields = call.getArray("requiredShippingContactFields", String.self) ?? [""]
self.allowedCountries = call.getArray("allowedCountries", String.self) ?? []
self.allowedCountriesErrorDescription = call.getString("allowedCountriesErrorDescription") ?? "Country not allowed"

let paymentRequest = StripeAPI.paymentRequest(withMerchantIdentifier: merchantIdentifier, country: call.getString("countryCode", "US"), currency: call.getString("currency", "USD"))
paymentRequest.paymentSummaryItems = paymentSummaryItems
if requiredShippingContactFields.count > 0 {
Expand Down Expand Up @@ -134,6 +139,18 @@ extension ApplePayExecutor {
handler(PKPaymentRequestShippingContactUpdate.init(paymentSummaryItems: []))
let jsonArray = self.transformPKContactToJSON(contact: contact)
self.plugin?.notifyListeners(ApplePayEvents.DidSelectShippingContact.rawValue, data: ["contact": jsonArray])

// Check allowed countries
if (!self.allowedCountries.isEmpty) {
let addressIsoCountry = (contact.postalAddress?.isoCountryCode as? String ?? "").lowercased();
if !self.allowedCountries.contains(addressIsoCountry) {
handler(PKPaymentRequestShippingContactUpdate.init(
errors: [PKPaymentRequest.paymentShippingAddressInvalidError(withKey: CNPostalAddressISOCountryCodeKey, localizedDescription: self.allowedCountriesErrorDescription)],
paymentSummaryItems: self.paymentRequest?.paymentSummaryItems ?? [],
shippingMethods: self.paymentRequest?.shippingMethods ?? []))
return
}
}
}

func applePayContext(_ context: STPApplePayContext, didCreatePaymentMethod paymentMethod: StripeAPI.PaymentMethod, paymentInformation: PKPayment, completion: @escaping STPIntentClientSecretCompletionBlock) {
Expand Down
2 changes: 2 additions & 0 deletions packages/payment/src/shared/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@ export interface CreateApplePayOption {
countryCode: string;
currency: string;
requiredShippingContactFields?: ('postalAddress' | 'phoneNumber' | 'emailAddress' | 'name')[];
allowedCountries?: string[];
allowedCountriesErrorDescription?: string;
}

export interface CreateGooglePayOption {
Expand Down

0 comments on commit 2fbf0c1

Please sign in to comment.