Skip to content

Commit

Permalink
adds payment validation polling
Browse files Browse the repository at this point in the history
  • Loading branch information
10xSebastian committed Sep 2, 2022
1 parent cf1e537 commit b6be270
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion dist/checkout.js

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

47 changes: 47 additions & 0 deletions includes/class-depay-wc-payments-rest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function register_routes() {
register_rest_route( 'depay/wc', '/checkouts/(?P<id>\d+)', [ 'methods' => 'GET', 'callback' => [ $this, 'get_checkout_accept' ] ]);
register_rest_route( 'depay/wc', '/checkouts/(?P<id>\d+)/track', [ 'methods' => 'POST', 'callback' => [ $this, 'track_payment' ] ]);
register_rest_route( 'depay/wc', '/validate', [ 'methods' => 'POST', 'callback' => [ $this, 'validate_payment' ] ]);
register_rest_route( 'depay/wc', '/release', [ 'methods' => 'POST', 'callback' => [ $this, 'check_release' ] ]);
register_rest_route( 'depay/wc', '/transactions', [ 'methods' => 'GET', 'callback' => [ $this, 'fetch_transactions' ], 'permission_callback' => array( $this, 'get_transactions_permission' ) ]);
}

Expand Down Expand Up @@ -137,6 +138,52 @@ public function track_payment($request) {
return $response;
}

public function check_release($request) {
global $wpdb;

$checkout_id = $request->get_param('checkout_id');
$existing_transaction_status = $wpdb->get_var(
$wpdb->prepare(
"SELECT status FROM wp_wc_depay_transactions WHERE checkout_id = %d ORDER BY id DESC LIMIT 1",
$checkout_id
)
);

if(empty($existing_transaction_status) || $existing_transaction_status == 'VALIDATING') {
$response = new WP_REST_Response();
$response->set_status(404);
return $response;
}

$order_id = $wpdb->get_var(
$wpdb->prepare(
"SELECT order_id FROM wp_wc_depay_transactions WHERE checkout_id = %d ORDER BY id DESC LIMIT 1",
$checkout_id
)
);
$order = wc_get_order($order_id);

if($existing_transaction_status == 'SUCCESS') {
$response = rest_ensure_response([
"forward_to" => $order->get_checkout_order_received_url()
]);
$response->set_status(200);
return $response;
} else {
$failed_reason = $wpdb->get_var(
$wpdb->prepare(
"SELECT failed_reason FROM wp_wc_depay_transactions WHERE checkout_id = %d ORDER BY id DESC LIMIT 1",
$checkout_id
)
);
$response = rest_ensure_response([
"failed_reason" => $failed_reason
]);
$response->set_status(409);
return $response;
}
}

public function validate_payment($request) {
global $wpdb;
$response = new WP_REST_Response();
Expand Down
9 changes: 9 additions & 0 deletions src/checkout.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ window.addEventListener( 'hashchange', async()=> {
.done((result)=>resolve({ status: 200 }))
.fail((request, status)=>reject(status))
})
},
poll: {
method: ()=>{
return fetch('/index.php?rest_route=/depay/wc/release', {
method: 'POST',
body: JSON.stringify({ checkout_id: checkoutId }),
headers: { "Content-Type": "application/json" }
})
}
}
}
})
Expand Down

0 comments on commit b6be270

Please sign in to comment.