Skip to content

Commit

Permalink
PT-762: Additional CONFIRMED call for AUTHORIZED orders (#117)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivan-pugach-mondu authored May 14, 2024
1 parent 7056ac1 commit 457b571
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mondu-buy-now-pay-later.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
* Plugin Name: Mondu Buy Now Pay Later
* Plugin URI: https://github.com/mondu-ai/bnpl-checkout-woocommerce/releases
* Description: Mondu provides B2B E-commerce and B2B marketplaces with an online payment solution to buy now and pay later.
* Version: 2.2.0
* Version: 2.2.1
* Author: Mondu
* Author URI: https://mondu.ai
*
Expand All @@ -27,7 +27,7 @@
die( 'Direct access not allowed' );
}

define( 'MONDU_PLUGIN_VERSION', '2.2.0' );
define( 'MONDU_PLUGIN_VERSION', '2.2.1' );
define( 'MONDU_PLUGIN_FILE', __FILE__ );
define( 'MONDU_PLUGIN_PATH', __DIR__ );
define( 'MONDU_PLUGIN_BASENAME', plugin_basename(MONDU_PLUGIN_FILE) );
Expand Down
37 changes: 37 additions & 0 deletions src/Mondu/Admin/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public function init() {
add_action('wp_ajax_cancel_invoice', [ $this, 'cancel_invoice' ]);
add_action('wp_ajax_create_invoice', [ $this, 'create_invoice' ]);

add_action('wp_ajax_confirm_order', [ $this, 'confirm_order' ]);

$this->mondu_request_wrapper = new MonduRequestWrapper();
}

Expand Down Expand Up @@ -117,4 +119,39 @@ public function create_invoice() {
]);
}
}

public function confirm_order() {
$is_nonce_valid = check_ajax_referer( 'mondu-confirm-order', 'security', false );
if ( !$is_nonce_valid ) {
status_header(400);
exit(esc_html__('Bad Request.', 'mondu'));
}

$order_id = isset($_POST['order_id']) ? sanitize_text_field($_POST['order_id']) : '';

$order = new WC_Order($order_id);
if ( null === $order ) {
return;
}

$mondu_order_id = isset($_POST['order_uuid']) ? sanitize_text_field($_POST['order_uuid']) : '';

if ( !$mondu_order_id ) {
return;
}

try {
$this->mondu_request_wrapper->confirm_order($order_id, $mondu_order_id);
} catch ( ResponseException $e ) {
wp_send_json([
'error' => true,
'message' => $e->getMessage(),
]);
} catch ( MonduException $e ) {
wp_send_json([
'error' => true,
'message' => $e->getMessage(),
]);
}
}
}
2 changes: 1 addition & 1 deletion src/Mondu/Mondu/Controllers/OrdersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function confirm( WP_REST_Request $request ) {
if ( in_array( $order->get_status(), [ 'pending', 'failed' ] ) ) {
$order->update_status('wc-on-hold', __('On hold', 'woocommerce'));

$this->mondu_request_wrapper->confirm_order($order->get_id(), $mondu_order_id, $order_number);
$this->mondu_request_wrapper->confirm_order($order->get_id(), $mondu_order_id);
}
} catch ( \Exception $e ) {
Helper::log([
Expand Down
18 changes: 18 additions & 0 deletions src/Mondu/Mondu/Presenters/PaymentInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,24 @@ public function get_mondu_section_html() {
<?php
}
?>
<?php
if ( in_array($this->order_data['state'], [ 'authorized' ], true) &&
$this->order->get_status() === 'on-hold'
) {
?>
<?php
$mondu_data = [
'order_id' => $this->order->get_id(),
'order_uuid' => $order_data['uuid'],
'security' => wp_create_nonce('mondu-confirm-order'),
];
?>
<button data-mondu='<?php echo( wp_json_encode($mondu_data) ); ?>' id="mondu-confirm-order-button" type="submit" class="button grant_access">
<?php esc_html_e('Confirm Order', 'mondu'); ?>
</button>
<?php
}
?>
</section>
<hr>
<?php $this->get_mondu_payment_html(); ?>
Expand Down
24 changes: 24 additions & 0 deletions views/admin/js/invoice.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,29 @@
});
}
})
$('#mondu-confirm-order-button').on('click', function (e) {
e.preventDefault();
if(confirm("Are you sure you want to confirm the Mondu's order?")) {
var attributes = JSON.parse($('#mondu-confirm-order-button')[0].attributes['data-mondu'].value);
jQuery.ajax({
type: 'POST',
url: ajaxurl,
data: {
action: 'confirm_order',
...attributes
},
success: function(res) {
if(res.error) {
alert(res.message);
} else {
location.reload();
}
}
});
}
})
});
</script>

0 comments on commit 457b571

Please sign in to comment.