Skip to content

Commit

Permalink
feat: return shippingContact from createPlatformPayPaymentMethod an…
Browse files Browse the repository at this point in the history
…d `createPlatformPayToken` (stripe#1500)

* return shippingContact on iOS

* return shippingContact on android

* changleog

* fix phone number on android

* [skip actions] Update CHANGELOG.md
  • Loading branch information
charliecruzan-stripe authored Sep 13, 2023
1 parent 8463c92 commit 4178490
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 45 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

**Features**

- [PaymentSheet] Added the `removeSavedPaymentMethodMessage` field to `initPaymentSheet` to display a custom message when a saved payment method is removed. iOS Only. [#1498](https://github.com/stripe/stripe-react-native/pull/1498)
- `createPlatformPayPaymentMethod` and `createPlatformPayToken` now also include an optional `shippingContact` field in their results. [#1500](https://github.com/stripe/stripe-react-native/pull/1500)
- Added the `removeSavedPaymentMethodMessage` field to `initPaymentSheet` to display a custom message when a saved payment method is removed. iOS Only. [#1498](https://github.com/stripe/stripe-react-native/pull/1498)

## 0.31.1 - 2023-09-08

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class GooglePayRequestHelper {

override fun onSuccess(result: PaymentMethod) {
promiseResult.putMap("paymentMethod", mapFromPaymentMethod(result))
GooglePayResult.fromJson(paymentInformation).let {
if (it.shippingInformation != null) {
promiseResult.putMap("shippingContact", mapFromShippingContact(it))
}
}
promise.resolve(promiseResult)
}
}
Expand All @@ -146,6 +151,9 @@ class GooglePayRequestHelper {
val promiseResult = WritableNativeMap()
googlePayResult.token?.let {
promiseResult.putMap("token", mapFromToken(it))
if (googlePayResult.shippingInformation != null) {
promiseResult.putMap("shippingContact", mapFromShippingContact(googlePayResult))
}
promise.resolve(promiseResult)
} ?: run {
promise.resolve(createError("Failed", "Unexpected response from Google Pay. No token was found."))
Expand Down
19 changes: 19 additions & 0 deletions android/src/main/java/com/reactnativestripesdk/utils/Mappers.kt
Original file line number Diff line number Diff line change
Expand Up @@ -881,3 +881,22 @@ fun toBundleObject(readableMap: ReadableMap?): Bundle {
}
return result
}

internal fun mapFromShippingContact(googlePayResult: GooglePayResult): WritableMap {
val map = WritableNativeMap()
map.putString("emailAddress", googlePayResult.email)
val name = WritableNativeMap()
googlePayResult.name
name.putString("givenName", googlePayResult.shippingInformation?.name)
map.putMap("name", name)
map.putString("phoneNumber", googlePayResult.phoneNumber)
val postalAddress = WritableNativeMap()
postalAddress.putString("city", googlePayResult.shippingInformation?.address?.city)
postalAddress.putString("country", googlePayResult.shippingInformation?.address?.country)
postalAddress.putString("postalCode", googlePayResult.shippingInformation?.address?.postalCode)
postalAddress.putString("state", googlePayResult.shippingInformation?.address?.state)
postalAddress.putString("street", googlePayResult.shippingInformation?.address?.line1 + "\n" + googlePayResult.shippingInformation?.address?.line2)
postalAddress.putString("isoCountryCode", googlePayResult.shippingInformation?.address?.country)
map.putMap("postalAddress", postalAddress)
return map
}
45 changes: 25 additions & 20 deletions example/src/screens/ApplePayScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -245,35 +245,39 @@ export default function ApplePayScreen() {
};

const createPaymentMethod = async () => {
const { paymentMethod, error } = await createPlatformPayPaymentMethod({
applePay: {
cartItems: cart,
merchantCountryCode: 'US',
currencyCode: 'USD',
shippingMethods,
requiredShippingAddressFields: [
PlatformPay.ContactField.EmailAddress,
PlatformPay.ContactField.PhoneNumber,
PlatformPay.ContactField.PostalAddress,
PlatformPay.ContactField.Name,
],
requiredBillingContactFields: [PlatformPay.ContactField.PostalAddress],
supportsCouponCode: true,
couponCode: '123',
shippingType: PlatformPay.ApplePayShippingType.StorePickup,
additionalEnabledNetworks: ['JCB'],
},
});
const { paymentMethod, shippingContact, error } =
await createPlatformPayPaymentMethod({
applePay: {
cartItems: cart,
merchantCountryCode: 'US',
currencyCode: 'USD',
shippingMethods,
requiredShippingAddressFields: [
PlatformPay.ContactField.EmailAddress,
PlatformPay.ContactField.PhoneNumber,
PlatformPay.ContactField.PostalAddress,
PlatformPay.ContactField.Name,
],
requiredBillingContactFields: [
PlatformPay.ContactField.PostalAddress,
],
supportsCouponCode: true,
couponCode: '123',
shippingType: PlatformPay.ApplePayShippingType.StorePickup,
additionalEnabledNetworks: ['JCB'],
},
});
if (error) {
Alert.alert(error.code, error.localizedMessage);
} else {
Alert.alert('Success', 'Check the logs for payment method details.');
console.log(JSON.stringify(paymentMethod, null, 2));
console.log(JSON.stringify(shippingContact, null, 2));
}
};

const createToken = async () => {
const { token, error } = await createPlatformPayToken({
const { token, shippingContact, error } = await createPlatformPayToken({
applePay: {
cartItems: cart,
merchantCountryCode: 'US',
Expand All @@ -297,6 +301,7 @@ export default function ApplePayScreen() {
} else {
Alert.alert('Success', 'Check the logs for token details.');
console.log(JSON.stringify(token, null, 2));
console.log(JSON.stringify(shippingContact, null, 2));
}
};

Expand Down
39 changes: 21 additions & 18 deletions example/src/screens/GooglePayScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -124,35 +124,37 @@ export default function GooglePayScreen() {
As an alternative you can only create a paymentMethod instead of confirming the payment.
*/
const createPaymentMethod = async () => {
const { error, paymentMethod } = await createPlatformPayPaymentMethod({
googlePay: {
amount: 12,
currencyCode: 'USD',
testEnv: true,
merchantName: 'Test',
merchantCountryCode: 'US',
billingAddressConfig: {
format: PlatformPay.BillingAddressFormat.Full,
isPhoneNumberRequired: true,
isRequired: true,
},
shippingAddressConfig: {
isRequired: true,
const { error, paymentMethod, shippingContact } =
await createPlatformPayPaymentMethod({
googlePay: {
amount: 12,
currencyCode: 'USD',
testEnv: true,
merchantName: 'Test',
merchantCountryCode: 'US',
billingAddressConfig: {
format: PlatformPay.BillingAddressFormat.Full,
isPhoneNumberRequired: true,
isRequired: true,
},
shippingAddressConfig: {
isRequired: true,
},
isEmailRequired: true,
},
isEmailRequired: true,
},
});
});

if (error) {
Alert.alert('Failure', error.localizedMessage);
} else {
Alert.alert('Success', 'Check the logs for payment method details.');
console.log(JSON.stringify(paymentMethod, null, 2));
console.log(JSON.stringify(shippingContact, null, 2));
}
};

const createToken = async () => {
const { error, token } = await createPlatformPayToken({
const { error, token, shippingContact } = await createPlatformPayToken({
googlePay: {
amount: 12,
currencyCode: 'USD',
Expand All @@ -176,6 +178,7 @@ export default function GooglePayScreen() {
} else {
Alert.alert('Success', 'Check the logs for token details.');
console.log(JSON.stringify(token, null, 2));
console.log(JSON.stringify(shippingContact, null, 2));
}
};

Expand Down
14 changes: 11 additions & 3 deletions ios/ApplePayViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@ extension StripeSdk : PKPaymentAuthorizationViewControllerDelegate, STPApplePayC
if let error = error {
self.createPlatformPayPaymentMethodResolver?(Errors.createError(ErrorType.Failed, error))
} else {
let promiseResult = [
"token": token != nil ? Mappers.mapFromToken(token: token!.splitApplePayAddressByNewline()) : [:]
var promiseResult = [
"token": token != nil ? Mappers.mapFromToken(token: token!.splitApplePayAddressByNewline()) : [:],
]
if let shippingContact = payment.shippingContact {
promiseResult["shippingContact"] = Mappers.mapFromShippingContact(shippingContact: shippingContact)
}

self.createPlatformPayPaymentMethodResolver?(promiseResult)
}
completion(PKPaymentAuthorizationResult.init(status: .success, errors: nil))
Expand All @@ -33,9 +37,13 @@ extension StripeSdk : PKPaymentAuthorizationViewControllerDelegate, STPApplePayC
if let error = error {
self.createPlatformPayPaymentMethodResolver?(Errors.createError(ErrorType.Failed, error))
} else {
let promiseResult = [
var promiseResult = [
"paymentMethod": Mappers.mapFromPaymentMethod(paymentMethod?.splitApplePayAddressByNewline()) ?? [:]
]
if let shippingContact = payment.shippingContact {
promiseResult["shippingContact"] = Mappers.mapFromShippingContact(shippingContact: shippingContact)
}

self.createPlatformPayPaymentMethodResolver?(promiseResult)
}
completion(PKPaymentAuthorizationResult.init(status: .success, errors: nil))
Expand Down
6 changes: 4 additions & 2 deletions src/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -726,7 +726,7 @@ export const createPlatformPayPaymentMethod = async (
params: PlatformPay.PaymentMethodParams
): Promise<PlatformPay.PaymentMethodResult> => {
try {
const { error, paymentMethod } =
const { error, paymentMethod, shippingContact } =
(await NativeStripeSdk.createPlatformPayPaymentMethod(
params,
false
Expand All @@ -738,6 +738,7 @@ export const createPlatformPayPaymentMethod = async (
}
return {
paymentMethod: paymentMethod!,
shippingContact,
};
} catch (error: any) {
return {
Expand All @@ -755,7 +756,7 @@ export const createPlatformPayToken = async (
params: PlatformPay.PaymentMethodParams
): Promise<PlatformPay.TokenResult> => {
try {
const { error, token } =
const { error, token, shippingContact } =
(await NativeStripeSdk.createPlatformPayPaymentMethod(
params,
true
Expand All @@ -767,6 +768,7 @@ export const createPlatformPayToken = async (
}
return {
token: token!,
shippingContact,
};
} catch (error: any) {
return {
Expand Down
5 changes: 4 additions & 1 deletion src/types/PlatformPay.ts
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,6 @@ interface ContactName {
nickname?: string;
}

/** iOS only. */
export interface ShippingContact {
emailAddress?: string;
name: ContactName;
Expand All @@ -398,20 +397,24 @@ export type IsGooglePaySupportedParams = {
export type PaymentMethodResult =
| {
paymentMethod: PaymentMethod;
shippingContact?: ShippingContact;
error?: undefined;
}
| {
paymentMethod?: undefined;
shippingContact?: undefined;
error: StripeError<PlatformPayError>;
};

export type TokenResult =
| {
token: Token;
shippingContact?: ShippingContact;
error?: undefined;
}
| {
token?: undefined;
shippingContact?: undefined;
error: StripeError<PlatformPayError>;
};

Expand Down

0 comments on commit 4178490

Please sign in to comment.