-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathae_paystack.php
More file actions
421 lines (354 loc) · 12.3 KB
/
ae_paystack.php
File metadata and controls
421 lines (354 loc) · 12.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
<?php
/*
Plugin Name: Paystack Gateway for Enginethemes.com DirectoryEngine, FreelanceEngine site
Plugin URI: http://paystack.com/
Description: Integrates the Paystack payment gateway to DirectoryEngine, FreelanceEngine site
Version: 1.0
Author: kendysond
Author URI: http://kendyson.com/
License: GPLv2
*/
add_filter('ae_admin_menu_pages','ae_paystack_add_settings', 10, 2 );
function ae_paystack_add_settings($pages){
$sections = array();
$options = AE_Options::get_instance();
/**
* ae fields settings
*/
$sections = array(
'args' => array(
'title' => __("Paystack API", ET_DOMAIN) ,
'id' => 'meta_field',
'icon' => 'F',
'class' => ''
) ,
'groups' => array(
array(
'args' => array(
'title' => __("Paystack Api Settings", ET_DOMAIN) ,
'id' => 'secret-key',
'class' => '',
'desc' => __('Get your api keys from your Paystack dashboard settings, under "Developer/Api" Tab.<br>
<!-- <h4>Optional: To avoid situations where bad network makes it impossible to verify transactions, set your webhook URL <a href="https://dashboard.paystack.co/#/settings/developer">here</a> to the URL below<strong style="color: red">
<pre><code>'. admin_url("admin-ajax.php") . "?action=ae_kkd_paystack_webhook".'</code></pre>
</strong></h4> -->', ET_DOMAIN),
'name' => 'paystack'
) ,
'fields' => array(
array(
'id' => 'mode',
// 'type' => 'radio',
'label' => __("Mode", ET_DOMAIN),
'title' => __("Mode", ET_DOMAIN),
'name' => 'mode',
'class' => '',
'type' => 'select',
'data' => array(
'disable' => __("Disable", ET_DOMAIN) ,
'test' => __("Test", ET_DOMAIN) ,
'live' => __("Live", ET_DOMAIN) ,
) ,
) ,
array(
'id' => 'tsk',
'type' => 'text',
'label' => __("Test Secret Key", ET_DOMAIN) ,
'name' => 'tsk',
'class' => ''
) ,
array(
'id' => 'tpk',
'type' => 'text',
'label' => __('Test Public Key', ET_DOMAIN),
'name' => 'tpk',
'class' => ''
),
array(
'id' => 'lsk',
'type' => 'text',
'label' => __("Live Secret Key", ET_DOMAIN) ,
'name' => 'lsk',
'class' => ''
) ,
array(
'id' => 'lpk',
'type' => 'text',
'label' => __('Live Public Key', ET_DOMAIN),
'name' => 'lpk',
'class' => ''
)
)
)
)
);
$temp = new AE_section($sections['args'], $sections['groups'], $options);
$paystack_setting = new AE_container(array(
'class' => 'field-settings',
'id' => 'settings',
) , $temp, $options);
$pages[] = array(
'args' => array(
'parent_slug' => 'et-overview',
'page_title' => __('Paystack', ET_DOMAIN) ,
'menu_title' => __('Paystack Api Key settings', ET_DOMAIN) ,
'cap' => 'administrator',
'slug' => 'ae-paystack',
'icon' => '$',
'desc' => __("Integrate the Paystack payment gateway to your site", ET_DOMAIN)
) ,
'container' => $paystack_setting
);
return $pages;
}
add_filter( 'ae_support_gateway', 'ae_paystack_add' );
function ae_paystack_add($gateways){
$gateways['paystack'] = 'Paystack';
return $gateways;
}
add_action('after_payment_list', 'ae_paystack_render_button');
function ae_paystack_render_button() {
$paystack = ae_get_option('paystack');
if($paystack['mode'] == 'disable')
return false;
?>
<li>
<span class="title-plan select-payment" data-type="paystack">
<?php _e("Paystack", ET_DOMAIN); ?>
</span>
<br>
<img src="<?php echo plugins_url( 'logos@2x.png' , __FILE__ ); ?>" alt="cardlogos" style="width: 200px !important;"/>
<a href="#" class="btn btn-submit-price-plan select-payment" data-type="paystack"><?php _e("Select", ET_DOMAIN); ?></a>
</li>
<?php
}
add_filter('ae_setup_payment', 'ae_paystack_setup_payment', 10, 4 );
function ae_paystack_setup_payment($response, $paymentType, $order) {
global $current_user,$user_email;
// kiem tra payment gateway
if ($paymentType == 'PAYSTACK') {
$paystack = ae_get_option('paystack');
$mode = $paystack['mode'];
if ($mode == 'test') {
$key = $paystack['tsk'];
}else{
$key = $paystack['lsk'];
}
//get info order
$order_pay = $order->generate_data_to_pay();
$orderId = $order_pay['product_id'];
$amount = $order_pay['total'];
$currency = $order_pay['currencyCodeType'];
$pakage_info = array_pop($order_pay['products']);
$pakage_name = $pakage_info['NAME'];
$paystack_info = ae_get_option('paystack');
$new_id = $order_pay['ID'];
$txnref = $new_id . '_' .time();
$return_url = et_get_page_link('process-payment', array(
'paymentType' => 'paystack',
'return' => "1"
)) ;
$koboamount = $amount*100;
$paystack_url = 'https://api.paystack.co/transaction/initialize';
$headers = array(
'Content-Type' => 'application/json',
'Authorization' => "Bearer ".$key
);
//Create Plan
$body = array(
'email' => $user_email,
'amount' => $koboamount,
'reference' => $txnref,
'callback_url' => $return_url
// 'metadata' => json_encode(array('custom_fields' => $meta )),
);
$args = array(
'body' => json_encode( $body ),
'headers' => $headers,
'timeout' => 60
);
$request = wp_remote_post( $paystack_url, $args );
if( ! is_wp_error( $request )) {
$paystack_response = json_decode(wp_remote_retrieve_body($request));
$url = $paystack_response->data->authorization_url;
$response = array(
'success' => true,
'data' => array(
'url' => $url,
'ACK' => true,
) ,
'paymentType' => 'PAYSTACK'
);
// exit;
}else{
$response = array(
'success' => false,
'data' => array(
'url' => site_url('post-place') ,
'ACK' => false
)
);
}
// $notify_url = et_get_page_link('process-payment', array(
// 'paymentType' => 'paystack',
// 'post_back' => "2",
// 'id_order' => $order_pay['ID']
// )) ;
// $data = array(
// "merchant_id" => $paystack_info['merchant_id'],
// "merchant_key" => $paystack_info['merchant_key'],
// "return_url" => $return_url,
// "cancel_url" => $return_url,
// "notify_url" => $notify_url,
// "m_payment_id" => $orderId,
// "amount" => $amount,
// "item_name" => $pakage_name,
// );
}
return $response;
}
add_filter('ae_process_payment', 'ae_paystack_process_payment', 10 ,2 );
function ae_paystack_process_payment($payment_return, $data) {
$paystack = ae_get_option('paystack');
$mode = $paystack['mode'];
if ($mode == 'test') {
$key = $paystack['tsk'];
}else{
$key = $paystack['tsk'];
}
$paymenttype = $data['payment_type'];
$order = $data['order'];
$order_pay = $order->generate_data_to_pay();
$main_order_id = $order_pay['ID'];
$main_amount = $order_pay['total'];
$main_kobo_amount = $main_amount*100;
// die();
if($paymenttype == 'paystack' && isset($_GET['reference'])){
$reference = $_GET['reference'];
$paystack_url = 'https://api.paystack.co/transaction/verify/' . $reference;
$headers = array(
'Authorization' => 'Bearer ' . $key
);
$args = array(
'headers' => $headers,
'timeout' => 60
);
$request = wp_remote_get( $paystack_url, $args );
if ( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
$paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
if ( 'success' == $paystack_response->data->status ) {
$order_details = explode( '_', $paystack_response->data->reference );
$order_id = (int) $order_details[0];
$amount_paid = $paystack_response->data->amount;
// check if the amount paid is equal to the order amount.
if ( $main_kobo_amount != $amount_paid ) {
$payment_return = array(
'ACK' => false,
'payment' => 'paystack',
'payment_status' => 'fail',
'msg' => 'Wrong amount paid'
);
} else {
$payment_return = array(
'ACK' => true,
'payment' => 'paystack',
'payment_status' => 'complete'
);
update_post_meta($order_id, 'paystack_status','COMPLETE');
}
// }
} else {
$payment_return = array(
'ACK' => false,
'payment' => 'paystack',
'payment_status' => 'fail',
'msg' => "Couldn't Verify Transaction"
);
}
}
}
return $payment_return;
}
// add_action( 'wp_ajax_ae_kkd_paystack_webhook', 'ae_kkd_paystack_webhook');
// add_action( 'wp_ajax_nopriv_ae_kkd_paystack_webhook', 'ae_kkd_paystack_webhook');
// function ae_kkd_paystack_webhook() {
// global $wpdb;
// // if ((strtoupper($_SERVER['REQUEST_METHOD']) != 'POST' ) || !array_key_exists('HTTP_X_PAYSTACK_SIGNATURE', $_SERVER) ) {
// // exit();
// // }
// $input = @file_get_contents("php://input");
// $event = json_decode($input);
// echo "<pre>";
// $paystack = ae_get_option('paystack');
// $mode = $paystack['mode'];
// if ($mode == 'test') {
// $key = $paystack['tsk'];
// }else{
// $key = $paystack['tsk'];
// }
// // update_post_meta(634, 'paystack_status','COMPLETE');
// ae_paystack_process_payment('','' ,634);
// die();
// // die();
// // die();
// // print_r($event);
// // // if(!$_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] || ($_SERVER['HTTP_X_PAYSTACK_SIGNATURE'] !== hash_hmac('sha512', $input, paystack_recurrent_billing_get_secret_key()))){
// // // exit();
// // // }
// switch($event->event){
// case 'subscription.create':
// break;
// case 'subscription.disable':
// break;
// case 'charge.success':
// $reference = $event->data->reference;
// $paystack_url = 'https://api.paystack.co/transaction/verify/' . $reference;
// $headers = array(
// 'Authorization' => 'Bearer ' . $key
// );
// $args = array(
// 'headers' => $headers,
// 'timeout' => 60
// );
// $request = wp_remote_get( $paystack_url, $args );
// if ( ! is_wp_error( $request ) && 200 == wp_remote_retrieve_response_code( $request ) ) {
// $paystack_response = json_decode( wp_remote_retrieve_body( $request ) );
// echo "string";
// if ( 'success' == $paystack_response->data->status ) {
// $order_details = explode( '_', $paystack_response->data->reference );
// $order_id = (int) $order_details[0];
// $amount_paid = $paystack_response->data->amount;
// // check if the amount paid is equal to the order amount.
// if ( $main_kobo_amount != $amount_paid ) {
// $payment_return = array(
// 'ACK' => false,
// 'payment' => 'paystack',
// 'payment_status' => 'fail',
// 'msg' => 'Wrong amount paid'
// );
// } else {
// $payment_return = array(
// 'ACK' => true,
// 'payment' => 'paystack',
// 'payment_status' => 'complete'
// );
// update_post_meta($order_id, 'paystack_status','COMPLETE');
// }
// // }
// } else {
// $payment_return = array(
// 'ACK' => false,
// 'payment' => 'paystack',
// 'payment_status' => 'fail',
// 'msg' => "Couldn't Verify Transaction"
// );
// }
// }
// print_r($payment_return);
// break;
// case 'invoice.create':
// case 'invoice.update':
// break;
// }
// // http_response_code(200);
// // exit();
// }