@storecraft/payments-paypal 1.2.5 → 1.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/adapter.html.js +155 -88
- package/adapter.js +1 -2
- package/package.json +1 -1
package/adapter.html.js
CHANGED
@@ -25,112 +25,179 @@ export default function html_buy_ui(config, order_data) {
|
|
25
25
|
<html style="height: 100%; width: 100%">
|
26
26
|
<head>
|
27
27
|
<meta charset="UTF-8" />
|
28
|
+
<meta name="color-scheme" content="light">
|
28
29
|
<meta name="viewport" content="width=device-width; height=device-height, initial-scale=1.0" />
|
29
30
|
<title>PayPal JS SDK Standard Integration</title>
|
31
|
+
<style>
|
32
|
+
/* Variables */
|
33
|
+
* {
|
34
|
+
box-sizing: border-box;
|
35
|
+
}
|
36
|
+
|
37
|
+
body {
|
38
|
+
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
|
39
|
+
font-size: 10px;
|
40
|
+
width: 100vw;
|
41
|
+
max-width: 500px;
|
42
|
+
margin: auto;
|
43
|
+
padding: 10px;
|
44
|
+
-webkit-font-smoothing: antialiased;
|
45
|
+
background-color: white;
|
46
|
+
}
|
47
|
+
</style>
|
30
48
|
</head>
|
31
|
-
<body
|
32
|
-
|
33
|
-
|
49
|
+
<body>
|
50
|
+
<div id="paypal-button-container" style='width: 100%' ></div>
|
51
|
+
<p id="result-message"></p>
|
52
|
+
|
34
53
|
<!-- Initialize the JS-SDK -->
|
35
54
|
<script src="https://www.paypal.com/sdk/js?currency=${config.default_currency_code}&client-id=${config.client_id}&intent=${config.intent_on_checkout.toLowerCase()}"></script>
|
36
55
|
|
37
56
|
<!-- code -->
|
38
57
|
<script>
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
}
|
44
|
-
|
45
|
-
window.paypal
|
46
|
-
.Buttons(
|
47
|
-
{
|
48
|
-
style: {
|
49
|
-
shape: "rect",
|
50
|
-
layout: "vertical",
|
51
|
-
color: "gold",
|
52
|
-
label: "paypal",
|
53
|
-
disableMaxWidth: true
|
54
|
-
},
|
58
|
+
const resultMessage = (msg) => {
|
59
|
+
document.getElementById('result-message').innerHTML = msg
|
60
|
+
console.log(msg);
|
61
|
+
}
|
55
62
|
|
56
|
-
|
57
|
-
|
63
|
+
const dispatchEvent = (event, data) => {
|
64
|
+
window?.parent?.postMessage(
|
65
|
+
{
|
66
|
+
who: "storecraft",
|
67
|
+
event,
|
68
|
+
data
|
58
69
|
},
|
70
|
+
"*"
|
71
|
+
);
|
72
|
+
}
|
73
|
+
// Override console.log to send messages to the parent window
|
74
|
+
console.log = function(...args) {
|
75
|
+
// Log to console
|
76
|
+
dispatchEvent(
|
77
|
+
"storecraft/checkout-log",
|
78
|
+
args
|
79
|
+
);
|
80
|
+
};
|
81
|
+
|
82
|
+
console.error = function(...args) {
|
83
|
+
// Log to console
|
84
|
+
dispatchEvent(
|
85
|
+
"storecraft/checkout-error",
|
86
|
+
args
|
87
|
+
);
|
88
|
+
};
|
89
|
+
|
90
|
+
console.log(
|
91
|
+
window.location.href,
|
92
|
+
);
|
93
|
+
|
94
|
+
window.paypal
|
95
|
+
.Buttons(
|
96
|
+
{
|
97
|
+
style: {
|
98
|
+
shape: "rect",
|
99
|
+
layout: "vertical",
|
100
|
+
color: "gold",
|
101
|
+
label: "paypal",
|
102
|
+
disableMaxWidth: true
|
103
|
+
},
|
104
|
+
|
105
|
+
message: {
|
106
|
+
amount: ${order_data.pricing.total},
|
107
|
+
},
|
108
|
+
|
109
|
+
async createOrder() {
|
110
|
+
try {
|
111
|
+
|
112
|
+
if ('${orderData.id}') {
|
113
|
+
return '${orderData.id}';
|
114
|
+
}
|
115
|
+
const errorDetail = ${orderData?.details?.[0]};
|
116
|
+
const errorMessage = errorDetail
|
117
|
+
? (errorDetail.issue + errorDetail.description + "(" + '${orderData.debug_id}' + ")")
|
118
|
+
: JSON.stringify(orderData);
|
59
119
|
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
120
|
+
throw new Error(errorMessage);
|
121
|
+
} catch (error) {
|
122
|
+
console.error(error);
|
123
|
+
resultMessage("Could not initiate PayPal Checkout...<br><br> " + error);
|
64
124
|
}
|
65
|
-
|
66
|
-
const errorMessage = errorDetail
|
67
|
-
? (errorDetail.issue + errorDetail.description + "(" + '${orderData.debug_id}' + ")")
|
68
|
-
: JSON.stringify(orderData);
|
69
|
-
|
70
|
-
throw new Error(errorMessage);
|
71
|
-
} catch (error) {
|
72
|
-
console.error(error);
|
73
|
-
resultMessage("Could not initiate PayPal Checkout...<br><br> " + error);
|
74
|
-
}
|
75
|
-
},
|
125
|
+
},
|
76
126
|
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
127
|
+
async onApprove(data, actions) {
|
128
|
+
try {
|
129
|
+
const response = await fetch(window.location.origin + "/api/checkout/${order_data.id}/complete", {
|
130
|
+
method: "POST",
|
131
|
+
headers: {
|
132
|
+
"Content-Type": "application/json",
|
133
|
+
},
|
134
|
+
});
|
135
|
+
|
136
|
+
const orderData_main = await response.json();
|
137
|
+
const orderData = orderData_main.payment_gateway.on_checkout_complete;
|
138
|
+
|
139
|
+
// Three cases to handle:
|
140
|
+
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
141
|
+
// (2) Other non-recoverable errors -> Show a failure message
|
142
|
+
// (3) Successful transaction -> Show confirmation or thank you message
|
143
|
+
|
144
|
+
const errorDetail = orderData?.details?.[0];
|
145
|
+
|
146
|
+
if (errorDetail?.issue === "INSTRUMENT_DECLINED") {
|
147
|
+
// (1) Recoverable INSTRUMENT_DECLINED -> call actions.restart()
|
148
|
+
// recoverable state, per
|
149
|
+
// https://developer.paypal.com/docs/checkout/standard/customize/handle-funding-failures/
|
150
|
+
return actions.restart();
|
151
|
+
} else if (errorDetail) {
|
152
|
+
// (2) Other non-recoverable errors -> Show a failure message
|
153
|
+
throw new Error(errorDetail.description + "(" + orderData.debug_id + ")");
|
154
|
+
}
|
155
|
+
// else if (!orderData.purchase_units) {
|
156
|
+
// throw new Error(JSON.stringify(orderData));
|
157
|
+
// }
|
158
|
+
else {
|
159
|
+
// (3) Successful transaction -> Show confirmation or thank you message
|
160
|
+
// Or go to another URL: actions.redirect('thank_you.html');
|
161
|
+
const transaction =
|
162
|
+
orderData?.purchase_units?.[0]?.payments?.captures?.[0] ||
|
163
|
+
orderData?.purchase_units?.[0]?.payments?.authorizations?.[0];
|
164
|
+
resultMessage(
|
165
|
+
"Transaction " + transaction?.status + ":" + transaction?.id +
|
166
|
+
" See console for all available details"
|
167
|
+
);
|
168
|
+
console.log(
|
169
|
+
"Capture result",
|
170
|
+
orderData,
|
171
|
+
JSON.stringify(orderData, null, 2)
|
172
|
+
);
|
173
|
+
dispatchEvent(
|
174
|
+
"storecraft/checkout-complete",
|
175
|
+
{
|
176
|
+
orderData,
|
177
|
+
order_id: '${orderData.id}',
|
178
|
+
payment_gateway: 'paypal'
|
179
|
+
}
|
180
|
+
);
|
181
|
+
|
182
|
+
}
|
183
|
+
} catch (error) {
|
184
|
+
console.error(error);
|
185
|
+
dispatchEvent(
|
186
|
+
"storecraft/checkout-error",
|
187
|
+
{
|
188
|
+
error: error.message,
|
189
|
+
order_id: '${orderData.id}',
|
190
|
+
payment_gateway: 'paypal'
|
191
|
+
}
|
117
192
|
);
|
118
|
-
|
119
|
-
"
|
120
|
-
orderData,
|
121
|
-
JSON.stringify(orderData, null, 2)
|
193
|
+
resultMessage(
|
194
|
+
"Sorry, your transaction could not be processed... " + error
|
122
195
|
);
|
123
196
|
}
|
124
|
-
} catch (error) {
|
125
|
-
console.error(error);
|
126
|
-
resultMessage(
|
127
|
-
"Sorry, your transaction could not be processed... " + error
|
128
|
-
);
|
129
197
|
}
|
198
|
+
|
130
199
|
}
|
131
|
-
|
132
|
-
}
|
133
|
-
).render("#paypal-button-container");
|
200
|
+
).render("#paypal-button-container");
|
134
201
|
|
135
202
|
</script>
|
136
203
|
|
package/adapter.js
CHANGED
@@ -77,8 +77,7 @@ export class Paypal {
|
|
77
77
|
get info() {
|
78
78
|
return {
|
79
79
|
name: 'Paypal payments',
|
80
|
-
description: `
|
81
|
-
You can get started quickly with this 15-minute copy-and-paste integration. If you have an older Checkout integration, you can upgrade your Checkout integration.`,
|
80
|
+
description: `Pay with Paypal Wallet or Credit Card`,
|
82
81
|
url: 'https://developer.paypal.com/docs/checkout/',
|
83
82
|
logo_url: 'https://www.paypalobjects.com/webstatic/mktg/logo/pp_cc_mark_37x23.jpg'
|
84
83
|
}
|