Skip to content

Commit de2543e

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

3 files changed

Lines changed: 48 additions & 32 deletions

File tree

assets/images/cards.png

-6 Bytes
Loading

assets/js/paystack.min.js

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

paystack-woocommerce-payment-gateway.php

Lines changed: 47 additions & 32 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,14 @@ 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+
$suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
179195

180-
wp_enqueue_script( 'wc_paystack', plugins_url( 'assets/js/paystack.js', __FILE__ ), array('paystack'), '1.0.0', true );
196+
wp_enqueue_script( 'paystack', 'https://js.paystack.co/v1/inline.js', array( 'jquery' ), '1.0.0', true );
197+
198+
wp_enqueue_script( 'wc_paystack', plugins_url( 'assets/js/paystack'. $suffix . '.js', __FILE__ ), array('paystack'), '1.0.0', true );
181199

182200
$paystack_params = array(
183-
'key' => $this->publishable_key
201+
'key' => $this->public_key
184202
);
185203

186204
if ( is_checkout_pay_page() && get_query_var( 'order-pay' ) ) {
@@ -192,7 +210,7 @@ public function payment_scripts() {
192210
$email = $order->billing_email;
193211
$amount = $order->order_total * 100;
194212

195-
$txnref = $order_id . '_' . time() . $this->merchant_id;
213+
$txnref = $order_id . '_' .time();
196214

197215
if ( $order->id == $order_id && $order->order_key == $order_key ) {
198216
$paystack_params['email'] = $email;
@@ -250,38 +268,36 @@ public function verify_paystack_transaction() {
250268

251269
if( isset( $_REQUEST['paystack_txnref'] ) ){
252270

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

255-
$body = array(
256-
'merchantid' => $this->merchant_id,
257-
'trxref' => $_REQUEST['paystack_txnref']
273+
$headers = array(
274+
'Authorization' => 'Bearer ' . $this->secret_key
258275
);
259276

260277
$args = array(
261-
'body' => $body,
278+
'headers' => $headers,
262279
'timeout' => 60
263280
);
264281

265-
$request = wp_remote_post( $paystack_url, $args );
282+
$request = wp_remote_get( $paystack_url, $args );
266283

267284
if( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
268285

269286
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
270287

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

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

276-
$order_id = (int) $order_id;
292+
$order_id = (int) $order_details[0];
277293

278294
$order = wc_get_order($order_id);
279295

280296
$order_total = $order->get_total();
281297

282-
$amount_paid = $paystack_response->transaction->amount / 100;
298+
$amount_paid = $paystack_response->data->amount / 100;
283299

284-
$paystack_ref = $paystack_response->transaction->paystack_reference;
300+
$paystack_ref = $paystack_response->data->reference;
285301

286302
// check if the amount paid is equal to the order amount.
287303
if( $order_total != $amount_paid ) {
@@ -317,10 +333,9 @@ public function verify_paystack_transaction() {
317333
}
318334
else {
319335

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

323-
$order_id = (int) $order_id;
338+
$order_id = (int) $order_details[0];
324339

325340
$order = wc_get_order( $order_id );
326341

0 commit comments

Comments
 (0)