-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathapiController.php
103 lines (85 loc) · 3.92 KB
/
apiController.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
<?php
require_once 'apiconnect.php';
/* Setting a custom WP-REST API endpoint to listen to Selcom callbacks */
add_action('rest_api_init', function() {
register_rest_route(
'selcom-push/v2', //Namespace
'/selcom_callback', //Callback endpoint
array (
'methods' => 'POST',
'callback' => 'receiveApiCallback',
'permission_callback' => '__return_true',
)
);
});
//Creating a minimal order to send to Checkout API before initiating payment
function sendMinOrder ($minOrder, $apiKey, $apiSecret, $baseUrl) {
//Set API endpoint
$apiEndpoint = "/checkout/create-order-minimal";
$url = $baseUrl.$apiEndpoint;
//Set POST request variables
$isPost =1;
$timestamp = date('c');
$authorization = base64_encode($apiKey);
$signedFields = implode(',', array_keys($minOrder));
$digest = computeSignature($minOrder, $signedFields, $timestamp, $apiSecret);
//Make HTTP POST Request for Minimal Order
return sendHTTPRequest($url, $isPost, json_encode($minOrder), $authorization, $digest, $signedFields, $timestamp);
}
//Initiating payment via Push USSD
function sendUSSDPush ($pushRequest, $apiKey, $apiSecret, $baseUrl) {
//Set API endpoint
$apiEndpoint = "/checkout/wallet-payment";
$url = $baseUrl.$apiEndpoint;
//Set POST request variables
$isPost =1;
$timestamp = date('c');
$authorization = base64_encode($apiKey);
$signedFields = implode(',', array_keys($pushRequest));
$digest = computeSignature($pushRequest, $signedFields, $timestamp, $apiSecret);
//Make HTTP POST Request for USSD Push to Wallet
return sendHTTPRequest($url, $isPost, json_encode($pushRequest), $authorization, $digest, $signedFields, $timestamp);
}
/* Receive final callback from Selcom API */
function receiveApiCallback (WP_REST_Request $request) {
//Create JSON data object from request object
$params = json_decode(stripslashes($request->get_body()));
//Validate variables and set response
if (isset($params)) {
//Create order object and fetch order for status & stock updates
// TODO: Check if order ID is present and if order exists
$order = wc_get_order($params->order_id);
if ($params->result == 'SUCCESS' && $params->payment_status=='COMPLETE') {
//Payment successful, update order status to successful
$order->update_status('processing', __('Payment is complete, order is being processed', 'woocommerce'));
//Update item(s) stock in the inventory
wc_reduce_stock_levels($order);
}
else {
//Payment failed, update order status to failed
$order->update_status('failed', __('Payment failed or was cancelled', 'woocommerce'));
}
$callbackResponse = array("error"=>200,"result"=>'Success',"order_id"=>$params->order_id,"message"=>'Callback successfully received');
}
else {
$callbackResponse = array("error"=>418,"result"=>'Failed',"message"=>'Callback failed, please resend');
}
//Return immediate acknowledgement to Selcom callback
return $callbackResponse;
}
//Getting order status to complete inventory updates & process delivery
function fetchOrderStatus($minOrder,$baseUrl) {
//Set API endpoint
$apiEndpoint = '/checkout/order-status?order_id='.$minOrder->order_id;
$url = $baseUrl.$apiEndpoint;
//Set GET request variables
$isPost =0;
$timestamp = date('c');
$authorization = base64_encode($apiKey);
$signedFields = implode(',', array_keys(array('order_id' => $minOrder->order_id)));
$digest = computeSignature($pushRequest, $signedFields, $timestamp, $apiSecret);
// TODO: Set to GET Request to Fetch Order Status
//Make HTTP GET Request to fetch order status
return sendHTTPRequest($url, $isPost, json_encode($pushRequest), $authorization, $digest, $signedFields, $timestamp);
return json_decode($result);
}