-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.php
344 lines (298 loc) · 14 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
<?php
/*
* Plugin Name: Epay Payment Gateway
* Plugin URI: https://docs.epaygh.com/docs/libraries/
* Description: Epay payment gateway plugin for WooCommerce. It allows you to accept Mobile Money payments in your shop
* Version: 1.1.4
* Author: AppGharage
* Author URI: https://appgharage.com
*/
/*
* This action hook registers our PHP class as a WooCommerce payment gateway
*/
add_filter('woocommerce_payment_gateways', 'epay_add_gateway_class');
function epay_add_gateway_class($gateways)
{
$gateways[] = 'WC_Epay_Payment_Gateway';
return $gateways;
}
/*
* The class itself, please note that it is inside plugins_loaded action hook
*/
add_action('plugins_loaded', 'epay_init_gateway_class');
function epay_init_gateway_class()
{
class WC_Epay_Payment_Gateway extends WC_Payment_Gateway
{
/**
* Class constructor
*/
public function __construct()
{
$this->id = 'epay';
$this->icon = plugins_url('banner.png', __FILE__);
$this->has_fields = true;
$this->method_title = 'Epay';
$this->method_description = 'Recieve Mobile Money payments in your shop.';
$this->instructions= "Kindly Approve the payment Request on your mobile Handset";
$this->supports = array(
'products'
);
// Method with all the options fields
$this->init_form_fields();
// Load the settings.
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->enabled = $this->get_option('enabled');
$this->merchant_key =$this->get_option('merchant_key');
$this->app_id =$this->get_option('app_id');
$this->app_secret =$this->get_option('app_secret');
// This action hook saves the settings
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ));
//Custom Actions
add_action('woocommerce_thankyou_' . $this->id, array( $this, 'thank_you_page' ));
}
/**
* Plugin options
*/
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => 'Enable/Disable',
'label' => 'Enable Epay Gateway',
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
'title' => array(
'title' => 'Title',
'type' => 'text',
'description' => 'This controls the title which the user sees during checkout.',
'default' => 'Mobile Money powered by Epay',
'desc_tip' => true,
),
'description' => array(
'title' => 'Description',
'type' => 'textarea',
'description' => 'This controls the description which the user sees during checkout.',
'default' => 'Pay with any Ghanaian Mobile Money Wallet powered by epaygh.com.',
),
'merchant_key' => array(
'title' => 'Account Merchant Key',
'type' => 'text',
'description' => 'This is your Merchant Key as displayed under Account Settings on your Epay Dashboard',
),
'app_id' => array(
'title' => 'Integration App ID',
'type' => 'text',
'description' => '<b>NB: You\'re required to create an Integration first</b>. This is your Integration\'s App ID as displayed under Integrations on your Epay Dashboard.',
),
'app_secret' => array(
'title' => 'Integration App Secret',
'type' => 'text',
'description' => '<b>NB: You\'re required to create an Integration first</b>. This is your Integration\'s App Secret as displayed under Integrations on your Epay Dashboard. ',
)
);
}
/**
* Custom Mobile Money Payment Form
*/
public function payment_fields()
{
// ok, let's display some description before the payment form
if ($this->description) {
// display the description with <p> tags etc.
echo wpautop(wp_kses_post($this->description));
}
// I will echo() the form, but you can close PHP tags and print it directly in HTML
echo '<fieldset id="wc-' . esc_attr($this->id) . '-cc-form" class="wc-credit-card-form wc-payment-form" style="background:transparent;">';
// Add this action hook if you want your custom gateway to support it
do_action('woocommerce_momo_form_start', $this->id);
// I recommend to use inique IDs, because other gateways could already use #ccNo, #expdate, #cvc
echo '<div class="form-row form-row-wide"><label>Mobile Wallet Provider <span class="required">*</span></label>
<select id="epaygh_mobile_wallet_network" name="epaygh_mobile_wallet_network">
<option name="epaygh_mobile_wallet_network" value="mtn" selected>MTN</option>
<option name="epaygh_mobile_wallet_network" value="airtel">Airtel</option>
<option name="epaygh_mobile_wallet_network" value="tigo">Tigo</option>
<option name="epaygh_mobile_wallet_network" value="vodafone">Vodafone</option>
</select>
</div>
<div class="form-row form-row-wide"><label>Mobile Wallet Number <span class="required">*</span></label>
<input id="epaygh_mobile_wallet_number" name="epaygh_mobile_wallet_number" type="tel">
</div>
<div class="form-row form-row-wide"><label>Voucher </label>
<input id="epaygh_payment_voucher" name="epaygh_payment_voucher" type="text" autocomplete="off"><br>
<span class="required">Leave empty if mobile network is not Vodafone</span>
</div>
<div class="clear"></div>';
do_action('woocommerce_momo_form_end', $this->id);
echo '<div class="clear"></div></fieldset>';
}
/*
* Fields validation
*/
public function validate_fields()
{
if (empty($_POST[ 'epaygh_mobile_wallet_network' ])) {
wc_add_notice('Mobile Wallet Network Provider is required!', 'error');
return false;
}
if (empty($_POST[ 'epaygh_mobile_wallet_number' ])) {
wc_add_notice('Mobile Wallet Number is required!', 'error');
return false;
}
if ($_POST[ 'epaygh_mobile_wallet_network' ] == 'vodafone' && empty($_POST[ 'epaygh_payment_voucher' ])) {
wc_add_notice('A Voucher is required for Vodafone payments!', 'error');
return false;
}
if (empty($_POST[ 'billing_first_name' ])) {
wc_add_notice('First name is required!', 'error');
return false;
}
if (empty($_POST[ 'billing_last_name' ])) {
wc_add_notice('Last name is required!', 'error');
return false;
}
return true;
}
/*
* We're processing the payments here
*/
public function process_payment($order_id)
{
global $woocommerce;
// we need it to get any order detailes
$order = wc_get_order($order_id);
/**
* Generate a random string, using a cryptographically secure
* pseudorandom number generator (random_int)
*
* For PHP 7, random_int is a PHP core function
* For PHP 5.x, depends on https://github.com/paragonie/random_compat
*
* @param int $length How many characters do we want?
* @param string $keyspace A string of all possible characters
* to select from
* @return string
*/
function random_str($length, $keyspace = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ')
{
$pieces = [];
$max = mb_strlen($keyspace, '8bit') - 1;
for ($i = 0; $i < $length; ++$i) {
$pieces []= $keyspace[random_int(0, $max)];
}
return implode('', $pieces);
}
/*
* Array with parameters for API interaction
*/
$auth_api_args = array(
"merchant_key" => $this->merchant_key,
"app_id" => $this->app_id,
"app_secret" => $this->app_secret
);
$auth_request_body = array(
'body' => json_encode($auth_api_args),
'headers' => array(
'Content-type' => ' application/json'
)
);
/*
* Generate Access Token
*/
$response = wp_remote_post('https://epaygh.com/api/v1/token', $auth_request_body);
if (!is_wp_error($response)) {
$body = json_decode($response['body'], true);
if (!$body['success']) {
wc_add_notice('Oops! Failed to generate Access Token. Please try again.', 'error');
return;
}
$access_token = $body['data']['access_token'];
$charge_api_args = array(
"reference" => random_str(12),
"amount" => (float) $order->get_total(),
"payment_method" => "momo",
"customer_name"=> $order->get_billing_first_name() . " " . $order->get_billing_last_name(),
"customer_email" => $order->get_billing_email(),
"customer_telephone" => $order->get_billing_phone(),
"mobile_wallet_number" => $_POST['epaygh_mobile_wallet_number'],
"mobile_wallet_network" => $_POST['epaygh_mobile_wallet_network'],
"voucher"=> $_POST['epaygh_payment_voucher'],
"payment_description" => 'Purchase with Order ID of '.$order->get_order_number(),
"currency" => $order->get_currency()
);
$charge_request_body = array(
'body' => json_encode($charge_api_args),
'headers' => array(
'Content-type' => ' application/json',
'Authorization' => 'Bearer '. $access_token
)
);
/*
* Charge Customer
*/
$charge_api_response = wp_remote_post('https://epaygh.com/api/v1/charge', $charge_request_body);
if (is_wp_error($charge_api_response)) {
wc_add_notice('Connection Lost whilst trying to charge customer.', 'error');
return;
}
$charge_api_body = json_decode($charge_api_response['body'], true);
if (!$charge_api_body['success']) {
wc_add_notice($charge_api_body['message'], 'error');
return;
}
//wc_add_notice('A Payment Request has been sent to your Mobile Wallet, Kindly approve Payment.', 'success');
// Return thankyou redirect
return array(
'result' => 'success',
'redirect' => $this->get_return_url($order)
);
return;
} else {
wc_add_notice('Connection error.', 'error');
return;
}
}
/**
* Output for the order received page.
*/
public function thank_you_page()
{
if ($this->instructions) {
return $this->displayPaymentApprovalInstructions();
}
}
private function displayPaymentApprovalInstructions()
{
echo "
<br>
<h3>A Payment Request has been sent to your Mobile Money Wallet, Kindly follow the payment approval procedures below to approve your payment;</h3>
<p><strong>Using MTN Mobile Money? Follow the steps below;</strong></p>
<ol>
<li>Dial *170#</li>
<li>Select option 6 [Wallet]</li>
<li>Select 3 [My Approvals]</li>
<li>Enter your MOMO pin</li>
<li>Select 1, which is the Pending transaction</li>
<li>Choose option 1 [Approve] </li>
<li>Confirm the transaction</li>
</ol>
<br>
<p><strong>Using Tigo Cash? Follow the steps below;</strong></p>
<ol>
<li>Dial *170#</li>
<li>Select option 7 [Wallet]</li>
<li>Select 3 [My Approvals]</li>
<li>Enter your MOMO pin</li>
<li>Select 1, which is the Pending transaction</li>
<li>Choose option 1 [Approve] </li>
<li>Confirm the transaction</li>
</ol>
<br>
";
}
}
}