Skip to content

Commit c3ffba1

Browse files
authored
🤖 Merge PR DefinitelyTyped#72221 [Braintree] Updating the Apple pay JS instance. by @Henrique0305
1 parent 18649b0 commit c3ffba1

File tree

2 files changed

+142
-1
lines changed

2 files changed

+142
-1
lines changed

‎types/braintree-web/apple-pay.d.ts‎

Lines changed: 103 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,103 @@ export enum ApplePayStatusCodes {
9595
STATUS_PIN_LOCKOUT,
9696
}
9797

98+
export type ApplePayErrorCode =
99+
/**
100+
* Shipping address or contact information is invalid or missing.
101+
*/
102+
| "shippingContactInvalid"
103+
/**
104+
* Billing address information is invalid or missing.
105+
*/
106+
| "billingContactInvalid"
107+
/**
108+
* The merchant cannot provide service to the shipping address (for example, can't deliver to a P.O. Box).
109+
*/
110+
| "addressUnserviceable"
111+
/**
112+
* The code that indicates an invalid coupon.
113+
*/
114+
| "couponCodeInvalid"
115+
/**
116+
* The code that indicates an expired coupon.
117+
*/
118+
| "couponCodeExpired"
119+
/**
120+
* An unknown but nonfatal error occurred during payment processing. The user can attempt authorization again.
121+
*/
122+
| "unknown";
123+
124+
export interface ApplePayError {
125+
/**
126+
* The error code for this instance.
127+
*/
128+
code: ApplePayErrorCode;
129+
130+
/**
131+
* The name of the field that contains the error.
132+
*/
133+
contactField?: ApplePayErrorContactField | undefined;
134+
135+
/**
136+
* A localized, user-facing string that describes the error.
137+
*/
138+
message: string;
139+
}
140+
141+
/**
142+
* Names of the fields in the shipping or billing contact information, used to locate errors in the payment sheet.
143+
*/
144+
export type ApplePayErrorContactField =
145+
| "phoneNumber"
146+
| "emailAddress"
147+
| "name"
148+
| "phoneticName"
149+
| "postalAddress"
150+
| "addressLines"
151+
| "locality"
152+
| "subLocality"
153+
| "postalCode"
154+
| "administrativeArea"
155+
| "subAdministrativeArea"
156+
| "country"
157+
| "countryCode";
158+
159+
export interface ApplePayPaymentOrderDetails {
160+
/**
161+
* An identifier for the order type associated with the order.
162+
*/
163+
orderTypeIdentifier: string;
164+
/**
165+
* A unique order identifier scoped to your order type identifier.
166+
*/
167+
orderIdentifier: string;
168+
/**
169+
* The URL of your web service.
170+
*/
171+
webServiceURL: string;
172+
/**
173+
* The authentication token supplied to your web service.
174+
*/
175+
authenticationToken: string;
176+
}
177+
178+
export interface ApplePayPaymentAuthorizationResult {
179+
/**
180+
* The status code for the authorization result.
181+
*/
182+
status: number;
183+
184+
/**
185+
* A list of custom errors to display on the payment sheet.
186+
*/
187+
errors?: ApplePayError[] | undefined;
188+
189+
/**
190+
* Optional metadata for an order that the customer placed using this payment method.
191+
*/
192+
orderDetails?: ApplePayPaymentOrderDetails;
193+
}
194+
98195
export type ApplePayTokenizeValues = "Yes" | "No" | "Unknown";
99196

100197
export interface ApplePayDetails {
@@ -137,7 +234,12 @@ export class ApplePaySession {
137234

138235
begin(): void;
139236

140-
completePayment(status: ApplePayStatusCodes): void;
237+
/**
238+
* Completes the payment authorization with a result.
239+
* @param result - The status of the payment, whether it succeeded or failed for Apple Pay JS versions 1 and 2,
240+
* or the result of the payment authorization, including its status and list of errors for Apple Pay JS version 3.
241+
*/
242+
completePayment(status: ApplePayStatusCodes | ApplePayPaymentAuthorizationResult): void;
141243

142244
completePaymentMethodSelection(newTotal: any, newLineItems: any): void;
143245

‎types/braintree-web/test/web.ts‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import * as braintree from "braintree-web";
2+
import { ApplePayError } from "braintree-web/apple-pay";
23
import { HostedFieldsBinPayload } from "braintree-web/hosted-fields";
34

45
const version: string = braintree.VERSION;
@@ -507,6 +508,44 @@ braintree.client.create(
507508
};
508509
});
509510

511+
braintree.applePay.create({ client: clientInstance }, (createErr, applePayInstance) => {
512+
const request = {
513+
countryCode: "US",
514+
currencyCode: "USD",
515+
supportedNetworks: ["visa", "masterCard"],
516+
merchantCapabilities: ["supports3DS"],
517+
total: { label: "Your Label", amount: "10.00" },
518+
};
519+
520+
// Creates apple pay with version 3
521+
const session = new braintree.ApplePaySession(3, request);
522+
523+
session.onpaymentauthorized = event => {
524+
applePayInstance.tokenize(
525+
{
526+
token: event.payment.token,
527+
},
528+
(err: braintree.BraintreeError, tokenizedPayload: braintree.ApplePayPayload) => {
529+
if (err) {
530+
const errorVerion3: ApplePayError = {
531+
code: "unknown",
532+
message: "An error occurred",
533+
};
534+
535+
session.completePayment({
536+
status: braintree.ApplePaySession.STATUS_FAILURE,
537+
errors: [errorVerion3],
538+
});
539+
return;
540+
}
541+
console.log(tokenizedPayload.nonce);
542+
543+
session.completePayment(braintree.ApplePaySession.STATUS_SUCCESS);
544+
},
545+
);
546+
};
547+
});
548+
510549
braintree.applePay.create({ client: clientInstance }).then(applePayInstance => {
511550
const request = {
512551
countryCode: "US",

0 commit comments

Comments
 (0)