Skip to content

Commit

Permalink
Merge pull request #156 from antondrob/master
Browse files Browse the repository at this point in the history
Fix capture and sub changes
  • Loading branch information
markusbrunke authored Sep 6, 2022
2 parents 4a1371f + f2c90d1 commit f2d0f48
Show file tree
Hide file tree
Showing 17 changed files with 2,501 additions and 2,269 deletions.
1 change: 1 addition & 0 deletions Readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ The Reepay plugin extends WooCommerce allowing you to take payments on your stor
See installation guide right here: https://intercom.help/reepay/reepay-plugins/woocommerce-plugin

== Changelog ==
v 1.4.30 - Fix capture
v 1.4.29 - Mobile conditions subscriptions, allow any webhooks, subscriptions recurring
v 1.4.28 - Settle fee on complete
v 1.4.27 - Update customer after reorder
Expand Down
57 changes: 35 additions & 22 deletions assets/js/thankyou.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
jQuery( function( $ ) {
jQuery(function ($) {
'use strict';

window.wc_reepay_thankyou = {
Expand All @@ -8,51 +8,64 @@ jQuery( function( $ ) {
/**
* Initialize the checking
*/
init: function() {
this.checkPayment( function ( err, data ) {
var status_elm = $( '#order-status-checking' ),
success_elm = $( '#order-success' ),
failed_elm = $( '#order-failed' );
init: function () {
this.checkPayment(function (err, data) {
var status_elm = $('#order-status-checking'),
success_elm = $('#order-success'),
failed_elm = $('#order-failed');

switch ( data.state ) {
switch (data.state) {
case 'paid':
status_elm.hide();
success_elm.show();
break;
case 'reload':
$('.woocommerce-order').block({
message: WC_Reepay_Thankyou.check_message,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
});
setTimeout(function () {
location.reload();
}, 2000);

break;
case 'failed':
case 'aborted':
status_elm.hide();
failed_elm.append( '<p class="transaction-error">' + data.message + "</p>" );
failed_elm.append('<p class="transaction-error">' + data.message + "</p>");
failed_elm.show();
break;
default:
window.wc_reepay_thankyou.attempts++;

if ( window.wc_reepay_thankyou.attempts > 6) {
if (window.wc_reepay_thankyou.attempts > 6) {
return;
}

setTimeout(function () {
window.wc_reepay_thankyou.init();
}, 10000);
}
} );
});
},

/**
* Check payment
* @return {JQueryPromise<any>}
*/
checkPayment: function ( callback ) {
$( '.woocommerce-order' ).block( {
checkPayment: function (callback) {
$('.woocommerce-order').block({
message: WC_Reepay_Thankyou.check_message,
overlayCSS: {
background: '#fff',
opacity: 0.6
}
} );
});

return $.ajax( {
return $.ajax({
type: 'POST',
url: WC_Reepay_Thankyou.ajax_url,
data: {
Expand All @@ -62,15 +75,15 @@ jQuery( function( $ ) {
order_key: WC_Reepay_Thankyou.order_key,
},
dataType: 'json'
} ).always( function() {
$( '.woocommerce-order' ).unblock();
} ).done( function ( response ) {
callback( null, response.data );
} );
}).always(function () {
$('.woocommerce-order').unblock();
}).done(function (response) {
callback(null, response.data);
});
},
};

$(document).ready( function () {
$(document).ready(function () {
window.wc_reepay_thankyou.init();
} );
} );
});
});
2 changes: 1 addition & 1 deletion assets/js/thankyou.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion assets/js/thankyou.min.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

54 changes: 46 additions & 8 deletions includes/abstracts/abstract-wc-gateway-reepay.php
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ public function process_payment($order_id)
}
}


// Try to charge with saved token
if (absint($token_id) > 0) {
$token = new WC_Payment_Token_Reepay($token_id);
Expand All @@ -568,16 +569,43 @@ public function process_payment($order_id)
// Don't charge payment if zero amount
$order->payment_complete();
} else {
// Charge payment
$result = $this->api->charge($order, $token->get_token(), $order->get_total(), $order->get_currency());
if (is_wp_error($result)) {
wc_add_notice($result->get_error_message(), 'error');

return false;
// Replace token
try {
self::assign_payment_token($order, $token);
} catch (Exception $e) {
$order->add_order_note($e->getMessage());

return array(
'result' => 'failure',
'message' => $e->getMessage()
);
}

if (wcs_cart_only_subscriptions()) {
$method = $this->api->request('GET', 'https://api.reepay.com/v1/payment_method/' . $token->get_token());
if (!empty($method) && $method['state'] == 'active') {

$data = [
'payment_method' => $method['id'],
'customer' => $method['customer'],
];

do_action('reepay_create_subscription', $data, $order);
}
} else {
// Charge payment
$result = $this->api->charge($order, $token->get_token(), $order->get_total(), $order->get_currency());
if (is_wp_error($result)) {
wc_add_notice($result->get_error_message(), 'error');

return false;
}

// Settle the charge
do_action('reepay_instant_settle', $order);
}

// Settle the charge
do_action('reepay_instant_settle', $order);
}

try {
Expand Down Expand Up @@ -634,6 +662,11 @@ public function process_payment($order_id)
'https://checkout-api.reepay.com/v1/session/recurring',
$params
);

if (!empty($result['id'])) {
update_post_meta($order_id, 'reepay_session_id', $result['id']);
}

if (is_wp_error($result)) {
/** @var WP_Error $result */
throw new Exception($result->get_error_message(), $result->get_error_code());
Expand Down Expand Up @@ -694,8 +727,11 @@ public function process_payment($order_id)
'cancel_url' => $order->get_cancel_order_url(),
];

// Get setting from parent method
$settings = get_option('woocommerce_reepay_checkout_settings');

if ($params['recurring']) {
$params['button_text'] = __('PAY AND SAVE CARD', 'reepay-checkout-gateway');
$params['button_text'] = $settings['payment_button_text'];
}

if (!empty($country)) {
Expand Down Expand Up @@ -766,6 +802,8 @@ public function process_payment($order_id)
$handle = rp_get_order_handle($order, true);
$params['order']['handle'] = $handle;

update_post_meta($order->get_id(), '_reepay_order', $handle);

$result = $this->api->request(
'POST',
'https://checkout-api.reepay.com/v1/session/charge',
Expand Down
Loading

0 comments on commit f2d0f48

Please sign in to comment.