PluginProbe
Paystack WooCommerce Payment Gateway / 1.0.0
Paystack WooCommerce Payment Gateway v1.0.0
5.8.5 5.8.4 trunk 1.0.0 1.1.0 2.0.0 2.0.1 2.1.0 3.0.0 3.1.0 3.1.1 4.0.0 4.0.1 4.1.0 5.0.0 5.0.1 5.0.2 5.1.0 5.2.0 5.2.1 5.3.0 5.3.1 5.4.0 5.4.1 5.4.2 All 42 releases
woo-paystack / woo-paystack.php

woo-paystack.php in Paystack WooCommerce Payment Gateway 1.0.0, at woo-paystack.php

389 lines 10.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /*
3 Plugin Name: Paystack WooCommerce Payment Gateway
4 Plugin URI: https://paystack.com
5 Description: WooCommerce payment gateway for Paystack
6 Version: 1.0.0
7 Author: Tunbosun Ayinla
8 Author URI: http://bosun.me
9 License: GPL-2.0+
10 License URI: http://www.gnu.org/licenses/gpl-2.0.txt
11 */
12
13 if ( ! defined( 'ABSPATH' ) ) {
14 exit;
15 }
16
17
18 add_action( 'plugins_loaded', 'tbz_wc_paystack_init', 0 );
19
20 function tbz_wc_paystack_init() {
21
22 class Tbz_WC_Paystack_Gateway extends WC_Payment_Gateway {
23
24 /**
25 * Constructor
26 */
27 public function __construct() {
28 $this->id = 'paystack';
29 $this->method_title = 'Paystack';
30 $this->method_description = 'Make payment using your debit and credit cards';
31 $this->has_fields = true;
32
33 // Load the form fields
34 $this->init_form_fields();
35
36 // Load the settings.
37 $this->init_settings();
38
39 // Get setting values
40 $this->title = 'Debit/Credit Cards';
41 $this->enabled = $this->get_option( 'enabled' );
42 $this->testmode = $this->get_option( 'testmode' ) === 'yes' ? true : false;
43
44 $this->test_public_key = $this->get_option( 'test_public_key' );
45 $this->test_secret_key = $this->get_option( 'test_secret_key' );
46
47 $this->live_public_key = $this->get_option( 'live_public_key' );
48 $this->live_secret_key = $this->get_option( 'live_secret_key' );
49
50 $this->public_key = $this->testmode ? $this->test_public_key : $this->live_public_key;
51 $this->secret_key = $this->testmode ? $this->test_secret_key : $this->live_secret_key;
52
53 // Hooks
54 add_action( 'wp_enqueue_scripts', array( $this, 'payment_scripts' ) );
55 add_action( 'admin_notices', array( $this, 'admin_notices' ) );
56 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
57
58 add_action( 'woocommerce_receipt_' . $this->id, array( $this, 'receipt_page' ) );
59
60 // Payment listener/API hook
61 add_action( 'woocommerce_api_tbz_wc_paystack_gateway', array( $this, 'verify_paystack_transaction' ) );
62
63 // Check if the gateway can be used
64 if ( ! $this->is_valid_for_use() ) {
65 $this->enabled = false;
66 }
67 }
68
69
70 /**
71 * Check if this gateway is enabled and available in the user's country.
72 */
73 public function is_valid_for_use() {
74
75 if( ! in_array( get_woocommerce_currency(), apply_filters( 'woocommerce_paystack_supported_currencies', array( 'NGN' ) ) ) ) {
76
77 $this->msg = 'Paystack does not support your store currency.';
78
79 return false;
80
81 }
82
83 return true;
84 }
85
86 /**
87 * Display paystack payment icon
88 */
89 public function get_icon() {
90
91 $icon = '<img src="' . WC_HTTPS::force_https_url( plugins_url( 'assets/images/paystack-woocommerce.png' , __FILE__ ) ) . '" alt="cards" />';
92
93 return apply_filters( 'woocommerce_gateway_icon', $icon, $this->id );
94 }
95
96
97 /**
98 * Check if paystack merchant details is filled
99 */
100 public function admin_notices() {
101
102 if ( $this->enabled == 'no' ) {
103 return;
104 }
105
106 // Check required fields
107 if ( ! ( $this->public_key && $this->secret_key ) ) {
108 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>';
109 return;
110 }
111
112 }
113
114
115 /**
116 * Check if this gateway is enabled
117 */
118 public function is_available() {
119
120 if ( $this->enabled == "yes" ) {
121
122 if ( ! ( $this->public_key && $this->secret_key ) ) {
123 return false;
124 }
125 return true;
126 }
127
128 return false;
129 }
130
131
132 /**
133 * Admin Panel Options
134 */
135 public function admin_options() {
136
137 echo '<h3>Paystack</h3>';
138
139 if ( $this->is_valid_for_use() ){
140
141 echo '<table class="form-table">';
142 $this->generate_settings_html();
143 echo '</table>';
144
145 }
146 else{ ?>
147 <div class="inline error"><p><strong>Paystack Payment Gateway Disabled</strong>: <?php echo $this->msg ?></p></div>
148
149 <?php }
150 }
151
152
153 /**
154 * Initialise Gateway Settings Form Fields
155 */
156 public function init_form_fields() {
157
158 $this->form_fields = array(
159 'enabled' => array(
160 'title' => 'Enable/Disable',
161 'label' => 'Enable Paystack',
162 'type' => 'checkbox',
163 'description' => '',
164 'default' => 'no'
165 ),
166 'testmode' => array(
167 'title' => 'Test mode',
168 'label' => 'Enable Test Mode',
169 'type' => 'checkbox',
170 'description' => 'Test Mode Option',
171 'default' => 'yes'
172 ),
173 'test_secret_key' => array(
174 'title' => 'Test Secret Key',
175 'type' => 'text',
176 'description' => 'Enter your Test Secret Key here',
177 'default' => ''
178 ),
179 'test_public_key' => array(
180 'title' => 'Test Public Key',
181 'type' => 'text',
182 'description' => 'Enter your Test Public Key here.',
183 'default' => ''
184 ),
185 'live_secret_key' => array(
186 'title' => 'Live Secret Key',
187 'type' => 'text',
188 'description' => 'Enter your Live Secret Key here.',
189 'default' => ''
190 ),
191 'live_public_key' => array(
192 'title' => 'Live Public Key',
193 'type' => 'text',
194 'description' => 'Enter your Live Public Key here.',
195 'default' => ''
196 ),
197 );
198
199 }
200
201
202 /**
203 * Outputs scripts used for paystack payment
204 */
205 public function payment_scripts() {
206
207 if ( is_checkout() ) {
208 wp_enqueue_style( 'paystack', plugins_url( 'assets/css/paystack.css', __FILE__ ) );
209 }
210
211 if ( ! is_checkout_pay_page() ) {
212 return;
213 }
214
215 $suffix = ( defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ) ? '' : '.min';
216
217 wp_enqueue_script( 'paystack', 'https://js.paystack.co/v1/inline.js', array( 'jquery' ), '1.0.0', true );
218
219 wp_enqueue_script( 'wc_paystack', plugins_url( 'assets/js/paystack'. $suffix . '.js', __FILE__ ), array('paystack'), '1.0.0', true );
220
221 $paystack_params = array(
222 'key' => $this->public_key
223 );
224
225 if ( is_checkout_pay_page() && get_query_var( 'order-pay' ) ) {
226
227 $order_key = urldecode( $_GET['key'] );
228 $order_id = absint( get_query_var( 'order-pay' ) );
229
230 $order = wc_get_order( $order_id );
231 $email = $order->billing_email;
232 $amount = $order->order_total * 100;
233
234 $txnref = $order_id . '_' .time();
235
236 if ( $order->id == $order_id && $order->order_key == $order_key ) {
237 $paystack_params['email'] = $email;
238 $paystack_params['amount'] = $amount;
239 $paystack_params['txnref'] = $txnref;
240 }
241
242 update_post_meta( $order_id, '_paystack_txn_ref', $txnref );
243
244 }
245
246 wp_localize_script( 'wc_paystack', 'wc_paystack_params', $paystack_params );
247
248 }
249
250
251 /**
252 * Process the payment
253 */
254 public function process_payment( $order_id ) {
255
256 $order = wc_get_order( $order_id );
257
258 return array(
259 'result' => 'success',
260 'redirect' => $order->get_checkout_payment_url( true )
261 );
262
263 }
264
265
266 /**
267 * Displays the payment page
268 */
269 public function receipt_page( $order_id ) {
270
271 $order = wc_get_order( $order_id );
272
273 echo '<p>Thank you for your order, please click the button below to pay with debit/credit card using Paystack.</p>';
274
275 echo '<div id="paystack_form"><form id="order_review" method="post" action="'. WC()->api_request_url( 'Tbz_WC_Paystack_Gateway' ) .'"></form><button class="button alt" id="paystack-payment-button">Pay Now</button> <a class="button cancel" href="' . esc_url( $order->get_cancel_order_url() ) . '">Cancel order &amp; restore cart</a></div>
276 ';
277 }
278
279
280 /**
281 * Verify Paystack payment
282 */
283 public function verify_paystack_transaction() {
284
285 @ob_clean();
286
287 header( 'HTTP/1.1 200 OK' );
288
289 if( isset( $_REQUEST['paystack_txnref'] ) ){
290
291 $paystack_url = 'https://api.paystack.co/transaction/verify/' . $_REQUEST['paystack_txnref'];
292
293 $headers = array(
294 'Authorization' => 'Bearer ' . $this->secret_key
295 );
296
297 $args = array(
298 'headers' => $headers,
299 'timeout' => 60
300 );
301
302 $request = wp_remote_get( $paystack_url, $args );
303
304 if( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
305
306 $paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
307
308 if ( 'success' == $paystack_response->data->status ) {
309
310 $order_details = explode( '_', $paystack_response->data->reference );
311
312 $order_id = (int) $order_details[0];
313
314 $order = wc_get_order($order_id);
315
316 $order_total = $order->get_total();
317
318 $amount_paid = $paystack_response->data->amount / 100;
319
320 $paystack_ref = $paystack_response->data->reference;
321
322 // check if the amount paid is equal to the order amount.
323 if( $order_total != $amount_paid ) {
324
325 $order->update_status( 'on-hold', '' );
326
327 add_post_meta( $order_id, '_transaction_id', $paystack_ref, true );
328
329 $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.';
330 $notice_type = 'notice';
331
332 // Add Customer Order Note
333 $order->add_order_note( $notice, 1 );
334
335 // Add Admin Order Note
336 $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 );
337
338 $order->reduce_order_stock();
339
340 wc_add_notice( $notice, $notice_type );
341
342 wc_empty_cart();
343 }
344 else{
345
346 $order->payment_complete( $paystack_ref );
347
348 $order->add_order_note( sprintf( 'PayStack Transaction Ref: %s', $paystack_ref ) );
349
350 wc_empty_cart();
351 }
352
353 }
354 else {
355
356 $order_details = explode( '_', $_REQUEST['paystack_txnref'] );
357
358 $order_id = (int) $order_details[0];
359
360 $order = wc_get_order( $order_id );
361
362 $order->update_status( 'failed', 'Payment was declined by Paystack.' );
363 }
364
365 }
366
367 wp_redirect( $this->get_return_url( $order ) );
368
369 exit;
370 }
371
372 wp_redirect( wc_get_page_permalink( 'cart' ) );
373
374 exit;
375
376 }
377
378 }
379
380 /**
381 * Add Paystack Gateway to WC
382 **/
383 function tbz_wc_add_paystack_gateway( $methods ) {
384 $methods[] = 'Tbz_WC_Paystack_Gateway';
385 return $methods;
386 }
387 add_filter( 'woocommerce_payment_gateways', 'tbz_wc_add_paystack_gateway' );
388
389 }