Skip to content

Commit

Permalink
Merge pull request #121 from degica/20240730-cancel-on-expiry
Browse files Browse the repository at this point in the history
Cancel on expiry and duplication of payments
  • Loading branch information
Dinwy authored Oct 10, 2024
2 parents 87c6b4b + 057be1b commit a58985b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 2 deletions.
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

0 comments on commit a58985b

Please sign in to comment.