-
Notifications
You must be signed in to change notification settings - Fork 20
/
class-wc-gateway-checkout-com-apple-pay.php
620 lines (510 loc) · 23.1 KB
/
class-wc-gateway-checkout-com-apple-pay.php
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
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
<?php
include_once('settings/class-wc-checkoutcom-cards-settings.php');
class WC_Gateway_Checkout_Com_Apple_Pay extends WC_Payment_Gateway
{
/**
* WC_Gateway_Checkout_Com_Apple_Pay constructor.
*/
public function __construct()
{
$this->id = 'wc_checkout_com_apple_pay';
$this->method_title = __("Checkout.com", 'wc_checkout_com');
$this->method_description = __("The Checkout.com extension allows shop owners to process online payments through the <a href=\"https://www.checkout.com\">Checkout.com Payment Gateway.</a>", 'wc_checkout_com');
$this->title = __("Apple Pay", 'wc_checkout_com');
$this->has_fields = true;
$this->supports = array( 'products', 'refund' );
$this->init_form_fields();
$this->init_settings();
// Turn these settings into variables we can use
foreach ( $this->settings as $setting_key => $value ) {
$this->$setting_key = $value;
}
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
// Redirection hook
add_action( 'woocommerce_api_wc_checkoutcom_session', array( $this, 'applepay_sesion' ) );
add_action( 'woocommerce_api_wc_checkoutcom_generate_token', array( $this, 'applepay_token' ) );
}
/**
* Show module configuration in backend
*
* @return string|void
*/
public function init_form_fields()
{
$this->form_fields = WC_Checkoutcom_Cards_Settings::apple_settings();
$this->form_fields = array_merge( $this->form_fields, array(
'screen_button' => array(
'id' => 'screen_button',
'type' => 'screen_button',
'title' => __( 'Other Settings', 'wc_checkout_com' ),
)
));
}
/**
* @param $key
* @param $value
*/
public function generate_screen_button_html( $key, $value )
{
WC_Checkoutcom_Admin::generate_links($key, $value);
}
/**
* Show frames js on checkout page
*/
public function payment_fields()
{
global $woocommerce;
$chosen_methods = wc_get_chosen_shipping_method_ids();
$chosen_shipping = $chosen_methods[0];
$shipping_amount = WC()->cart->get_shipping_total();
$checkoutFields = json_encode($woocommerce->checkout->checkout_fields,JSON_HEX_APOS);
$session_url = str_replace( 'https:', 'https:', add_query_arg( 'wc-api', 'wc_checkoutcom_session', home_url( '/' ) ) );
$generate_token_url = str_replace( 'https:', 'https:', add_query_arg( 'wc-api', 'wc_checkoutcom_generate_token', home_url( '/' ) ) );
$apple_settings = get_option('woocommerce_wc_checkout_com_apple_pay_settings');
$mada_enabled = isset( $apple_settings['enable_mada_apple_pay'] ) && ( $apple_settings['enable_mada_apple_pay'] === 'yes' );
if(!empty($this->get_option( 'description' ))){
echo $this->get_option( 'description' );
}
// get country of current user
$country_code = WC()->customer->get_billing_country();
$supportedNetworks = ['amex', 'masterCard', 'visa'];
if ($mada_enabled) {
array_push($supportedNetworks, 'mada');
$country_code = 'SA';
}
?>
<!-- Input needed to sent the card token -->
<input type="hidden" id="cko-apple-card-token" name="cko-apple-card-token" value="" />
<!-- ApplePay warnings -->
<p style="display:none" id="ckocom_applePay_not_actived">ApplePay is possible on this browser, but not currently activated.</p>
<p style="display:none" id="ckocom_applePay_not_possible">ApplePay is not available on this browser</p>
<script type="text/javascript">
// Magic strings used in file
var applePayOptionSelector = '.payment_method_wc_checkout_com_apple_pay';
var applePayButtonId = 'ckocom_applePay';
// Warning messages for ApplePay
var applePayNotActivated = document.getElementById('ckocom_applePay_not_actived');
var applePayNotPossible = document.getElementById('ckocom_applePay_not_possible');
// Initially hide the ApplePay as a payment option
hideAppleApplePayOption();
// If ApplePay is available as a payment option, and enabled on the checkout page, un-hide the payment option
if (window.ApplePaySession) {
var promise = ApplePaySession.canMakePaymentsWithActiveCard("<?php echo $this->get_option( 'ckocom_apple_mercahnt_id' ); ?>");
promise.then(function (canMakePayments) {
if (canMakePayments) {
showAppleApplePayOption();
} else {
displayApplePayNotPossible();
}
});
} else {
displayApplePayNotPossible();
}
// Display the button and remove the default place order
checkoutInitialiseApplePay = function () {
jQuery('#payment').append('<div id="' + applePayButtonId + '" class="apple-pay-button '
+ "<?php echo $this->get_option( 'ckocom_apple_type' ); ?>" + " "
+ "<?php echo $this->get_option( 'ckocom_apple_theme' ); ?>" + '" lang="'
+ "<?php echo $this->get_option( 'ckocom_apple_language' ); ?>" + '"></div>');
jQuery('#ckocom_applePay').hide();
};
// Listen for when the ApplePay button is pressed
jQuery(document).unbind("click").on('click', '#' + applePayButtonId, function () {
var checkoutFields = '<?php echo $checkoutFields?>';
var result = isValidFormField(checkoutFields);
if(result){
var applePaySession = new ApplePaySession(3, getApplePayConfig());
handleApplePayEvents(applePaySession);
applePaySession.begin();
}
});
/**
*Get the configuration needed to initialise the ApplePay session
*
* @param {function} callback
*/
function getApplePayConfig() {
var networksSupported = <?php echo json_encode($supportedNetworks); ?>;
return {
currencyCode: "<?php echo get_woocommerce_currency(); ?>",
countryCode: "<?php echo $country_code; ?>",
merchantCapabilities: ['supports3DS', 'supportsEMV', 'supportsCredit', 'supportsDebit'],
supportedNetworks: networksSupported,
total: {
label: window.location.host,
amount: "<?php echo $woocommerce->cart->total ?>",
type: 'final'
}
}
}
/**
* Handle ApplePay events
*/
function handleApplePayEvents(session) {
/**
* An event handler that is called when the payment sheet is displayed.
*
* @param {object} event - The event contains the validationURL
*/
session.onvalidatemerchant = function (event) {
performAppleUrlValidation(event.validationURL, function (merchantSession) {
session.completeMerchantValidation(merchantSession);
});
};
/**
* An event handler that is called when a new payment method is selected.
*
* @param {object} event - The event contains the payment method selected
*/
session.onpaymentmethodselected = function (event) {
// base on the card selected the total can be change, if for example you
// plan to charge a fee for credit cards for example
var newTotal = {
type: 'final',
label: window.location.host,
amount: "<?php echo $woocommerce->cart->total ?>",
};
var newLineItems = [
{
type: 'final',
label: 'Subtotal',
amount: "<?php echo $woocommerce->cart->subtotal ?>"
},
{
type: 'final',
label: 'Shipping - ' + "<?php echo $chosen_shipping ?>",
amount: "<?php echo $shipping_amount ?>"
}
];
// if (<?php //echo $this->getPaymentInfo()['discounts'] ?>// > 0) {
// newLineItems.push({
// type: 'final',
// label: 'Discount',
// amount: "-<?php //echo $this->getPaymentInfo()['discounts'] ?>//"
// })
// }
session.completePaymentMethodSelection(newTotal, newLineItems);
};
/**
* An event handler that is called when the user has authorized the Apple Pay payment
* with Touch ID, Face ID, or passcode.
*/
session.onpaymentauthorized = function (event) {
generateCheckoutToken(event.payment.token.paymentData, function (outcome) {
if (outcome) {
document.getElementById('cko-apple-card-token').value = outcome;
status = ApplePaySession.STATUS_SUCCESS;
// jQuery('#place_order').prop("disabled",false);
jQuery('#place_order').prop("disabled",false);
jQuery('#place_order').trigger('click');
} else {
status = ApplePaySession.STATUS_FAILURE;
}
session.completePayment(status);
});
};
/**
* An event handler that is automatically called when the payment UI is dismissed.
*/
session.oncancel = function (event) {
// popup dismissed
};
}
/**
*Perform the session validation
*
* @param {string} valURL validation URL from Apple
* @param {function} callback
*/
function performAppleUrlValidation(valURL, callback) {
jQuery.ajax({
type: 'POST',
url: "<?php echo $session_url ?>",
data: {
url: valURL,
merchantId: "<?php echo $this->get_option( 'ckocom_apple_mercahnt_id' ); ?>",
domain: window.location.host,
displayName: window.location.host,
},
success: function (outcome) {
var data = JSON.parse(outcome);
callback(data);
}
});
}
/**
* Generate the checkout.com token based on the ApplePAy payload
*
* @param {function} callback
*/
function generateCheckoutToken(token, callback) {
jQuery.ajax({
type: 'POST',
url: "<?php echo $generate_token_url; ?>",
data: {
token: token
},
success: function (outcome) {
callback(outcome);
},
error: function () {
callback('');
}
});
}
/**
* This will display the ApplePay not activated message
*/
function displayApplePayNotActivated() {
applePayNotActivated.style.display = '';
}
/**
* This will display the ApplePay not possible message
*/
function displayApplePayNotPossible() {
applePayNotPossible.style.display = '';
}
/**
* Hide the ApplePay payment option from the checkout page
*/
function hideAppleApplePayOption() {
jQuery(applePayOptionSelector).hide();
// jQuery('#ckocom_applePay').hide();
// jQuery(applePayOptionBodySelector).hide();
}
/**
* Show the ApplePay payment option on the checkout page
*/
function showAppleApplePayOption() {
jQuery( applePayOptionSelector ).show();
// jQuery('.apple-pay-button').show();
// jQuery(applePayOptionBodySelector).show();
if ( jQuery( '.payment_method_wc_checkout_com_apple_pay' ).is( ':visible' ) ) {
//check if Apple Pay method is checked.
if ( jQuery( '#payment_method_wc_checkout_com_apple_pay' ).is( ':checked' ) ) {
// Disable place order button.
// jQuery('#place_order').prop("disabled",true);
jQuery( '#place_order' ).hide();
// Show Apple Pay button.
jQuery( '#ckocom_applePay' ).show();
} else {
// Show default place order button.
jQuery( '#place_order' ).show();
// Hide apple pay button.
// jQuery('#place_order').prop("disabled",false);
jQuery( '#ckocom_applePay' ).hide();
}
// On payment radio button click.
jQuery( "input[name='payment_method']" ).click( function () {
// Check if payment method is google pay
if ( this.value == 'wc_checkout_com_apple_pay' ) {
// Hide default place order button.
// jQuery('#place_order').prop("disabled",true);
jQuery( '#place_order' ).hide();
// Show Apple Pay button.
jQuery( '#ckocom_applePay' ).show();
} else {
// Enable place order button.
// jQuery('#place_order').prop("disabled",false);
jQuery( '#place_order' ).show();
// Hide apple pay button.
jQuery( '#ckocom_applePay' ).hide();
}
} )
} else {
jQuery( '#place_order' ).prop( "disabled", false );
}
}
// Initialise apple pay when page is ready
jQuery( document ).ready(function() {
checkoutInitialiseApplePay();
});
// Validate checkout form before submitting order
function isValidFormField(fieldList) {
var result = {error: false, messages: []};
var fields = JSON.parse(fieldList);
if(jQuery('#terms').length === 1 && jQuery('#terms:checked').length === 0){
result.error = true;
result.messages.push({target: 'terms', message : 'You must accept our Terms & Conditions.'});
}
if (fields) {
jQuery.each(fields, function(group, groupValue) {
if (group === 'shipping' && jQuery('#ship-to-different-address-checkbox:checked').length === 0) {
return true;
}
jQuery.each(groupValue, function(name, value ) {
if (!value.hasOwnProperty('required')) {
return true;
}
if (name === 'account_password' && jQuery('#createaccount:checked').length === 0) {
return true;
}
var inputValue = jQuery('#' + name).length > 0 && jQuery('#' + name).val().length > 0 ? jQuery('#' + name).val() : '';
if (value.required && jQuery('#' + name).length > 0 && jQuery('#' + name).val().length === 0) {
result.error = true;
result.messages.push({target: name, message : value.label + ' is a required field.'});
}
if (value.hasOwnProperty('type')) {
switch (value.type) {
case 'email':
var reg = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/;
var correct = reg.test(inputValue);
if (!correct) {
result.error = true;
result.messages.push({target: name, message : value.label + ' is not correct email.'});
}
break;
case 'tel':
var tel = inputValue;
var filtered = tel.replace(/[\s\#0-9_\-\+\(\)]/g, '').trim();
if (filtered.length > 0) {
result.error = true;
result.messages.push({target: name, message : value.label + ' is not correct phone number.'});
}
break;
}
}
});
});
} else {
result.error = true;
result.messages.push({target: false, message : 'Empty form data.'});
}
if (!result.error) {
return true;
}
jQuery('.woocommerce-error, .woocommerce-message').remove();
jQuery.each(result.messages, function(index, value) {
jQuery('form.checkout').prepend('<div class="woocommerce-error">' + value.message + '</div>');
});
jQuery('html, body').animate({
scrollTop: (jQuery('form.checkout').offset().top - 100 )
}, 1000 );
jQuery(document.body).trigger('checkout_error');
return false;
}
</script>
<?php
}
public function applepay_sesion() {
$url = $_POST["url"];
$domain = $_POST["domain"];
$displayName = $_POST["displayName"];
$merchantId = $this->get_option( 'ckocom_apple_mercahnt_id' );
$certificate = $this->get_option( 'ckocom_apple_certificate' );
$certificateKey = $this->get_option( 'ckocom_apple_key' );
if (
"https" == parse_url( $url, PHP_URL_SCHEME ) &&
substr( parse_url( $url, PHP_URL_HOST ), - 10 ) == ".apple.com"
) {
$ch = curl_init();
$data =
'{
"merchantIdentifier":"' . $merchantId . '",
"domainName":"' . $domain . '",
"displayName":"' . $displayName . '"
}';
curl_setopt( $ch, CURLOPT_URL, $url );
curl_setopt( $ch, CURLOPT_SSLCERT, $certificate );
curl_setopt( $ch, CURLOPT_SSLKEY, $certificateKey );
curl_setopt( $ch, CURLOPT_POST, 1 );
curl_setopt( $ch, CURLOPT_POSTFIELDS, $data );
// TODO: throw error and log it
if ( curl_exec( $ch ) === false ) {
echo '{"curlError":"' . curl_error( $ch ) . '"}';
}
// close cURL resource, and free up system resources
curl_close( $ch );
exit();
}
}
public function applepay_token() {
// Generate apple token
$token = WC_Checkoutcom_Api_request::generate_apple_token();
echo $token;
exit();
}
/**
* Process payment with apple pay
*
* @param int $order_id
*
* @return array
*/
public function process_payment( $order_id ) {
if ( ! session_id() ) {
session_start();
}
global $woocommerce;
$order = new WC_Order( $order_id );
// create google token from google payment data
$apple_token = $_POST['cko-apple-card-token'];
// Check if apple token is not empty
if ( empty( $apple_token ) ) {
WC_Checkoutcom_Utility::wc_add_notice_self( __( 'There was an issue completing the payment.', 'wc_checkout_com' ), 'error' );
return;
}
// Create payment with google token
$result = (array) ( new WC_Checkoutcom_Api_request )->create_payment( $order, $apple_token );
// check if result has error and return error message
if ( isset( $result['error'] ) && ! empty( $result['error'] ) ) {
WC_Checkoutcom_Utility::wc_add_notice_self( __( $result['error'] ), 'error' );
return;
}
// Set action id as woo transaction id
update_post_meta( $order_id, '_transaction_id', $result['action_id'] );
update_post_meta( $order_id, '_cko_payment_id', $result['id'] );
// Get cko auth status configured in admin
$status = WC_Admin_Settings::get_option( 'ckocom_order_authorised', 'on-hold' );
$message = __( "Checkout.com Payment Authorised " . "</br>" . " Action ID : {$result['action_id']} ", 'wc_checkout_com' );
// check if payment was flagged
if ( $result['risk']['flagged'] ) {
// Get cko auth status configured in admin
$status = WC_Admin_Settings::get_option( 'ckocom_order_flagged', 'flagged' );
$message = __( "Checkout.com Payment Flagged " . "</br>" . " Action ID : {$result['action_id']} ", 'wc_checkout_com' );
}
// add notes for the order and update status
$order->add_order_note( $message );
$order->update_status( $status );
// Reduce stock levels
wc_reduce_stock_levels( $order_id );
// Remove cart
$woocommerce->cart->empty_cart();
// Return thank you page
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
);
}
/**
* Process refund for the order.
*
* @param int $order_id Order ID.
* @param int $amount Amount to refund.
* @param string $reason Refund reason.
*
* @return bool
*/
public function process_refund( $order_id, $amount = null, $reason = '' ) {
$order = wc_get_order( $order_id );
$result = (array) WC_Checkoutcom_Api_request::refund_payment( $order_id, $order );
// check if result has error and return error message.
if ( isset( $result['error'] ) && ! empty( $result['error'] ) ) {
WC_Checkoutcom_Utility::wc_add_notice_self( $result['error'] );
return false;
}
// Set action id as woo transaction id.
update_post_meta( $order_id, '_transaction_id', $result['action_id'] );
update_post_meta( $order_id, 'cko_payment_refunded', true );
if ( isset( $_SESSION['cko-refund-is-less'] ) ) {
if ( $_SESSION['cko-refund-is-less'] ) {
$order->add_order_note( sprintf( __( 'Checkout.com Payment Partially refunded from Admin - Action ID : %s', 'wc_checkout_com' ), $result['action_id'] ) );
unset( $_SESSION['cko-refund-is-less'] );
return true;
}
}
$order->add_order_note( sprintf( __( 'Checkout.com Payment refunded from Admin - Action ID : %s', 'wc_checkout_com' ), $result['action_id'] ) );
// when true is returned, status is changed to refunded automatically.
return true;
}
}