-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPaymentPayPalExpressCheckout.module
397 lines (326 loc) · 13.9 KB
/
PaymentPayPalExpressCheckout.module
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
66
67
68
69
70
71
72
73
74
75
76
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
<?php namespace ProcessWire;
/**
* PayPal Checkout Module
* ProcessWire 3.x, Copyright 2018 by Ryan Cramer
* https://processwire.com
*
*/
class PaymentPayPalExpressCheckout extends PaymentModule {
public static function getModuleInfo() {
return array(
'title' => 'PayPal Checkout',
'version' => '2.0.2',
'summary' => 'PayPal Checkout module using the Client-Side REST API.',
'singular' => false,
'autoload' => false,
'requires' => 'PaymentModule',
'author' => 'PWaddict',
'icon' => 'paypal'
);
}
static public function getDefaultData() {
return array(
"formId" => "checkout-form",
"loadingHTML" => "",
"notificationHTML" => "",
"aboveHTML" => "",
"belowHTML" => "",
"ajaxCustomJavaScriptOnApprove" => "",
"ajax" => 0,
"ajaxId" => ""
);
}
public function init() {
$this->currency = $this->defaultCurrency;
}
private function calculateAmount() {
return $this->getTotalAmount() / 100;
}
public function getTitle() {
return $this->_("PayPal");
}
public function getFailureReason() {
return $this->_("There was something wrong with the payment.");
}
public function processPayment() {
if ($_POST['payment_status'] == 'COMPLETED') {
return true;
}
return false;
}
public function render() {
if ($this->getTotalAmount() <= 0) throw new WireException("Products are not set");
if ($this->processUrl == '') throw new WireException("processUrl is not set");
if ($this->wire('languages')) {
$userLanguage = $this->wire('user')->language;
$lang = $userLanguage->isDefault() ? '' : "__$userLanguage->id";
}
else {
$lang = '';
}
if ($this->data['enviroment'] == "sandbox") {
$clientID = $this->data['clientIdSandbox'];
} else {
$clientID = $this->data['clientIdLive'];
}
$fundings = "";
if ($this->fundingMethods) {
$fundings = array();
foreach ($this->fundingMethods as $method) {
array_push($fundings, $method);
}
$fundings = "&disable-funding=" . implode(',', $fundings);
}
if ($this->data['ajax']) {
$form = "";
$submit = "var xhr = new XMLHttpRequest();
xhr.open('POST', '{$this->processUrl}', true);
xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.onreadystatechange = function () {
if (this.readyState == 4 && this.status == 200) {
document.getElementById('" . $this->data['ajaxId'] . "').innerHTML = this.responseText;
{$this->ajaxCustomJavaScriptOnApprove}
}
};
xhr.send('payment_status=' + response.purchase_units[0].payments.captures[0].status + '');";
} else {
$form = "<form id='{$this->formId}' action='{$this->processUrl}' method='POST'></form>";
$submit = "var form = document.getElementById('{$this->formId}');
input = document.createElement('input');
input.setAttribute('type', 'hidden');
input.setAttribute('name', 'payment_status');
input.setAttribute('value', response.purchase_units[0].payments.captures[0].status);
form.appendChild(input);
form.submit();";
}
$out = "";
$out .= "
<div id='paypal-button-loading'>" . ($this->data['loadingHTML' . $lang] ? $this->data['loadingHTML' . $lang] : $this->data['loadingHTML']) . "</div>
<div id='paypal-button-above'>" . ($this->data['aboveHTML' . $lang] ? $this->data['aboveHTML' . $lang] : $this->data['aboveHTML']) . "</div>
<div id='paypal-button-container'></div>
<div id='paypal-button-notification'>" . ($this->data['notificationHTML' . $lang] ? $this->data['notificationHTML' . $lang] : $this->data['notificationHTML']) . "</div>
<div id='paypal-button-below'>" . ($this->data['belowHTML' . $lang] ? $this->data['belowHTML' . $lang] : $this->data['belowHTML']) . "</div>
{$form}
<script>
document.getElementById('paypal-button-loading').style.display = 'block';
document.getElementById('paypal-button-above').style.display = 'none';
document.getElementById('paypal-button-container').style.display = 'none';
document.getElementById('paypal-button-notification').style.display = 'none';
document.getElementById('paypal-button-below').style.display = 'none';
function loadAsync(url, callback) {
var s = document.createElement('script');
s.setAttribute('src', url); s.onload = callback;
document.head.insertBefore(s, document.head.firstElementChild);
}
loadAsync('https://www.paypal.com/sdk/js?client-id={$clientID}¤cy=" . ($this->data['currency' . $lang] ? $this->data['currency' . $lang] : $this->data['currency']) . "{$fundings}', function() {
paypal.Buttons({
style: {
layout: 'vertical',
color: '{$this->color}',
shape: '{$this->shape}'
},
createOrder: function(data, actions) {
return actions.order.create({
purchase_units: [{
amount: {
value: '" . str_replace(',', '.', $this->calculateAmount()) . "',
currency: '" . ($this->data['currency' . $lang] ? $this->data['currency' . $lang] : $this->data['currency']) . "',
breakdown: {
item_total: {
value: '" . str_replace(',', '.', $this->calculateAmount()) . "',
currency_code: '" . ($this->data['currency' . $lang] ? $this->data['currency' . $lang] : $this->data['currency']) . "'
}
}
},
invoice_id: '{$this->id}',
items: [";
foreach($this->products as $p) {
$htmlEntities = ["&", "<", ">", """, "'"];
$htmlEntitiesReplacement = ["&", "<", ">", "\"", ""];
$out .= "{
name: '" . str_replace($htmlEntities, $htmlEntitiesReplacement, htmlspecialchars_decode($p->title)) . "',
unit_amount: {
value: '" . str_replace(',', '.', $p->amount / 100 ) . "',
currency_code: '" . ($this->data['currency' . $lang] ? $this->data['currency' . $lang] : $this->data['currency']) . "'
},
quantity: {$p->quantity}
},";
}
$out .= "]
}]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(response) {
document.getElementById('paypal-button-above').style.display = 'none';
document.getElementById('paypal-button-container').style.display = 'none';
document.getElementById('paypal-button-notification').style.display = 'block';
document.getElementById('paypal-button-below').style.display = 'none';
{$submit}
});
},
onInit: function() {
document.getElementById('paypal-button-loading').style.display = 'none';
document.getElementById('paypal-button-above').style.display = 'block';
document.getElementById('paypal-button-container').style.display = 'block';
document.getElementById('paypal-button-below').style.display = 'block';
}
}).render('#paypal-button-container');
});
</script>";
return $out;
}
public static function getModuleConfigInputfields(array $data) {
$data = array_merge(self::getDefaultData(), $data);
$inputfields = new InputfieldWrapper();
$modules = wire('modules');
$requiredFields = $modules->get("InputfieldFieldset");
$requiredFields->label = __('Required Fields');
$f = $modules->get('InputfieldText');
$f->name = 'formId';
$f->label = __("Form ID");
$f->description = __("ID attached to dynamically generated form element.");
$f->notes = __("E.g. checkout-form");
$f->required = 1;
$f->columnWidth = 50;
if(isset($data['formId'])) $f->value = $data['formId'];
$requiredFields->add($f);
$f = $modules->get('InputfieldText');
$f->name = 'currency';
$f->label = __("Currency");
$f->description = __("Add the currency code for the transactions.");
$f->notes = __("E.g. USD or EUR etc. All supported codes are listed [here](https://developer.paypal.com/docs/checkout/reference/customize-sdk/#currency).");
$f->required = 1;
$f->columnWidth = 50;
$f->useLanguages = true;
if(isset($data['currency'])) $f->value = $data['currency'];
$requiredFields->add($f);
$f = $modules->get('InputfieldRadios');
$f->name = 'enviroment';
$f->label = __('Enviroment');
$f->required = 1;
$f->columnWidth = 20;
$f->addOption('sandbox', __('Sandbox'));
$f->addOption('production', __('Live'));
$f->value = isset($data['enviroment']) ? $data['enviroment'] : 'sandbox';
$requiredFields->add($f);
$f = $modules->get('InputfieldText');
$f->name = 'clientIdSandbox';
$f->label = __("Sandbox Client ID");
$f->notes = __("Get your Sandbox Client ID by creating a REST API app [here](https://developer.paypal.com/developer/applications/create).");
$f->required = 1;
$f->columnWidth = 40;
if(isset($data['clientIdSandbox'])) $f->value = $data['clientIdSandbox'];
$requiredFields->add($f);
$f = $modules->get('InputfieldText');
$f->name = 'clientIdLive';
$f->label = __("Live Client ID");
$f->notes = __("Get your Live Client ID by creating a REST API app [here](https://developer.paypal.com/developer/applications/create).");
$f->required = 1;
$f->columnWidth = 40;
if(isset($data['clientIdLive'])) $f->value = $data['clientIdLive'];
$requiredFields->add($f);
$inputfields->add($requiredFields);
$optionalFields = $modules->get("InputfieldFieldset");
$optionalFields->label = __('Optional Fields');
$f = $modules->get('InputfieldRadios');
$f->name = 'color';
$f->label = __('Button Color');
$f->columnWidth = 33;
$f->addOption('gold', __('Gold'));
$f->addOption('blue', __('Blue'));
$f->addOption('silver', __('Silver'));
$f->addOption('white', __('White'));
$f->addOption('black', __('Black'));
$f->value = isset($data['color']) ? $data['color'] : 'gold';
$optionalFields->add($f);
$f = $modules->get('InputfieldRadios');
$f->name = 'shape';
$f->label = __('Button Shape');
$f->columnWidth = 33;
$f->addOption('rect', __('Rect'));
$f->addOption('pill', __('Pill'));
$f->value = isset($data['shape']) ? $data['shape'] : 'rect';
$optionalFields->add($f);
$f = $modules->get('InputfieldCheckboxes');
$f->name = 'fundingMethods';
$f->label = __('Hide Funding Buttons');
$f->notes = __('**IMPORTANT**: Real money gaming merchants and/or non-US merchants who do not have the correct licenses and approvals to display the Credit button should disable "PayPal Credit" funding button.');
$f->columnWidth = 33;
$f->addOption('card', __('Debit or Credit Cards'));
$f->addOption('credit', __('PayPal Credit'));
$f->addOption('bancontact', __('Bancontact'));
$f->addOption('blik', __('BLIK'));
$f->addOption('eps', __('eps'));
$f->addOption('giropay', __('giropay'));
$f->addOption('ideal', __('iDEAL'));
$f->addOption('mercadopago', __('Mercado Pago'));
$f->addOption('mybank', __('MyBank'));
$f->addOption('p24', __('Przelewy24'));
$f->addOption('sepa', __('SEPA-Lastschrift'));
$f->addOption('sofort', __('Sofort'));
$f->addOption('venmo', __('Venmo'));
$f->value = isset($data['fundingMethods']) ? $data['fundingMethods'] : 'credit';
$optionalFields->add($f);
$f = $modules->get('InputfieldTextarea');
$f->name = 'loadingHTML';
$f->label = __("Loading Buttons HTML");
$f->description = __("HTML added while the PayPal buttons are fully rendered and ready to be clicked.");
$f->columnWidth = 50;
$f->useLanguages = true;
if(isset($data['loadingHTML'])) $f->value = $data['loadingHTML'];
$optionalFields->add($f);
$f = $modules->get('InputfieldTextarea');
$f->name = 'notificationHTML';
$f->label = __("Processing Notification HTML");
$f->description = __("HTML added after the PayPal payment executed.");
$f->columnWidth = 50;
$f->useLanguages = true;
if(isset($data['notificationHTML'])) $f->value = $data['notificationHTML'];
$optionalFields->add($f);
$f = $modules->get('InputfieldTextarea');
$f->name = 'aboveHTML';
$f->label = __("Above PayPal Buttons HTML");
$f->description = __("HTML added above PayPal buttons after they are fully rendered and ready to be clicked.");
$f->columnWidth = 50;
$f->useLanguages = true;
if(isset($data['aboveHTML'])) $f->value = $data['aboveHTML'];
$optionalFields->add($f);
$f = $modules->get('InputfieldTextarea');
$f->name = 'belowHTML';
$f->label = __("Below PayPal Buttons HTML");
$f->description = __("HTML added below PayPal buttons after they are fully rendered and ready to be clicked.");
$f->columnWidth = 50;
$f->useLanguages = true;
if(isset($data['belowHTML'])) $f->value = $data['belowHTML'];
$optionalFields->add($f);
$f = $modules->get('InputfieldCheckbox');
$f->attr('name+id', 'ajax');
$f->label = __('AJAX Checkout');
$f->notes = __("**IMPORTANT**: Enable this option ONLY if your checkout process is already using AJAX in order to display the success page via AJAX too.");
$f->columnWidth = 50;
$f->attr('checked', isset($data['ajax']) && $data['ajax'] ? 'checked' : '' );
$optionalFields->add($f);
$f = $modules->get('InputfieldText');
$f->name = 'ajaxId';
$f->label = __("AJAX Checkout Div ID");
$f->description = __("Add your div ID you're using to output the AJAX checkout content.");
$f->notes = __("E.g. checkout-ajax");
$f->showIf = "ajax=1";
$f->required = 1;
$f->columnWidth = 50;
if(isset($data['ajaxId'])) $f->value = $data['ajaxId'];
$optionalFields->add($f);
$f = $modules->get('InputfieldTextarea');
$f->name = 'ajaxCustomJavaScriptOnApprove';
$f->label = __("Additional Custom JavaScript (AJAX - onApprove)");
$f->description = __("Additional custom JavaScript executed if the transaction is approved (onApprove function) while AJAX Checkout enabled.");
$f->notes = __("**IMPORTANT**: Use this field ONLY if you know what you're doing.");
$f->showIf = "ajax=1";
$f->columnWidth = 100;
if(isset($data['ajaxCustomJavaScriptOnApprove'])) $f->value = $data['ajaxCustomJavaScriptOnApprove'];
$optionalFields->add($f);
$inputfields->add($optionalFields);
return $inputfields;
}
}