-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpaypal.js
65 lines (62 loc) · 1.97 KB
/
paypal.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import { loadScript } from "@paypal/paypal-js";
async function initPayPalButtons(clientID = 'test', containerID, cartTotal = '0.01') {
let paypal;
try {
paypal = await loadScript({
"clientId": clientID,
"disableFunding": 'sepa,giropay,sofort,card',
});
} catch (error) {
console.error("failed to load the PayPal JS SDK script", error);
}
if (paypal) {
try {
await paypal.Buttons({
style: {
color: "silver",
height: 55,
tagline: false,
},
createOrder: function (data, actions) {
// Set up the transaction
// @info This is just for testing.
// Refer to your server-side integration / your PSP endpoint here
// and return the order id
return actions.order.create({
purchase_units: [
{
amount: {
value: cartTotal,
},
},
],
});
},
onApprove: function (data, actions) {
// Capture order after payment approved
// @info This is just for testing.
// Refer to your server-side integration / your PSP endpoint here
return actions.order.capture().then(function (details) {
alert("Payment successful!");
});
},
onError: function (err) {
// Log error if something goes wrong during approval
// Handle error messaging for the buyer
alert("Something went wrong");
console.log("Something went wrong", err);
},
onCancel: function () {
// Log error if something goes wrong during approval
// Handle info messaging for the buyer
alert("Payment was cancelled");
console.log("Payment was cancelled");
},
}
).render(containerID);
} catch (error) {
console.error("failed to render the PayPal Buttons", error);
}
}
}
export { initPayPalButtons };