Skip to content

Commit 1785401

Browse files
casigoldipull[bot]
authored andcommitted
🤖 Merge PR #57876 [braintree-web] Add localPayment types by @casigoldi
* [braintree-web] Add localPayment types * [braintree-web] Prettier format
1 parent 39ffaa0 commit 1785401

4 files changed

Lines changed: 270 additions & 0 deletions

File tree

types/braintree-web/index.d.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import {
1818
HostedFieldsStateObject,
1919
HostedFieldsBinPayload,
2020
} from './modules/hosted-fields';
21+
import { LocalPayment, LocalPaymentTokenizePayload, LocalPaymentTypes } from './modules/local-payment';
2122
import { PayPal, PayPalTokenizePayload } from './modules/paypal';
2223
import { PayPalCheckout, PayPalCheckoutCreatePaymentOptions } from './modules/paypal-checkout';
2324
import { ThreeDSecure, ThreeDSecureVerifyPayload } from './modules/three-d-secure';
@@ -37,6 +38,7 @@ export const client: Client;
3738
export const dataCollector: DataCollector;
3839
export const googlePayment: GooglePayment;
3940
export const hostedFields: HostedFields;
41+
export const localPayment: LocalPayment;
4042
export const paypal: PayPal;
4143
export const paypalCheckout: PayPalCheckout;
4244
export const threeDSecure: ThreeDSecure;
@@ -65,6 +67,9 @@ export {
6567
HostedFieldsTokenizePayload,
6668
HostedFieldsEvent,
6769
HostedFieldsStateObject,
70+
LocalPayment,
71+
LocalPaymentTypes,
72+
LocalPaymentTokenizePayload,
6873
PayPal,
6974
PayPalTokenizePayload,
7075
PayPalCheckout,
Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { callback } from './core';
2+
import { Client } from './client';
3+
4+
export type LocalPaymentTypes =
5+
| 'bancontact'
6+
| 'blik'
7+
| 'eps'
8+
| 'giropay'
9+
| 'ideal'
10+
| 'sofort'
11+
| 'mybank'
12+
| 'p24'
13+
| 'trustly';
14+
15+
export interface LocalPaymentTokenizePayload {
16+
correlationId: string;
17+
nonce: string;
18+
details: unknown;
19+
type: string;
20+
}
21+
22+
export interface LocalPaymentAddress {
23+
streetAddress?: string | undefined;
24+
extendedAddress?: string | undefined;
25+
locality?: string | undefined;
26+
region?: string | undefined;
27+
postalCode?: string | undefined;
28+
countryCode?: string | undefined;
29+
}
30+
31+
export interface LocalPaymentFallback {
32+
buttonText?: string | undefined;
33+
url?: string | undefined;
34+
cancelButtonText?: string | undefined;
35+
cancelUrl?: string | undefined;
36+
}
37+
38+
export interface LocalPaymentStartData {
39+
paymentId: string;
40+
}
41+
42+
export interface LocalPaymentStartPaymentOptions {
43+
amount: string | number;
44+
currencyCode: string;
45+
paymentType: LocalPaymentTypes;
46+
fallback?: LocalPaymentFallback | undefined;
47+
windowOptions?:
48+
| {
49+
width?: number | undefined;
50+
height?: number | undefined;
51+
}
52+
| undefined;
53+
givenName?: string | undefined;
54+
surname?: string | undefined;
55+
displayName?: string | undefined;
56+
countryCode?: string | undefined;
57+
paymentTypeCountryCode?: string | undefined;
58+
email?: string | undefined;
59+
phone?: string | undefined;
60+
bic?: string | undefined;
61+
shippingAddressRequired?: boolean | undefined;
62+
address?: LocalPaymentAddress | undefined;
63+
onPaymentStart?: ((data: LocalPaymentStartData, callback: callback) => void) | undefined;
64+
}
65+
66+
export interface LocalPaymentTokenizeOptions {
67+
btLpToken?: string | undefined;
68+
btLpPaymentId?: string | undefined;
69+
btLpPayerId?: string | undefined;
70+
}
71+
72+
export interface LocalPaymentCreateOptions {
73+
authorization?: string | undefined;
74+
client?: Client | undefined;
75+
merchantAccountId?: string | undefined;
76+
}
77+
78+
export interface LocalPayment {
79+
/**
80+
* braintree.localPayment.create({
81+
* client: client
82+
* }, callback)
83+
*/
84+
create(options: LocalPaymentCreateOptions): Promise<LocalPayment>;
85+
create(options: LocalPaymentCreateOptions, callback: callback<LocalPayment>): void;
86+
87+
/**
88+
* The current version of the SDK, i.e. 3.84.0.
89+
*/
90+
VERSION: string;
91+
92+
/**
93+
* Closes the LocalPayment window if it is open.
94+
* @example
95+
* localPaymentInstance.closeWindow();
96+
*/
97+
closeWindow(): void;
98+
99+
/**
100+
* Focuses the LocalPayment window if it is open.
101+
* @example
102+
* localPaymentInstance.focusWindow();
103+
*/
104+
focusWindow(): void;
105+
106+
/**
107+
* Checks if required tokenization parameters are available in querystring for manual tokenization requests.
108+
* @example
109+
* // if query string contains
110+
* // ?btLpToken=token&btLpPaymentId=payment-id&btLpPayerId=payer-id
111+
* localPaymentInstance.hasTokenizationParams(); // true
112+
* // if query string is missing required params
113+
* localPaymentInstance.hasTokenizationParams(); // false
114+
* if (localPaymentInstance.hasTokenizationParams()) {
115+
* localPaymentInstance.tokenize();
116+
* }
117+
*/
118+
hasTokenizationParams(): boolean;
119+
120+
/**
121+
* Launches the local payment flow and returns a nonce payload. Only one local payment flow should be active at a time.
122+
* One way to achieve this is to disable your local payment button while the flow is open.
123+
* @example
124+
* button.addEventListener('click', function () {
125+
* // Disable the button when local payment is in progress
126+
* button.setAttribute('disabled', 'disabled');
127+
* // Because startPayment opens a new window, this must be called
128+
* // as a result of a user action, such as a button click.
129+
* localPaymentInstance.startPayment({
130+
* paymentType: 'ideal',
131+
* paymentTypeCountryCode: 'NL',
132+
* fallback: {
133+
* buttonText: 'Return to Merchant',
134+
* url: 'https://example.com/my-checkout-page'
135+
* },
136+
* amount: '10.00',
137+
* currencyCode: 'EUR',
138+
* onPaymentStart: function (data, continueCallback) {
139+
* // Do any preprocessing before starting the flow
140+
* // data.paymentId is the ID of the localPayment
141+
* continueCallback();
142+
* }
143+
* }).then(function (payload) {
144+
* button.removeAttribute('disabled');
145+
* // Submit payload.nonce to your server
146+
* }).catch(function (startPaymentError) {
147+
* button.removeAttribute('disabled');
148+
* // Handle flow errors or premature flow closure
149+
* console.error('Error!', startPaymentError);
150+
* });
151+
* });
152+
*/
153+
startPayment(options: LocalPaymentStartPaymentOptions): Promise<LocalPaymentTokenizePayload>;
154+
startPayment(options: LocalPaymentStartPaymentOptions, callback?: callback<LocalPaymentTokenizePayload>): void;
155+
156+
/**
157+
* Cleanly remove anything set up by {@link module:braintree-web/local-payment.create|create}.
158+
* @example
159+
* localPaymentInstance.teardown();
160+
* @example <caption>With callback</caption>
161+
* localPaymentInstance.teardown(function () {
162+
* // teardown is complete
163+
* });
164+
*/
165+
teardown(callback?: callback): void;
166+
teardown(): Promise<void>;
167+
168+
/**
169+
* Manually tokenizes params for a local payment received from PayPal.
170+
* When app switching back from a mobile application (such as a bank application for an iDEAL payment) the window may lose context with the parent page.
171+
* In that case, a fallback url is used, and this method can be used to finish the flow.
172+
* @example
173+
* localPaymentInstance.tokenize().then(function (payload) {
174+
* // send payload.nonce to your server
175+
* }).catch(function (err) {
176+
* // handle tokenization error
177+
* });
178+
*/
179+
tokenize(params?: LocalPaymentTokenizeOptions): Promise<LocalPaymentTokenizePayload>;
180+
tokenize(params?: LocalPaymentTokenizeOptions, callback?: callback<LocalPaymentTokenizePayload>): void;
181+
}

types/braintree-web/test/node.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,48 @@ braintree.client.create(
530530
console.error('Error!', error);
531531
});
532532
});
533+
534+
// Local Payment
535+
braintree.localPayment.create({
536+
client: clientInstance
537+
}, (err, localPaymentInstance) => {
538+
localPaymentInstance
539+
.startPayment({
540+
amount: 11.00,
541+
currencyCode: 'EUR',
542+
paymentType: 'sofort',
543+
onPaymentStart: (data, next) => {
544+
if (data.paymentId) {
545+
// Implementation
546+
}
547+
next();
548+
}
549+
})
550+
.then((payload: braintree.LocalPaymentTokenizePayload) => {
551+
console.log(payload.nonce);
552+
})
553+
.catch((error: braintree.BraintreeError) => {
554+
console.error('Error!', error);
555+
});
556+
557+
localPaymentInstance.tokenize({
558+
btLpPayerId: '1234',
559+
btLpPaymentId: '1234',
560+
btLpToken: '1234'
561+
}, (error, data) => {
562+
if (error) {
563+
console.error('Tokenize Error!', error);
564+
return;
565+
}
566+
567+
// Implementation
568+
console.log(data.nonce);
569+
});
570+
571+
localPaymentInstance.teardown(err => {
572+
// Implementation
573+
});
574+
});
533575
},
534576
);
535577

types/braintree-web/test/web.ts

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -738,6 +738,48 @@ braintree.client.create(
738738
clientInstance.teardown(err => {
739739
// implementation
740740
});
741+
742+
// Local Payment
743+
braintree.localPayment.create({
744+
client: clientInstance
745+
}, (err, localPaymentInstance) => {
746+
localPaymentInstance
747+
.startPayment({
748+
amount: 11.00,
749+
currencyCode: 'EUR',
750+
paymentType: 'sofort',
751+
onPaymentStart: (data, next) => {
752+
if (data.paymentId) {
753+
// Implementation
754+
}
755+
next();
756+
}
757+
})
758+
.then((payload: braintree.LocalPaymentTokenizePayload) => {
759+
console.log(payload.nonce);
760+
})
761+
.catch((error: braintree.BraintreeError) => {
762+
console.error('Error!', error);
763+
});
764+
765+
localPaymentInstance.tokenize({
766+
btLpPayerId: '1234',
767+
btLpPaymentId: '1234',
768+
btLpToken: '1234'
769+
}, (error, data) => {
770+
if (error) {
771+
console.error('Tokenize Error!', error);
772+
return;
773+
}
774+
775+
// Implementation
776+
console.log(data.nonce);
777+
});
778+
779+
localPaymentInstance.teardown(err => {
780+
// Implementation
781+
});
782+
});
741783
},
742784
);
743785

0 commit comments

Comments
 (0)