-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.php
434 lines (372 loc) · 15.6 KB
/
index.php
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
422
423
424
425
426
427
428
429
430
431
432
<?php
/*
Plugin Name: sampay Payment Gateway
Plugin URI: https://github.com/cynojine/sampay-woocommerce
Description: sampay Woocommerce Payment Gateway allows you to accept payment on your Woocommerce store via Visa Cards,MTN MOoMo.
Version: 1.0.0
Author: kazashim Kuzasuwat
Author URI: http://samafricaonline.com/
WC requires at least: 3.3
WC tested up to: 3.4
License: GPL-2.0+
License URI: http://www.gnu.org/licenses/gpl-2.0.txt
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
/*
* This action hook registers our PHP class as a WooCommerce payment gateway
*/
add_filter( 'woocommerce_payment_gateways', 'sampay_add_gateway_class' );
function sampay_add_gateway_class( $gateways ) {
$gateways[] = 'WC_sampay'; // your class name is here
return $gateways;
}
/*
* The class itself, please note that it is inside plugins_loaded action hook
*/
add_action( 'plugins_loaded', 'sampay_init_gateway_class' );
function sampay_init_gateway_class() {
class WC_sampay extends WC_Payment_Gateway {
/**
* Class constructor, more about it in Step 3
*/
public function __construct() {
$this->id = "sampay_gateway"; // global ID
$this->has_fields = false;
$this->method_title = "sampay Payment Gateway"; // Show Title
$this->method_description = 'sampay Payment Gateway allows you to receive Mastercard, MTN MoMo and Visa Card Payments via your Woocommerce Powered Site.';// Show Description
$this->icon = apply_filters('woocommerce_sampay_icon', plugins_url( 'assets/images/logo.png' , __FILE__ ) );// Icon link
$this->notify_url = WC()->api_request_url( 'WC_sampay' );
// Method with all the options fields
$this->init_form_fields();
// load variable setting
$this->init_settings();
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->appkey = $this->get_option('appkey');
$this->authkey = $this->get_option('authkey');
$this->$currency = $this->get_option('currency');
if ($this->get_option('sandbox') == 'yes') {
$this->posturl = 'https://samafricaonline.com/sam_pay/public/merchantcheckout';
$this->geturl = 'https://samafricaonline.com/sam_pay/public/paymentconfirmation';
} else {
$this->posturl = 'https://samafricaonline.com/sam_pay/public/merchantcheckout';
$this->geturl = 'https://samafricaonline.com/sam_pay/public/paymentconfirmation';
}
// This action hook saves the settings
add_action('woocommerce_receipt_' . $this->id, array( $this, 'receipt_page'), 10, 1);
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
// You can also register a webhook here
add_action( 'woocommerce_api_'.strtolower(get_class($this)) , array(&$this, 'get_sampay_response' ));
}
/**
* Plugin options, we deal with it in Step 3 too
*/
public function init_form_fields(){
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable / Disable',
'label' => 'Enable sampay',
'type' => 'checkbox',
'default' => 'no',
),
'title' => array(
'title' => 'Title',
'type' => 'text',
'description' => 'This controls the title which the user sees during checkout.',
'default' => 'sampay',
'desc_tip' => true,
),
'description' => array(
'title' => 'Description',
'type' => 'textarea',
'description' => 'This controls the description which the user sees during checkout.',
'default' => 'Payment Methods Accepted: MasterCard, VisaCard, Mtn MOMO. Sampay Assured',
'desc_tip' => true,
'css' => 'max-width:450px;',
),
'appkey' => array(
'title' => 'sampay App Key',
'type' => 'text',
'description' => 'Enter Your sampay Merchant ID, this can be gotten on your account page when you login on sampay',
'desc_tip' => true,
),
'authkey' => array(
'title' => 'Auth Key',
'type' => 'text',
'description' => 'long string of Alphanumeric characters sent to you by your account officer',
'desc_tip' => true,
),
'currency' => array(
'title' => 'Store Currency',
'type' => 'select',
'description' => 'Currency you wish to accept payment in. Make sure this tallys with set currency of woocommerce and bank',
'desc_tip' => true,
'options' => array(
"566" => "ZMW"),
),
'sandbox' => array(
'title' => 'Sandbox',
'label' => 'Enable Test Mode',
'type' => 'checkbox',
'description' => 'Enable/Disable Test mode',
'desc_tip' => true,
'default' => 'yes',
)
);
}
public function receipt_page($order_id) {
if(isset($_GET['token'])){
$this->sampay_process_onreturn();
}
echo '<p><span style="background-color: blue;color: #f4f4f4;padding: 5px;">Please review your order, then click on "Pay via sampay" to be redirected to the Payment Gateway</span></p>';
echo $this->generate_sampay_button($order_id);
}
/**
* Generate the sampay Payment button link
**/
public function sampay_process_onreturn(){
global $woocommerce;
//process order here
//check order status on sampay
$url = "https://samafricaonline.com/sam_pay/public/wpcheckstatus";
$tok = $_GET['token'];
$data = array('token' => $tok);
$response = wp_remote_post($url, array(
'method' => 'POST',
'httpversion' => '1.0',
'timeout' => 45,
'sslverify' => false,
'headers' => array('Content-Type'=>'application/json; charset=utf-8'),
'body' => json_encode($data))
);
if ( is_wp_error( $response ) ) {
//$error_message = $response->get_error_message();
}
else{
if ( 200 !== wp_remote_retrieve_response_code( $response ) ) {
//echo "response from page not 200";
// var_dump( $response);
}
else{
if ( 200 === wp_remote_retrieve_response_code( $response ) ) {
$response_body = wp_remote_retrieve_body( $response );
$apiresponse = json_decode($response_body);
//fetch all need variables from response and loacl
$status = $apiresponse->status;
$status = strtolower($status);
$orderid = $apiresponse->orderid;
$order_id = trim($orderid);
$tranxid = "n/a";
$order = wc_get_order($order_id);
//check order status
$ostat = $order->get_status();
//process if still pending
if($ostat=='pending'){
if($status=='canceled'){
#payment cancelled
$respond_desc = "Transaction Cancelled.";
$message_resp = "Transaction was cancelled by Sampay User.";
$order->add_order_note('sampay payment failed:<br> ' . $respond_desc, true);
$order->update_status('cancelled');
wp_redirect($order->get_cancel_order_url());
}
elseif($status=='paid'){
$respond_desc = "Transaction Successful";
$message_resp = "Thank You!. <br><strong>Reason</strong>: " . $respond_desc;
$message_type = "success";
$order->payment_complete();
$order->update_status('completed');
wc_reduce_stock_levels( $order->get_id() );
$order->add_order_note( 'Hey, your order is paid! Thank you! <br>Sampay Transaction Reference: ' . $tranxid, true );
wc_add_notice( $message_resp, $message_type );
// Empty cart
$woocommerce->cart->empty_cart();
// Redirect to the thank you page
wp_redirect($this->get_return_url( $order ));
}
else{}
}
}
}
}
}
public function generate_sampay_button($order_id) {
global $woocommerce;
// we need it to get any order details
$order = wc_get_order( $order_id );
$appkey = $this->appkey;
$authkey = $this->authkey;
$orderID = $order->get_id() . '_' . date("ymds");
$ordername = $order->get_id() . '_' . date("ymds");
$orderdetails = $order->get_id() . '_' . date("ymds");
$ordertotal = $order->get_total();
$ordertotal *=1;
$currency = $this->$currency;
$sampay_tranx_noti_url = $this->notify_url;
//Perform hash
$my_hash = $appkey . $authkey . $orderID . $ordername . $orderdetails . $ordertotal . $currency . $sampay_tranx_noti_url;
$sampay_hash = hash('sha512', $my_hash);
/*
* Array with parameters for API interaction
*/
foreach ( $order->get_items() as $item_id => $item ) {
$product_id = $item->get_product_id();
$variation_id = $item->get_variation_id();
$product = $item->get_product();
$name = $item->get_name();
$quantity = $item->get_quantity();
$subtotal = $item->get_subtotal();
$total = $item->get_total();
$tax = $item->get_subtotal_tax();
$taxclass = $item->get_tax_class();
$taxstat = $item->get_tax_status();
$allmeta = $item->get_meta_data();
$somemeta = $item->get_meta( '_whatever', true );
$type = $item->get_type();
}
;
$purchasedetails="Purchase";
$data = array(
'AppKey' => $this->appkey, // API Key Merchant /
'AuthKey' => $this->authkey,
'OrderID' => $order->get_id(),
'OrderName' => $order->$product,
'OrderTotal' => $order->order_total ?? $order->total,
'OrderDetails' => $purchasedetails,
'Currency' => $order->get_currency(),
'WPRTURL' => $order->get_order_key()
);
$sampay_args_array = array();
foreach ($data as $key => $value) {
$sampay_args_array[] = "<input type='hidden' name='$key' value='$value'/>";
}
return '<form action="' . $this->posturl . '" method="POST" id="sampay_payment_form">' . implode('', $sampay_args_array) . '
<input type="submit" class="button-alt" id="submit_sampay_payment_form" value="' . __('Pay via sampay', 'sampay') . '" /> <a class="button cancel" href="' . $order->get_cancel_order_url() . '">' . __('Cancel order & restore cart', 'sampay') . '</a>
<script type="text/javascript">
function processsampayJSPayment(){
jQuery("body").block(
{
message: "<img src=\"' . plugins_url('assets/images/ajax-loader.gif', __FILE__) . '\" alt=\"redirecting...\" style=\"float:left; margin-right: 10px;\" />' . __('Thank you for your order. We are now redirecting you to Payment Gateway to make payment.', 'sampay') . '",
overlayCSS:
{
background: "#fff",
opacity: 0.6
},
css: {
padding: 20,
textAlign: "center",
color: "#555",
border: "3px solid #aaa",
backgroundColor:"#fff",
cursor: "wait",
lineHeight:"32px"
}
});
jQuery("#sampay_payment_form").submit();
}
jQuery("#submit_sampay_payment_form").click(function (e) {
e.preventDefault();
processsampayJSPayment();
});
</script>
</form>';
}
/*
* We're processing the payments here, everything about it is in Step 5
*/
public function process_payment( $order_id ) {
global $woocommerce;
$order = wc_get_order($order_id);
return array(
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true )
);
}
/*
* In case you need a webhook, like PayPal IPN etc
*/
public function get_sampay_response() {
global $woocommerce;
//Get reponse
$filepamil = 'logPam.txt';
if(! empty( $_POST )){
//echo '<pre>'; print_r($_POST); echo '</pre>';
$posted = wp_unslash($_POST);
$appkey = $this->appkey;
$authkey = $this->authkey;
$tranxid = $posted['sampay_tranx_id'];
$sampay_echo_data = $posted['sampay_echo_data'];
$data = explode(";", $sampay_echo_data);
$wc_order_arr = explode("_", $data[0]);
$wc_order_id = trim($wc_order_arr[0]);
$resp_order = wc_get_order($wc_order_id);
$total_amount = $resp_order->get_total();
$total_amt =$total_amount * 100;
$reff = base64_encode($total_amount . "| Sampay_TranxId" . $tranxid);
$resp_order->add_order_note('Reff: ' . $reff);
if ($posted['sampay_tranx_status_code'] === '200') {
#payment cancelled
$respond_desc = $posted['sampay_tranx_status_msg'];
$message_resp = "Your transaction failed. <br><strong>Reason</strong>: " . $respond_desc . "<br><strong>Transaction Reference<strong>:" . $tranxid . '<br>You may restart your payment below.';
$message_type = "error";
$resp_order->add_order_note('sampay payment failed:<br> ' . $respond_desc . '<br>Transaction Reference: ' . $tranxid, true);
$resp_order->update_status('cancelled');
wc_add_notice($message_resp, $message_type);
wp_redirect($resp_order->get_cancel_order_url());
}else{
//Payment params successfully posted
//confirm hash
// if (strtolower($posted['sampay_verification_hash']) == strtolower($sampay_hash)){
$hash_req = hash('sha512', $appkey.$authkey.$token.$tranxid);
$get_url = $this->geturl;
$params ='appkey='.$appkey.'authkey='.$authkey.'token='.$token.'&amount='.$total_amt.'&tranxid='.$tranxid.'&hash='.$hash_req;
$my_url = $get_url. '?' .$params;
$gt_json_response = wp_remote_get($my_url);
if( !is_wp_error( $gt_json_response ) ) {
$data = json_decode( $gt_json_response['body'], true );
if( $data['ResponseCode'] == '00'){
#payment successful
// we received the payment
$respond_desc = $data['ResponseDescription'];
$message_resp = "Thank You!. <br><strong>Reason</strong>: " . $respond_desc . "<br><strong>Transaction Reference</strong>:" . $tranxid;
$message_type = "success";
$resp_order->payment_complete();
$resp_order->update_status('completed');
wc_reduce_stock_levels( $resp_order->get_id() );
$resp_order->add_order_note( 'Hey, your order is paid! Thank you! <br>sampay Transaction Reference: ' . $tranxid, true );
wc_add_notice( $message_resp, $message_type );
// Empty cart
$woocommerce->cart->empty_cart();
// Redirect to the thank you page
wp_redirect($this->get_return_url( $resp_order ));
}else{
// something went horribly wrong
$respond_desc = $data['ResponseDescription'];
$message_resp = "Your Transaction was unsuccessful.<br>Reason: ". $respond_desc . "<br><strong>Transaction Reference</strong>:" . $tranxid ."<br><strong>Please Try Again!</strong>";
$message_type = "error";
$resp_order->add_order_note('sampay payment failed:<br> ' . $respond_desc . '<br>Transaction Reference: ' . $tranxid, true);
$resp_order->update_status('cancelled');
wc_add_notice( $message_resp, $message_type );
wp_redirect($resp_order->get_cancel_order_url());
}
} else {
wc_add_notice( 'Connection error.', 'error' );
}
}
}else {
exit();
die();
}
}
public function getostat($order_id){
global $woocommerce;
$order = wc_get_order($order_id);
$status = $order->get_status();
return $status;
}
}
}
//add_action( 'plugins_loaded', 'sampay_alt_process' );