Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cancel on expiry and duplication of payments #121

Merged
merged 2 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions includes/class-wc-gateway-komoju-ipn-handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ public function valid_response($webhookEvent)

$order = $this->get_komoju_order($webhookEvent, $this->invoice_prefix);
if ($order) {
$this->save_komoju_meta_data($order, $webhookEvent);
switch ($webhookEvent->status()) {
case 'captured':
$this->payment_status_captured($order, $webhookEvent);
Expand Down Expand Up @@ -214,8 +215,6 @@ protected function payment_status_captured($order, $webhookEvent)
$this->validate_amount($order, $webhookEvent->grand_total() - $webhookEvent->payment_method_fee());
}

$this->save_komoju_meta_data($order, $webhookEvent);

if ('captured' === $webhookEvent->status()) {
$this->payment_complete($order, !empty($webhookEvent->uuid()) ? wc_clean($webhookEvent->uuid()) : '', __('IPN payment captured', 'komoju-woocommerce'));

Expand Down
14 changes: 14 additions & 0 deletions includes/class-wc-gateway-komoju-single-slug.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,17 @@ public function process_payment($order_id, $payment_type = null)
// Otherwise we will redirect to the KOMOJU hosted page.
$token = sanitize_text_field($_POST['komoju_payment_token']);

// Gimmick: If there is a KOMOJU payment UUID already set for the order, try to cancel the preceding payment.
// Some considerations:
// - It will be helpful to prevent duplicated Konbini payments, which can be caused through the "order-pay" page.
// - If the metadata is set, the payment already exists, and it is safe to cancel the payment.
// - It is also worth to note that the paths to this code guarantee that this order is payable, i.e., no payment is captured or has expired yet, and therefore it is cancellable.
// - If the metadata is not set, there is nothing we can do anyway.
$komoju_payment_id = $order->get_meta('komoju_payment_id');
if (!empty($komoju_payment_id)) {
$this->komoju_api->cancel($komoju_payment_id, []);
}

if (!$token || $token === '') {
return parent::process_payment($order_id, $this->payment_method['type_slug']);
}
Expand All @@ -215,6 +226,9 @@ public function process_payment($order_id, $payment_type = null)
'payment_details' => $token,
]);

$order->set_transaction_id($session->payment_data->external_order_num);
$order->save();

if ($result->redirect_url) {
return [
'result' => 'success',
Expand Down
5 changes: 5 additions & 0 deletions komoju-php/komoju-php/lib/komoju/KomojuApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public function refund($paymentUuid, $payload)
return $this->post('/api/v1/payments/' . $paymentUuid . '/refund', $payload);
}

public function cancel($paymentUuid, $payload)
{
return $this->post('/api/v1/payments/' . $paymentUuid . '/cancel', $payload);
}

private function get($uri, $asArray = false)
{
$ch = curl_init($this->endpoint . $uri);
Expand Down
Loading