-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcreatePayment.php
129 lines (103 loc) · 3.94 KB
/
createPayment.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
<?php
require("../../../init.php");
include("../../../includes/gatewayfunctions.php");
include("../../../invoicefunctions.php");
define("CLIENTAREA",true);
define("FORCESSL",true); // Uncomment to force the page to use https://
global $CONFIG;
$gatewaymodule = "tigopesa";
$gateway = getGatewayVariables($gatewaymodule);
$systemurl = ($CONFIG['SystemSSLURL']) ? $CONFIG['SystemSSLURL'].'/' : $CONFIG['SystemURL'].'/';
$baseURL = $gateway['basedomainurl'] ? $gateway['basedomainurl'] : $systemurl;
// Checks gateway module is active before accepting callback
if (!$gateway["type"]) die ("Tigopesa Module Not Activate");
// Customer Phone Number
$phone = $_POST['phone'];
$new_phone = preg_replace('/(?<=\d)\s+(?=\d)/', '', $phone);
$country_code = $_POST['country-calling-code-phone'];
$phoneNumber = $country_code.$new_phone;
// Gateway Configurations
$api_key = $gateway['apiKey'];
$secret_key = $gateway['secretKey'];
$customerMSISDN = $gateway['customerMSISDN'];
$customerPIN = $gateway['customerPIN'];
$accountID = $gateway['accountID'];
// Order Configuration
$data = base64_decode($_POST["data"]);
$data = unserialize($data);
// Subscriber
$account = $phoneNumber;
$firstname = $data['clientdetails']['firstname'];
$lastname = $data['clientdetails']['lastname'];
$email = $data['clientdetails']['email'];
$invoiceid = $data['invoiceid'];
$redirect_url = $baseURL . "modules/gateways/callback/tigopesa.php";
// Origin Payment
$amount = $data['amount'];
$currencyCode = $data['currency'];
$refecence_id = $invoiceid. "DU". generate_random_char();
// Function to Generate Reference ID.
function generate_random_char($data = null) {
$data = $data ?? random_bytes(16);
assert(strlen($data) == 16);
// Set version to 0100
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
// Set bits 6-7 to 10
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
// Output the 36 character UUID.
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
$payUrl = $gateway['paymentURL'];
$payload = array (
"MasterMerchant" => array(
"account" => $customerMSISDN,
"pin" => $customerPIN,
"id" => $accountID
),
"Subscriber" => array(
"account" => $phoneNumber,
"countryCode" => "255",
"country" => "TZA",
"firstName" => $firstname,
"lastName" => $lastname,
"emailId" => $email
),
"redirectUri" => $redirect_url,
"language" => "swa",
"terminalId" => "",
"originPayment" => array(
"amount" => $amount,
"currencyCode" => $currencyCode,
"tax" => "0.00",
"fee" => "0.00"
),
"exchangeRate" => "1",
"LocalPayment" => array(
"amount" => $amount,
"currencyCode" => $currencyCode,
),
"transactionRefId" => $refecence_id,
);
$payload = json_encode($payload);
$accessToken = base64_decode($_POST['accessToken']);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $payUrl);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt_array($ch, array(
CURLOPT_POST => TRUE,
CURLOPT_RETURNTRANSFER => TRUE,
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"accessToken: $accessToken",
),
));
$result = curl_exec($ch);
$results = json_decode($result);
$redirectUrl = $results->redirectUrl;
curl_close($ch);
// Redirect to tigopesa Secure Payment URL
echo '<script type="text/javascript">
window.location = "'. $redirectUrl.'"
</script>';
?>