forked from invite-frey/is-woo-payment-fps
-
Notifications
You must be signed in to change notification settings - Fork 0
/
wc-gateway-invite-fps-payment-gateway-class.php
344 lines (287 loc) · 16 KB
/
wc-gateway-invite-fps-payment-gateway-class.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
if (!defined('ABSPATH')) {
return;
}
if( !class_exists('WC_Gateway_Invite_FPS_Payment_Gateway') ){
class WC_Gateway_Invite_FPS_Payment_Gateway extends WC_Payment_Gateway {
public function __construct() {
$this->id = ITS_WPF_PLUGIN_ID;
$this->icon = plugins_url('assets/fps.png', __FILE__);
$this->has_fields = true;
$this->method_title = __('Hong Kong Faster Payment System (FPS)',ITS_WPF_PLUGIN_ID);
$this->method_description = __("Hong Kong interbank real time transfer using account holder ids and QR codes.",ITS_WPF_PLUGIN_ID);
$this->supports = array(
'products'
);
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->account_id_type = $this->get_option( 'account_id_type' );
$this->account_fps_id = $this->get_option( 'account_fps_id' );
$this->account_bank_code = $this->get_option( 'account_bank_code' );
$this->ask_to_pay = $this->get_option( 'ask_to_pay' );
$this->fps_payment_reference_guide = $this->get_option( 'fps_payment_reference_guide' );
$this->write_qr_code_to_file = $this->get_option('write_qr_code_to_file');
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
}
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable/Disable',ITS_WPF_PLUGIN_ID),
'label' => __('Enable Hong Kong Faster Payment System',ITS_WPF_PLUGIN_ID),
'type' => 'checkbox',
'description' => '',
'default' => 'no'
),
'title' => array(
'title' => __('Title',ITS_WPF_PLUGIN_ID),
'type' => 'text',
'description' => __('This controls the title which the user sees during checkout.',ITS_WPF_PLUGIN_ID),
'default' => __('Hong Kong FPS',ITS_WPF_PLUGIN_ID),
'desc_tip' => true
),
'description' => array(
'title' => __('Description',ITS_WPF_PLUGIN_ID),
'type' => 'textarea',
'description' => __('This controls the description which the user sees during checkout.',ITS_WPF_PLUGIN_ID),
'default' => __('Pay with HK Faster Payment System (FPS). Scan the presented QR code with your bank\'s app or enter the payee FPS id.',ITS_WPF_PLUGIN_ID),
),
'account_id_type' => array(
'title' => __('Account Id Type',ITS_WPF_PLUGIN_ID),
'type' => 'select',
'options' => array(
'02' => __('FPS ID',ITS_WPF_PLUGIN_ID),
'03' => __('Mobile Phone Number',ITS_WPF_PLUGIN_ID),
'04' => __('Email Address',ITS_WPF_PLUGIN_ID)
),
'default' => '02'
),
'account_fps_id' => array(
'title' => __('Account Id',ITS_WPF_PLUGIN_ID),
'type' => 'text',
'description' => __('E-mail address, phone number (+852-xxxxxxxx) or specific FPS id',ITS_WPF_PLUGIN_ID),
),
'account_bank_code' => array(
'title' => __('Bank Code',ITS_WPF_PLUGIN_ID),
'type' => 'text',
'description' => __('Three digit Hong Kong bank code.',ITS_WPF_PLUGIN_ID)
),
'fps_payment_reference_guide' => array(
'title' => __('Payment Reference Guide',ITS_WPF_PLUGIN_ID),
'type' => 'textarea',
'default' => __('Please use the code below as a Message to Payee when initiating the transaction in your bank\'s app.',ITS_WPF_PLUGIN_ID),
'description' => __('Instructions visible to the customer for providing payment reference number after payment. This is not visble of Ask to Pay is active.',ITS_WPF_PLUGIN_ID)
),
'ask_to_pay' => array(
'title' => __('Ask to Pay Enabled',ITS_WPF_PLUGIN_ID),
'label' => __('Ask to Pay Enabled',ITS_WPF_PLUGIN_ID),
'type' => 'checkbox',
'description' => __('The ask to pay function must be enabled by your bank in order to use payment reference numbers.',ITS_WPF_PLUGIN_ID),
'default' => 'no'
),
);
}
public function validate_text_field($key, $value){
switch($key){
case 'account_fps_id':
$fps_id = trim($value);
switch($this->get_option( 'account_id_type' )){
case '03':
if( preg_match('/^\+852\-[0-9]{8}$/', $fps_id) ){
return $fps_id;
}else{
function my_error_notice() {
?>
<div class="error notice">
<p><?php _e( 'The account id must be a HK phone number formatted: +852-xxxxxxxx.', ITS_WPF_PLUGIN_ID ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'my_error_notice' );
function alert_border_account_id(){
?>
<script language="javascript">
document.querySelector('#woocommerce_its_wpf_payment_gateway_account_fps_id').style.borderColor = 'red';
</script>
<?php
}
add_action( 'admin_footer', 'alert_border_account_id');
return $this->account_fps_id;
}
break;
case '04':
if( preg_match('/^(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){255,})(?!(?:(?:\x22?\x5C[\x00-\x7E]\x22?)|(?:\x22?[^\x5C\x22]\x22?)){65,}@)(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22))(?:\.(?:(?:[\x21\x23-\x27\x2A\x2B\x2D\x2F-\x39\x3D\x3F\x5E-\x7E]+)|(?:\x22(?:[\x01-\x08\x0B\x0C\x0E-\x1F\x21\x23-\x5B\x5D-\x7F]|(?:\x5C[\x00-\x7F]))*\x22)))*@(?:(?:(?!.*[^.]{64,})(?:(?:(?:xn--)?[a-z0-9]+(?:-[a-z0-9]+)*\.){1,126}){1,}(?:(?:[a-z][a-z0-9]*)|(?:(?:xn--)[a-z0-9]+))(?:-[a-z0-9]+)*)|(?:\[(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})|(?:(?!(?:.*[a-f0-9][:\]]){7,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?)))|(?:(?:IPv6:(?:(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)|(?:(?!(?:.*[a-f0-9]:){5,})(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?::(?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?)))?(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))(?:\.(?:(?:25[0-5])|(?:2[0-4][0-9])|(?:1[0-9]{2})|(?:[1-9]?[0-9]))){3}))\]))$/', $fps_id)){
return $fps_id;
}else{
function my_error_notice() {
?>
<div class="error notice">
<p><?php _e( 'The account id must be a valid e-mail address', ITS_WPF_PLUGIN_ID ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'my_error_notice' );
function alert_border_account_id(){
?>
<script language="javascript">
document.querySelector('#woocommerce_its_wpf_payment_gateway_account_fps_id').style.borderColor = 'red';
</script>
<?php
}
add_action( 'admin_footer', 'alert_border_account_id');
return $this->account_fps_id;
}
break;
default:
return parent::validate_text_field($key,$value);
break;
}
break;
case 'account_bank_code':
$account = trim($value);
if( preg_match('/^[0-9]{3}$/', $account) ){
return $account;
}else{
function my_error_notice() {
?>
<div class="error notice">
<p><?php _e( 'The bank code must consist of three digits, including leading zeros.', ITS_WPF_PLUGIN_ID ); ?></p>
</div>
<?php
}
add_action( 'admin_notices', 'my_error_notice' );
function alert_border_bank_code(){
?>
<script language="javascript">
document.querySelector('#woocommerce_its_wpf_payment_gateway_account_bank_code').style.borderColor = 'red';
</script>
<?php
}
add_action( 'admin_footer', 'alert_border_bank_code');
return $this->account_bank_code;
}
break;
default:
return parent::validate_text_field($key,$value);
break;
}
}
private function fps_data($reference){
global $woocommerce;
$currency_code = get_woocommerce_currency();
//echo $currency_code . '<br>';
$fps_currencies = array(
"HKD" => "344",
"CNY" => "156"
);
$fps_currency = $fps_currencies[$currency_code] ?? null;
if( !$fps_currency )
return null;
$data = array(
"account" => $this->account_id_type,
"bank_code" => $this->account_bank_code,
"fps_id" => $this->account_id_type === "02" ? $this->account_fps_id : "",
"mobile" => $this->account_id_type === "03" ? $this->account_fps_id : "",
"email" => $this->account_id_type === "04" ? $this->account_fps_id : "",
"mcc" => "0000",
"curr" => $fps_currency,
"amount" => $this->get_order_total(),
"reference" => $this->ask_to_pay === 'yes' ? $reference : ""
);
return $data;
}
private function urlencode_array($array){
$url = "";
$delimiter = "";
foreach ($array as $key => $value) {
if($value !== ""){
$url .= $delimiter . $key . '=' . urlencode($value);
$delimiter = "&";
}
}
return $url;
}
private function qrcode_img_data($data_string){
/**
* Load the PHP QRcode library
*/
if( !class_exists( 'QRtools' ) ){
require_once 'libs/phpqrcode.php';
}
$fp = fopen('php://memory','r+');
QRCode::png($data_string,$fp,QR_ECLEVEL_H);
rewind($fp);
$qrcode = stream_get_contents($fp);
return $qrcode;
}
public function payment_fields() {
global $wp;
$fps_ref_string = $wp->query_vars['order-pay'] ?? $this->random_strings(5);
$fps_data = $this->fps_data($fps_ref_string);
if( !$fps_data ){
echo __("This payment method is only available for HKD payments",ITS_WPF_PLUGIN_ID);
return;
}
$fps_data['currency'] = $fps_data['curr'];
$qrcode = new ITS_FPS_QRCodeData($fps_data);
$qr_code_img = $this->qrcode_img_data($qrcode->getDataString());
if ( $this->description ) {
echo wpautop( wp_kses_post( $this->description ) );
}
echo '<fieldset id="wc-' . esc_attr( $this->id ) . '-cc-form" class="wc-credit-card-form wc-payment-form" style="background:transparent;">';
?>
<div class="form-row form-row-wide">
<?php __("FPS id:",ITS_WPF_PLUGIN_ID); ?> <strong><?php echo $this->account_fps_id ?></strong>
</div>
<?php
if ($this->ask_to_pay === 'no') {?>
<div class="form-row form-row-wide">
<label>FPS Payment Reference <span class="required">*</span><br><small><?php echo $this->fps_payment_reference_guide?></small></label>
<input id="its_wpf_payment_ref" name="its_wpf_payment_ref" type="text" autocomplete="off" value="<?php echo $fps_ref_string ?>">
</div>
<?php
}else{?>
<input id="its_wpf_payment_ref" name="its_wpf_payment_ref" type="hidden" value="<?php echo $fps_ref_string ?>">
<?php
}
?>
<div class="form-row form-row-wide" style="text-align: center;">
<img style="margin: 0 auto;" src="data:image/png;base64,<?php echo base64_encode($qr_code_img); ?>" />
</div>
<?php
echo '<div class="clear"></div></fieldset>';
}
public function validate_fields() {
if( empty( $_POST[ 'its_wpf_payment_ref' ]) ) {
wc_add_notice( __('Payment reference is required!',ITS_WPF_PLUGIN_ID), 'error' );
return false;
}else{
$trimmed = preg_replace('/\s+/', '', sanitize_text_field($_POST[ 'its_wpf_payment_ref' ]) );
if (strlen($trimmed) === 0) {
wc_add_notice( __('Invalid payment reference!',ITS_WPF_PLUGIN_ID), 'error' );
return false;
}
}
return true;
}
public function process_payment( $order_id ) {
global $woocommerce;
$order = new WC_Order( $order_id );
$order->update_status('on-hold', __( 'Awaiting manual confirmation of FPS payment.', ITS_WPF_PLUGIN_ID ));
$order->set_transaction_id(sanitize_text_field($_POST[ 'its_wpf_payment_ref' ]));
$order->save();
$woocommerce->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $order )
);
}
private function random_strings($length_of_string) {
// md5 the timestamps and returns substring
// of specified length
return substr(md5(time()), 0, $length_of_string);
}
}
}