Skip to content

Commit

Permalink
fix double complete (#15)
Browse files Browse the repository at this point in the history
  • Loading branch information
ntheile authored Dec 20, 2023
1 parent d5a5bbe commit 94ab079
Showing 1 changed file with 7 additions and 36 deletions.
43 changes: 7 additions & 36 deletions plugin/zaprite.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,11 +83,6 @@ public function __construct()
));
add_action('woocommerce_api_wc_gateway_' . $this->id, array($this, 'check_payment'));

// This action allows us to set the order to complete even if in a local dev environment
add_action('woocommerce_thankyou', array(
$this,
'check_payment'
));
}

/**
Expand Down Expand Up @@ -152,7 +147,7 @@ public function init_form_fields()
'zaprite_api_key' => array(
'title' => __('Zaprite API Key', 'zaprite-for-woocommerce'),
'description' => sprintf(
__("Enter the Zaprite API Key from your <a href='%s' target='_blank' rel='noopener noreferrer'>Woo store connection settings</a>.", "zaprite-for-woocommerce"),
__("Enter the Zaprite API Key from your <a href='%s' target='_blank' rel='noopener noreferrer'>Woo store connection settings</a>.", "zaprite-for-woocommerce"),
htmlentities(ZAPRITE_APP_URL . '/org/default/connections')
),
'type' => 'text',
Expand Down Expand Up @@ -232,33 +227,6 @@ function set_order_status_pending( $order_id ) {
}
}

/**
* Checks payment on Thank you against the zaprite api
*/
public function check_payment()
{
try {
$order_id = wc_get_order_id_by_order_key($_REQUEST['key']);
$order = wc_get_order($order_id);
$r = $this->api->checkCharge($order_id);
if ($r['status'] == 200) {
if ($r['response']['paid'] == true) {
$order->update_status('processing', 'Order status updated via API.', true);
$order->add_order_note('Payment completed (checkout).');
$order->payment_complete();
$order->save();
error_log("ZAPRITE: check_payment paid true!!!");
}
} else {
// handle non 200 response status
die();
}
} catch(Exception $e) {
die(get_class($e) . ': ' . $e->getMessage());
}

}

}

/**
Expand Down Expand Up @@ -307,7 +275,6 @@ function zaprite_server_add_update_status_callback($data)

switch ($wooStatus) {
case 'processing':
$order->add_order_note('Payment is settled.');
// check if fiat premium was applied, if so, save to custom data in woo
$paidPremium = $orderStatusRes['response']['paidPremium'];
$paidPremiumCurrency = $orderStatusRes['response']['currency'];
Expand Down Expand Up @@ -339,8 +306,12 @@ function zaprite_server_add_update_status_callback($data)
$order->add_meta_data('zaprite_fiat_premium_extra_paid_amount', $paidPremiumAmountMajorUnits, true);
$order->save();
}
$order->payment_complete();
$order->save();
if ( !$order->has_status( 'completed' ) ) {
$order->update_status('processing', 'Order status updated via API.', true);
$order->add_order_note('Payment is settled.');
$order->payment_complete();
$order->save();
}
error_log("PAID");
echo(json_encode(array(
'result' => 'success',
Expand Down

0 comments on commit 94ab079

Please sign in to comment.