Skip to content

Commit 3bbeac7

Browse files
committed
Version 2.1.0
1 parent cb87d4e commit 3bbeac7

5 files changed

Lines changed: 200 additions & 14 deletions

File tree

README.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@
55

66
**Tags:** paystack, woocommerce, payment gateway, interswitch, tubiz plugins, verve, nigeria, mastercard, visa
77

8-
**Requires at least:** 4.3
8+
**Requires at least:** 4.4
99

10-
**Tested up to:** 4.5
10+
**Tested up to:** 4.6
1111

12-
**Stable tag:** 2.0.1
12+
**Stable tag:** 2.1.0
1313

1414
**License:** GPLv2 or later
1515

@@ -107,6 +107,9 @@ To configure the plugin, go to __WooCommerce > Settings__ from the left hand me
107107

108108
## Changelog ##
109109

110+
### 2.1.0 ###
111+
* New: Add support for confirming payment using the webhook url
112+
110113
### 2.0.1 ###
111114
* Fix: Paystack payment option and settings not available if Paystack WooCommerce Payment Gateway version 2.0.0 is installed and WooCommerce version 2.5.5 and below is installed
112115

@@ -126,8 +129,8 @@ To configure the plugin, go to __WooCommerce > Settings__ from the left hand me
126129

127130
## Upgrade Notice ##
128131

129-
### 2.0.1 ###
130-
* Important update. Fix Paystack payment option and settings not available if Paystack WooCommerce Payment Gateway version 2.0.0 is installed and WooCommerce version 2.5.5 and below is installed
132+
### 2.1.0 ###
133+
* Add support for confirming payment using the webhook url. This will fix issues with cancelled transactions after successful payment by customers.
131134

132135

133136

includes/class-paystack-deprecated.php

Lines changed: 90 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ public function __construct() {
4646
// Payment listener/API hook
4747
add_action( 'woocommerce_api_tbz_wc_paystack_gateway', array( $this, 'verify_paystack_transaction' ) );
4848

49+
add_action( 'woocommerce_api_tbz_wc_paystack_webhook', array( $this, 'process_webhooks' ) );
50+
4951
// Check if the gateway can be used
5052
if ( ! $this->is_valid_for_use() ) {
5153
$this->enabled = false;
@@ -124,7 +126,13 @@ public function is_available() {
124126
*/
125127
public function admin_options() {
126128

127-
echo '<h3>Paystack</h3>';
129+
?>
130+
131+
<h3>Paystack</h3>
132+
133+
<h4>If you are having issues with cancelled transactions after successful payment by customers, kindly set the url below as the Webhook URL in your <a href="https://dashboard.paystack.co/#/settings/developer">Paystack account</a> <strong style="color: red"><pre><code><?php echo WC()->api_request_url( 'Tbz_WC_Paystack_Webhook' ); ?></code></pre></strong></h4>
134+
135+
<?php
128136

129137
if ( $this->is_valid_for_use() ){
130138

@@ -307,6 +315,11 @@ public function verify_paystack_transaction() {
307315

308316
$order = wc_get_order($order_id);
309317

318+
if( in_array( $order->get_status(), array( 'processing', 'completed', 'on-hold' ) ) ) {
319+
wp_redirect( $this->get_return_url( $order ) );
320+
exit;
321+
}
322+
310323
$order_total = $order->get_total();
311324

312325
$amount_paid = $paystack_response->data->amount / 100;
@@ -369,4 +382,80 @@ public function verify_paystack_transaction() {
369382

370383
}
371384

385+
386+
/**
387+
* Process Webhook
388+
*/
389+
public function process_webhooks() {
390+
391+
header( 'HTTP/1.1 200 OK' );
392+
393+
if ( ( strtoupper( $_SERVER['REQUEST_METHOD'] ) != 'POST' ) || ! array_key_exists( 'HTTP_X_PAYSTACK_SIGNATURE', $_SERVER ) ) {
394+
exit;
395+
}
396+
397+
$json = file_get_contents( "php://input" );
398+
399+
$event = json_decode( $json );
400+
401+
if( 'charge.success' == $event->event ) {
402+
403+
$order_details = explode( '_', $event->data->reference );
404+
405+
$order_id = (int) $order_details[0];
406+
407+
$order = wc_get_order($order_id);
408+
409+
$paystack_txn_ref = get_post_meta( $order_id, '_paystack_txn_ref', true );
410+
411+
if( $event->data->reference != $paystack_txn_ref ) {
412+
exit;
413+
}
414+
415+
if( in_array( $order->get_status(), array( 'processing', 'completed', 'on-hold' ) ) ) {
416+
wp_redirect( $this->get_return_url( $order ) );
417+
exit;
418+
}
419+
420+
$order_total = $order->get_total();
421+
422+
$amount_paid = $event->data->amount / 100;
423+
424+
$paystack_ref = $event->data->reference;
425+
426+
// check if the amount paid is equal to the order amount.
427+
if( $order_total != $amount_paid ) {
428+
429+
$order->update_status( 'on-hold', '' );
430+
431+
add_post_meta( $order_id, '_transaction_id', $paystack_ref, true );
432+
433+
$notice = 'Thank you for shopping with us.<br />Your payment transaction was successful, but the amount paid is not the same as the total order amount.<br />Your order is currently on-hold.<br />Kindly contact us for more information regarding your order and payment status.';
434+
$notice_type = 'notice';
435+
436+
// Add Customer Order Note
437+
$order->add_order_note( $notice, 1 );
438+
439+
// Add Admin Order Note
440+
$order->add_order_note('<strong>Look into this order</strong><br />This order is currently on hold.<br />Reason: Amount paid is less than the total order amount.<br />Amount Paid was <strong>&#8358;'.$amount_paid.'</strong> while the total order amount is <strong>&#8358;'.$order_total.'</strong><br />Paystack Transaction Reference: '.$paystack_ref );
441+
442+
$order->reduce_order_stock();
443+
444+
wc_add_notice( $notice, $notice_type );
445+
446+
wc_empty_cart();
447+
}
448+
else{
449+
450+
$order->payment_complete( $paystack_ref );
451+
452+
$order->add_order_note( sprintf( 'PayStack Transaction Ref: %s', $paystack_ref ) );
453+
454+
wc_empty_cart();
455+
}
456+
457+
wp_redirect( $this->get_return_url( $order ) );
458+
}
459+
}
460+
372461
}

includes/class-paystack.php

Lines changed: 93 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ public function __construct() {
5353
// Payment listener/API hook
5454
add_action( 'woocommerce_api_tbz_wc_paystack_gateway', array( $this, 'verify_paystack_transaction' ) );
5555

56+
add_action( 'woocommerce_api_tbz_wc_paystack_webhook', array( $this, 'process_webhooks' ) );
57+
5658
// Check if the gateway can be used
5759
if ( ! $this->is_valid_for_use() ) {
5860
$this->enabled = false;
@@ -131,7 +133,13 @@ public function is_available() {
131133
*/
132134
public function admin_options() {
133135

134-
echo '<h3>Paystack</h3>';
136+
?>
137+
138+
<h3>Paystack</h3>
139+
140+
<h4>If you are having issues with cancelled transactions after successful payment by customers, kindly set the url below as the Webhook URL in your <a href="https://dashboard.paystack.co/#/settings/developer">Paystack account</a> <strong style="color: red"><pre><code><?php echo WC()->api_request_url( 'Tbz_WC_Paystack_Webhook' ); ?></code></pre></strong></h4>
141+
142+
<?php
135143

136144
if ( $this->is_valid_for_use() ){
137145

@@ -473,7 +481,12 @@ public function verify_paystack_transaction() {
473481

474482
$order_id = (int) $order_details[0];
475483

476-
$order = wc_get_order($order_id);
484+
$order = wc_get_order( $order_id );
485+
486+
if( in_array( $order->get_status(), array( 'processing', 'completed', 'on-hold' ) ) ) {
487+
wp_redirect( $this->get_return_url( $order ) );
488+
exit;
489+
}
477490

478491
$order_total = $order->get_total();
479492

@@ -540,6 +553,84 @@ public function verify_paystack_transaction() {
540553
}
541554

542555

556+
/**
557+
* Process Webhook
558+
*/
559+
public function process_webhooks() {
560+
561+
header( 'HTTP/1.1 200 OK' );
562+
563+
if ( ( strtoupper( $_SERVER['REQUEST_METHOD'] ) != 'POST' ) || ! array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
564+
exit;
565+
}
566+
567+
$json = file_get_contents( "php://input" );
568+
569+
$event = json_decode( $json );
570+
571+
if( 'charge.success' == $event->event ) {
572+
573+
$order_details = explode( '_', $event->data->reference );
574+
575+
$order_id = (int) $order_details[0];
576+
577+
$order = wc_get_order($order_id);
578+
579+
$paystack_txn_ref = get_post_meta( $order_id, '_paystack_txn_ref', true );
580+
581+
if( $event->data->reference != $paystack_txn_ref ) {
582+
exit;
583+
}
584+
585+
if( in_array( $order->get_status(), array( 'processing', 'completed', 'on-hold' ) ) ) {
586+
wp_redirect( $this->get_return_url( $order ) );
587+
exit;
588+
}
589+
590+
$order_total = $order->get_total();
591+
592+
$amount_paid = $event->data->amount / 100;
593+
594+
$paystack_ref = $event->data->reference;
595+
596+
// check if the amount paid is equal to the order amount.
597+
if( $order_total != $amount_paid ) {
598+
599+
$order->update_status( 'on-hold', '' );
600+
601+
add_post_meta( $order_id, '_transaction_id', $paystack_ref, true );
602+
603+
$notice = 'Thank you for shopping with us.<br />Your payment transaction was successful, but the amount paid is not the same as the total order amount.<br />Your order is currently on-hold.<br />Kindly contact us for more information regarding your order and payment status.';
604+
$notice_type = 'notice';
605+
606+
// Add Customer Order Note
607+
$order->add_order_note( $notice, 1 );
608+
609+
// Add Admin Order Note
610+
$order->add_order_note('<strong>Look into this order</strong><br />This order is currently on hold.<br />Reason: Amount paid is less than the total order amount.<br />Amount Paid was <strong>&#8358;'.$amount_paid.'</strong> while the total order amount is <strong>&#8358;'.$order_total.'</strong><br />Paystack Transaction Reference: '.$paystack_ref );
611+
612+
$order->reduce_order_stock();
613+
614+
wc_add_notice( $notice, $notice_type );
615+
616+
wc_empty_cart();
617+
}
618+
else{
619+
620+
$order->payment_complete( $paystack_ref );
621+
622+
$order->add_order_note( sprintf( 'PayStack Transaction Ref: %s', $paystack_ref ) );
623+
624+
wc_empty_cart();
625+
}
626+
627+
$this->save_card_details( $event, $order->get_user_id(), $order_id );
628+
629+
wp_redirect( $this->get_return_url( $order ) );
630+
}
631+
}
632+
633+
543634
/**
544635
* Save Customer Card Details
545636
*/

readme.txt

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
Contributors: tubiz
33
Donate link: http://bosun.me/donate
44
Tags: paystack, woocommerce, payment gateway, interswitch, tubiz plugins, verve, nigeria, mastercard, visa
5-
Requires at least: 4.3
6-
Tested up to: 4.5
7-
Stable tag: 2.0.1
5+
Requires at least: 4.4
6+
Tested up to: 4.6
7+
Stable tag: 2.1.0
88
License: GPLv2 or later
99
License URI: http://www.gnu.org/licenses/gpl-2.0.html
1010

@@ -99,6 +99,9 @@ To configure the plugin, go to __WooCommerce > Settings__ from the left hand me
9999

100100
== Changelog ==
101101

102+
= 2.1.0 =
103+
* New: Add support for confirming payment using the webhook url
104+
102105
= 2.0.1 =
103106
* Fix: Paystack payment option and settings not available if Paystack WooCommerce Payment Gateway version 2.0.0 is installed and WooCommerce version 2.5.5 and below is installed
104107

@@ -118,8 +121,8 @@ To configure the plugin, go to __WooCommerce > Settings__ from the left hand me
118121

119122
== Upgrade Notice ==
120123

121-
= 2.0.1 =
122-
* Important update. Fix Paystack payment option and settings not available if Paystack WooCommerce Payment Gateway version 2.0.0 is installed and WooCommerce version 2.5.5 and below is installed
124+
= 2.1.0 =
125+
* Add support for confirming payment using the webhook url. This will fix issues with cancelled transactions after successful payment by customers.
123126

124127

125128
== Screenshots ==

woo-paystack.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Plugin Name: Paystack WooCommerce Payment Gateway
44
Plugin URI: https://paystack.com
55
Description: WooCommerce payment gateway for Paystack
6-
Version: 2.0.1
6+
Version: 2.1.0
77
Author: Tunbosun Ayinla
88
Author URI: http://bosun.me
99
License: GPL-2.0+

0 commit comments

Comments
 (0)