-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathEasyPay.php
228 lines (209 loc) · 7.41 KB
/
EasyPay.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
<?php
/**
* WHMCS EasyPay Module
* This is the EasyPay (EasyPaisa) Payment Module for WHMCS
* @author Fahad Yousaf mahar (www.webit.pk)
* @license MIT
*/
if (!defined("WHMCS")) {
die("This file cannot be accessed directly");
}
/**
* EasyPay metaData
*
* Values returned here are used to determine module related capabilities and
* settings.
*
* @see http://docs.whmcs.com/Gateway_Module_Meta_Data_Parameters
*
* @return array
*/
function easypay_MetaData()
{
return array(
'DisplayName' => 'easypay',
'APIVersion' => '1.1', // Use API Version 1.1
'DisableLocalCredtCardInput' => true,
'TokenisedStorage' => false,
);
}
/**
* Define gateway configuration options.
*
* The fields you define here determine the configuration options that are
* presented to administrator users when activating and configuring your
* payment gateway module for use.
*
* Supported field types include:
* * text
* * password
* * yesno
* * dropdown
* * radio
* * textarea
*
* Examples of each field type and their possible configuration parameters are
* provided in the sample function below.
*
* @return array
*/
function easypay_config()
{
return array(
// the friendly display name for a payment gateway should be
// defined here for backwards compatibility
'FriendlyName' => array(
'Type' => 'System',
'Value' => 'easypay',
),
// a text field type allows for single line text input
'accountID' => array(
'FriendlyName' => 'Account ID',
'Type' => 'text',
'Size' => '4',
'Default' => '',
'Description' => 'Enter your account ID here',
),
'hashkey' => array(
'FriendlyName' => 'Secret Hash Key',
'Type' => 'text',
'Size' => '32',
'Default' => '',
'Description' => 'Enter your Easy Paisa Merchant Secret Hash Key ',
),
/**
* Test Mode Option Added
* Author: Shaz3e
* @url shaz3e.com
*/
'testMode' => array(
'FriendlyName' => 'Test Mode',
'Type' => 'yesno',
'Default' => '',
'Description' => 'Tick to enable test mode (Optional)',
),
);
}
/**
* getHashedRequest() Function to generate EasyPay Hash
**/
function getHashedRequest($hKey, $orderId, $amt, $autoRed, $email, $expiryDate, $storeId, $merchantConfirmPage) {
$hashRequest = '';
if(strlen($hKey) > 0 && (strlen($hKey) == 16 || strlen($hKey) == 24 || strlen($hKey) == 32 )) {
// Create Parameter map
//error_log('Order INFO: '. $orderId);
$paramMap = array();
$paramMap['amount'] = $amt ;
$paramMap['autoRedirect'] = $autoRed ;
$paramMap['emailAddr'] = $email ;
$paramMap['expiryDate'] = $expiryDate;
//$paramMap['mobileNum'] = $phone ;
$paramMap['orderRefNum'] = $orderId;
//$paramMap['paymentMethod'] = $paymentMode;
$paramMap['postBackURL'] = $merchantConfirmPage;
$paramMap['storeId'] = $storeId;
//Creating string to be encoded
$mapString = '';
foreach ($paramMap as $key => $val) {
$mapString .= $key.'='.$val.'&';
}
$mapString = substr($mapString , 0, -1);
// error_log('MAPString: '.$mapString);
// Encrypting mapString
function pkcs5_pad($text, $blocksize) {
$pad = $blocksize - (strlen($text) % $blocksize);
return $text . str_repeat(chr($pad), $pad);
}
$alg = MCRYPT_RIJNDAEL_128; // AES
$mode = MCRYPT_MODE_ECB; // ECB
$iv_size = mcrypt_get_iv_size($alg, $mode);
$block_size = mcrypt_get_block_size($alg, $mode);
$iv = mcrypt_create_iv($iv_size, MCRYPT_DEV_URANDOM);
$mapString = pkcs5_pad($mapString, $block_size);
$crypttext = mcrypt_encrypt($alg, $hKey, $mapString, $mode, $iv);
$hashRequest = base64_encode($crypttext);
//error_log('hashReq: '.$hashRequest);
}
return $hashRequest;
}
/**
* Generation of Payment link.
*
* @return string
*/
function easypay_link($params)
{
// Gateway Configuration Parameters
$accountId = $params['accountID'];
$hashKey = $params['hashkey'];
$testMode = $params['testMode'];
// Invoice Parameters
$invoiceId = $params['invoiceid'];
$description = $params["description"];
$amount = $params['amount'];
// $currencyCode = $params['currency'];
//Client Parameters
$firstname = $params['clientdetails']['firstname'];
$lastname = $params['clientdetails']['lastname'];
$email = $params['clientdetails']['email'];
$address1 = $params['clientdetails']['address1'];
$address2 = $params['clientdetails']['address2'];
$city = $params['clientdetails']['city'];
$state = $params['clientdetails']['state'];
$postcode = $params['clientdetails']['postcode'];
$country = $params['clientdetails']['country'];
$phone = $params['clientdetails']['phonenumber'];
// System Parameters
$companyName = $params['companyname'];
$systemUrl = $params['systemurl']; // System URL
$returnUrl = $params['returnurl'];
$langPayNow = $params['langpaynow'];
$moduleDisplayName = $params['name'];
$moduleName = $params['paymentmethod'];
$whmcsVersion = $params['whmcsVersion'];
// EasyPay POST Parameters
$storeId=$accountId; // setting Store ID equal to Account Id as given in Gateway Config
$orderId=$invoiceId; // setting Order ID equal to Invoice Id
$autoRedirect = 1; // Auto Redirection after invoice payment at Easypay Portal
$currentDate = new DateTime();
$currentDate->modify('+ 10 day');
$expiryDate = $currentDate->format('Ymd His'); // Conversion for expiry date&time
/**
* Test Mode yes/no
* Added in v.1.1
*/
if ( !$testMode ) {
$url = 'https://easypay.easypaisa.com.pk/easypay/Index.jsf';
$confirmUrl = 'https://easypay.easypaisa.com.pk/easypay/Confirm.jsf';
}else{
$url = 'https://easypaystg.easypaisa.com.pk/easypay/Index.jsf';
$confirmUrl = 'https://easypaystg.easypaisa.com.pk/easypay/Confirm.jsf';
}
$amount = number_format($amount,1,'.', ''); //Converting amount to one-decimal point
$callback = $systemUrl.'modules/gateways/callback/EasyPay.php'; // Callback
// Generation of MerchantHashedRequest
$hash = getHashedRequest($hashKey, $orderId, $amount, $autoRedirect, $email, $expiryDate, $storeId, $callback);
// Post Fields to be placed in HTML form on invoice page
$postfields = array();
$postfields['storeId'] = $storeId;
$postfields['merchantHashedReq'] = $hash;
$postfields['orderRefNum'] = $orderId;
$postfields['expiryDate'] = $expiryDate;
$postfields['amount'] = $amount;
$postfields['emailAddr'] = $email;
$postfields['autoRedirect'] = $autoRedirect;
$postfields['paymentMethod'] = $paymentMode;
$postfields['postBackURL'] = $callback;
// Start of HTML form with action = EasyPay Portal URL
$htmlOutput = '';
$htmlOutput .= '<form action="'.$url.'" method="POST">';
// Generation of <input> tags with Parameters to be sent to EasyPay
foreach ($postfields as $k => $v) {
$htmlOutput .= '<input type="hidden" name="' . $k . '" value="'. $v. '" />';
}
$htmlOutput .= '<button type="submit" class="btn btn-success"><i class="glyphicon glyphicon-credit-card"></i> '.$langPayNow.'</button>';
// End HTML Form
$htmlOutput .= '</form>';
return $htmlOutput;
}
?>