Skip to content

Commit 193a73d

Browse files
committed
update plugin to use recent paystack api changes
1 parent 259a324 commit 193a73d

1 file changed

Lines changed: 44 additions & 31 deletions

File tree

paystack-woocommerce-payment-gateway.php

Lines changed: 44 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
/*
33
Plugin Name: Paystack WooCommerce Payment Gateway
4-
Plugin URI: https://paystack.con
4+
Plugin URI: https://paystack.com
55
Description: WooCommerce payment gateway for Paystack
66
Version: 1.0.0
77
Author: Tunbosun Ayinla
@@ -37,15 +37,19 @@ public function __construct() {
3737
$this->init_settings();
3838

3939
// Get setting values
40-
$this->title = $this->get_option( 'title' );
41-
$this->description = $this->get_option( 'description' );
42-
$this->enabled = $this->get_option( 'enabled' );
43-
$this->testmode = $this->get_option( 'testmode' ) === 'yes' ? true : false;
44-
$this->merchant_id = $this->get_option( 'merchant_id' );
45-
$this->test_publishable_key = $this->get_option( 'test_publishable_key' );
46-
$this->live_publishable_key = $this->get_option( 'live_publishable_key' );
40+
$this->title = $this->get_option( 'title' );
41+
$this->description = $this->get_option( 'description' );
42+
$this->enabled = $this->get_option( 'enabled' );
43+
$this->testmode = $this->get_option( 'testmode' ) === 'yes' ? true : false;
4744

48-
$this->publishable_key = $this->testmode ? $this->test_publishable_key : $this->live_publishable_key;
45+
$this->test_public_key = $this->get_option( 'test_public_key' );
46+
$this->test_secret_key = $this->get_option( 'test_secret_key' );
47+
48+
$this->live_public_key = $this->get_option( 'live_public_key' );
49+
$this->live_secret_key = $this->get_option( 'live_secret_key' );
50+
51+
$this->public_key = $this->testmode ? $this->test_public_key : $this->live_public_key;
52+
$this->secret_key = $this->testmode ? $this->test_secret_key : $this->live_secret_key;
4953

5054
// Hooks
5155
add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
@@ -79,7 +83,7 @@ public function admin_notices() {
7983
}
8084

8185
// Check required fields
82-
if ( ! ( $this->merchant_id && $this->publishable_key ) ) {
86+
if ( ! ( $this->public_key && $this->secret_key ) ) {
8387
echo '<div class="error"><p>' . sprintf( 'Please enter your Paystack merchant details <a href="%s">here</a> to be able to use the Paystack WooCommerce plugin.', admin_url( 'admin.php?page=wc-settings&tab=checkout&section=wc_gateway_paystack' ) ) . '</p></div>';
8488
return;
8589
}
@@ -94,7 +98,7 @@ public function is_available() {
9498

9599
if ( $this->enabled == "yes" ) {
96100

97-
if ( ! ( $this->merchant_id && $this->publishable_key ) ) {
101+
if ( ! ( $this->public_key && $this->secret_key ) ) {
98102
return false;
99103
}
100104
return true;
@@ -149,13 +153,25 @@ public function init_form_fields() {
149153
'description' => 'Test Mode Option',
150154
'default' => 'yes'
151155
),
152-
'test_publishable_key' => array(
156+
'test_secret_key' => array(
157+
'title' => 'Test Secret Key',
158+
'type' => 'text',
159+
'description' => '',
160+
'default' => ''
161+
),
162+
'test_public_key' => array(
153163
'title' => 'Test Public Key',
154164
'type' => 'text',
155165
'description' => '',
156166
'default' => ''
157167
),
158-
'live_publishable_key' => array(
168+
'live_secret_key' => array(
169+
'title' => 'Live Secret Key',
170+
'type' => 'text',
171+
'description' => '',
172+
'default' => ''
173+
),
174+
'live_public_key' => array(
159175
'title' => 'Live Public Key',
160176
'type' => 'text',
161177
'description' => '',
@@ -175,12 +191,12 @@ public function payment_scripts() {
175191
return;
176192
}
177193

178-
wp_enqueue_script( 'paystack', 'https://paystack.ng/js/inline.js', array( 'jquery' ), '1.0.0', true );
194+
wp_enqueue_script( 'paystack', 'https://js.paystack.co/v1/inline.js', array( 'jquery' ), '1.0.0', true );
179195

180196
wp_enqueue_script( 'wc_paystack', plugins_url( 'assets/js/paystack.js', __FILE__ ), array('paystack'), '1.0.0', true );
181197

182198
$paystack_params = array(
183-
'key' => $this->publishable_key
199+
'key' => $this->public_key
184200
);
185201

186202
if ( is_checkout_pay_page() && get_query_var( 'order-pay' ) ) {
@@ -192,7 +208,7 @@ public function payment_scripts() {
192208
$email = $order->billing_email;
193209
$amount = $order->order_total * 100;
194210

195-
$txnref = $order_id . '_' . time() . $this->merchant_id;
211+
$txnref = $order_id . '|' . time();
196212

197213
if ( $order->id == $order_id && $order->order_key == $order_key ) {
198214
$paystack_params['email'] = $email;
@@ -250,38 +266,36 @@ public function verify_paystack_transaction() {
250266

251267
if( isset( $_REQUEST['paystack_txnref'] ) ){
252268

253-
$paystack_url = 'https://paystack.ng/charge/verification';
269+
$paystack_url = 'https://api.paystack.co/transaction/verify/' . $_REQUEST['paystack_txnref'];
254270

255-
$body = array(
256-
'merchantid' => $this->merchant_id,
257-
'trxref' => $_REQUEST['paystack_txnref']
271+
$headers = array(
272+
'Authorization' => 'Bearer ' . $this->secret_key
258273
);
259274

260275
$args = array(
261-
'body' => $body,
276+
'headers' => $headers,
262277
'timeout' => 60
263278
);
264279

265-
$request = wp_remote_post( $paystack_url, $args );
280+
$request = wp_remote_get( $paystack_url, $args );
266281

267282
if( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
268283

269284
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
270285

271-
if ( '1' == $paystack_response->transaction->status ) {
286+
if ( 'success' == $paystack_response->data->status ) {
272287

273-
$order_details = explode( '_', $_REQUEST['paystack_txnref'] );
274-
$order_id = $order_details[0];
288+
$order_details = explode( '|', $paystack_response->data->reference );
275289

276-
$order_id = (int) $order_id;
290+
$order_id = (int) $order_details[0];
277291

278292
$order = wc_get_order($order_id);
279293

280294
$order_total = $order->get_total();
281295

282-
$amount_paid = $paystack_response->transaction->amount / 100;
296+
$amount_paid = $paystack_response->data->amount / 100;
283297

284-
$paystack_ref = $paystack_response->transaction->paystack_reference;
298+
$paystack_ref = $paystack_response->data->reference;
285299

286300
// check if the amount paid is equal to the order amount.
287301
if( $order_total != $amount_paid ) {
@@ -317,10 +331,9 @@ public function verify_paystack_transaction() {
317331
}
318332
else {
319333

320-
$order_details = explode('_', $_REQUEST['paystack_txnref'] );
321-
$order_id = $order_details[0];
334+
$order_details = explode( '|', $_REQUEST['paystack_txnref'] );
322335

323-
$order_id = (int) $order_id;
336+
$order_id = (int) $order_details[0];
324337

325338
$order = wc_get_order( $order_id );
326339

0 commit comments

Comments
 (0)