Skip to content

Commit 0833a9e

Browse files
feat(payment): Added AppSwitch
1 parent 0bd65bc commit 0833a9e

File tree

1 file changed

+1
-81
lines changed

1 file changed

+1
-81
lines changed

packages/paypal-commerce-integration/src/paypal-commerce/paypal-commerce-button-strategy.ts

Lines changed: 1 addition & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
105105

106106
const defaultCallbacks = {
107107
appSwitchWhenAvailable: true, // Need an indicator to trigger App Switch
108-
createOrder: () => this.createPayPalOrder(),
108+
createOrder: () => this.paypalCommerceIntegrationService.createOrder('paypalcommerce'),
109109
onApprove: ({ orderID }: ApproveCallbackPayload) =>
110110
this.paypalCommerceIntegrationService.tokenizePayment(methodId, orderID),
111111
};
@@ -155,86 +155,6 @@ export default class PayPalCommerceButtonStrategy implements CheckoutButtonStrat
155155
}
156156
}
157157

158-
///---------------------------
159-
private async getAccessToken() {
160-
const clientId =
161-
'AVQ8E5FI-dz2P6q3ZZHrTSSl3GjmOReS_ni2lo7AuRX3LlL0DZgZlb_Q8dOjd4CBxjLYNLM8SqzaNY7u';
162-
const clientSecret =
163-
'EK2LjzZDR2up6pOYKT-UbqHvWpJnOw1zWQVTU8QAmw5hj5IAXkHBaONwXQgXCJBDz9dw3HWMv6n0224U';
164-
165-
const credentials = btoa(`${clientId}:${clientSecret}`);
166-
167-
const response = await fetch('https://api-m.sandbox.paypal.com/v1/oauth2/token', {
168-
method: 'POST',
169-
headers: {
170-
// eslint-disable-next-line prettier/prettier
171-
'Authorization': `Basic ${credentials}`,
172-
'Content-Type': 'application/x-www-form-urlencoded',
173-
},
174-
body: 'grant_type=client_credentials',
175-
});
176-
177-
const data = await response.json();
178-
179-
if (!response.ok) {
180-
throw new Error(`Error fetching token: ${JSON.stringify(data)}`);
181-
}
182-
183-
return data.access_token;
184-
}
185-
186-
private async createPayPalOrder() {
187-
try {
188-
const accessToken = await this.getAccessToken();
189-
const response = await fetch('https://api-m.sandbox.paypal.com/v2/checkout/orders', {
190-
method: 'POST',
191-
headers: {
192-
'Content-Type': 'application/json',
193-
// 'PayPal-Request-Id': 'YOUR_PAYPAL_REQUEST_ID', // Optional
194-
// eslint-disable-next-line prettier/prettier, @typescript-eslint/restrict-template-expressions
195-
'Authorization': `Bearer ${accessToken}`,
196-
},
197-
body: JSON.stringify({
198-
intent: 'CAPTURE',
199-
payment_source: {
200-
paypal: {
201-
email_address: '[email protected]',
202-
experience_context: {
203-
user_action: 'PAY_NOW',
204-
return_url: 'https://example.com/merchant_app_universal_link',
205-
cancel_url: 'https://example.com/merchant_app_universal_link',
206-
app_switch_preference: {
207-
launch_paypal_app: true,
208-
},
209-
},
210-
},
211-
},
212-
purchase_units: [
213-
{
214-
amount: {
215-
currency_code: 'USD',
216-
value: '64.00',
217-
},
218-
},
219-
],
220-
}),
221-
});
222-
223-
if (!response.ok) {
224-
const errorData = await response.json();
225-
throw new Error(`PayPal order creation failed: ${JSON.stringify(errorData)}`);
226-
}
227-
228-
const data = await response.json();
229-
console.log('Order created:', data);
230-
return data;
231-
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions
232-
} catch (error) {
233-
console.log('Error', error);
234-
}
235-
}
236-
///---------------------------
237-
238158
private async handleClick(
239159
buyNowInitializeOptions?: PayPalBuyNowInitializeOptions,
240160
): Promise<void> {

0 commit comments

Comments
 (0)