1
1
<?php
2
2
3
- /* PaayByte Payment Gateway Class */
3
+ /* PayByte Payment Gateway Class */
4
4
class WC_Gateway_PayByte extends WC_Payment_Gateway {
5
5
6
6
public $ domain ;
@@ -12,13 +12,14 @@ public function __construct() {
12
12
13
13
$ plugin_dir = plugin_dir_url (__FILE__ );
14
14
15
- $ this ->domain = 'custom_payment ' ;
15
+ $ this ->domain = 'paybyte ' ;
16
16
17
- $ this ->id = 'custom ' ;
17
+ $ this ->id = 'paybyte ' ;
18
18
$ this ->icon = apply_filters ('woocommerce_custom_gateway_icon ' , $ plugin_dir .'/img/paybyte_logo_2.png ' );
19
19
$ this ->has_fields = false ;
20
+ $ this ->order_button_text = __ ('Proceed to PayByte ' , $ this ->domain );
20
21
$ this ->method_title = __ ( 'PayByte ' , $ this ->domain );
21
- $ this ->method_description = __ ( 'Allows Crypto payments with PayByte . ' , $ this ->domain );
22
+ $ this ->method_description = __ ( 'PayByte allows you to accept crypto payments on your WooCommerce Store . ' , $ this ->domain );
22
23
23
24
// Load the settings.
24
25
$ this ->init_form_fields ();
@@ -40,16 +41,16 @@ public function __construct() {
40
41
add_action ( 'woocommerce_update_options_payment_gateways_ ' . $ this ->id , array ( $ this , 'process_admin_options ' ) );
41
42
42
43
// Lets check for SSL
43
- add_action ( 'admin_notices ' , array ( $ this , 'do_ssl_check ' ) );
44
+ add_action ( 'admin_notices ' , array ( $ this , 'paybyte_do_ssl_check ' ) );
44
45
45
46
add_action ('woocommerce_api_ ' . strtolower (get_class ($ this )), array (
46
47
&$ this ,
47
- 'handle_callback '
48
+ 'paybyte_handle_callback '
48
49
));
49
50
50
51
add_action ('woocommerce_thankyou ' , function ($ order_id )
51
52
{
52
- $ coinAmount = get_post_meta ( $ order_id , 'coin_total ' , true );
53
+ $ coinAmount = get_post_meta ( $ order_id , 'paybyte_coin_total ' , true );
53
54
54
55
if (strcmp ($ coinAmount , "true " ) == 0 ) {
55
56
return ;
@@ -70,11 +71,10 @@ public function __construct() {
70
71
</table>
71
72
<?php
72
73
});
73
-
74
-
75
74
}
76
-
77
75
76
+ public function __destruct (){}
77
+
78
78
/**
79
79
* Build the administration fields for this specific Gateway
80
80
*/
@@ -106,10 +106,10 @@ public function init_form_fields() {
106
106
'desc_tip ' => false ,
107
107
),
108
108
'isTestnet ' => array (
109
- 'title ' => __ ( 'Testnet Mode ' ),
110
- 'label ' => __ ( 'Enable Testnet payments ' ),
109
+ 'title ' => __ ( 'Testnet Mode ' , $ this -> domain ),
110
+ 'label ' => __ ( 'Enable Testnet payments ' , $ this -> domain ),
111
111
'type ' => 'checkbox ' ,
112
- 'description ' => __ ( 'Place the payment gateway in test mode. ' ),
112
+ 'description ' => __ ( 'Place the payment gateway in test mode. ' , $ this -> domain ),
113
113
'default ' => 'no ' ,
114
114
),
115
115
'description ' => array (
@@ -127,21 +127,21 @@ public function init_form_fields() {
127
127
'default ' => 'BTC ' ,
128
128
'desc_tip ' => true ,
129
129
'options ' => array (
130
- 'BTC ' => __ ( 'Bitcoin ' , 'woocommerce ' ),
131
- 'BCH ' => __ ( 'Bitcoin Cash ' , 'woocommerce ' ),
132
- 'BTG ' => __ ( 'Bitcoin Gold ' , 'woocommerce ' ),
133
- 'BTX ' => __ ( 'BitCore ' , 'woocommerce ' ),
134
- 'DGB ' => __ ( 'DigiByte ' , 'woocommerce ' ),
135
- 'DASH ' => __ ( 'Dash ' , 'woocommerce ' ),
136
- 'GRS ' => __ ( 'Groestlcoin ' , 'woocommerce ' ),
137
- 'LTC ' => __ ( 'Litecoin ' , 'woocommerce ' )
130
+ 'BTC ' => __ ( 'Bitcoin ' , 'woocommerce ' ),
131
+ 'BCH ' => __ ( 'Bitcoin Cash ' , 'woocommerce ' ),
132
+ 'BTG ' => __ ( 'Bitcoin Gold ' , 'woocommerce ' ),
133
+ 'BTX ' => __ ( 'BitCore ' , 'woocommerce ' ),
134
+ 'DGB ' => __ ( 'DigiByte ' , 'woocommerce ' ),
135
+ 'DASH ' => __ ( 'Dash ' , 'woocommerce ' ),
136
+ 'GRS ' => __ ( 'Groestlcoin ' , 'woocommerce ' ),
137
+ 'LTC ' => __ ( 'Litecoin ' , 'woocommerce ' )
138
138
)
139
139
),
140
140
);
141
141
}
142
142
143
143
144
- /* function to display text and BTC attributes on checkout page*/
144
+ /* function to display text and crypto attributes on checkout page*/
145
145
public function payment_fields (){
146
146
global $ total_amt ;
147
147
@@ -152,7 +152,7 @@ public function payment_fields(){
152
152
153
153
$ url = "https://paybyte.io/api/get-rate?currency= " .$ currency ;
154
154
155
- // Send this payload to PayByte for processing
155
+ // Send this payload to PayByte for processing
156
156
$ response = wp_remote_get ( $ url );
157
157
158
158
// Retrieve the body's resopnse if no errors found
@@ -178,9 +178,12 @@ public function payment_fields(){
178
178
* Prepares the create payment URL.
179
179
*
180
180
* @param WC_Order $customer_order
181
+ * @param int $order_id
182
+ * @param string $callaback_guid
183
+ *
181
184
* @return string
182
185
*/
183
- public function prepare_create_payment_url ($ customer_order , $ order_id , $ callaback_guid ) {
186
+ public function paybyte_prepare_create_payment_url ($ customer_order , $ order_id , $ callaback_guid ) {
184
187
185
188
error_log ("preparing create payment url.. " );
186
189
$ api_key = $ this ->api_key ;
@@ -203,10 +206,10 @@ public function prepare_create_payment_url($customer_order, $order_id, $callabac
203
206
* Create a callback secret.
204
207
*
205
208
* @param WooCommerceOrder $customer_order
206
- * @return
209
+ * @return UniqueIdentifier for this callback.
207
210
*/
208
- public function create_callback_guid ($ customer_order ) {
209
- return uniqid ("" , true );
211
+ public function paybyte_create_callback_guid ($ customer_order ) {
212
+ return uniqid ("" , true );
210
213
}
211
214
212
215
/**
@@ -223,9 +226,9 @@ public function process_payment( $order_id ) {
223
226
// who to charge and how much
224
227
$ customer_order = new WC_Order ( $ order_id );
225
228
226
- $ callback_guid = $ this ->create_callback_guid ($ customer_order );
229
+ $ callback_guid = $ this ->paybyte_create_callback_guid ($ customer_order );
227
230
228
- $ create_payment_url = $ this ->prepare_create_payment_url ($ customer_order , $ order_id , $ callback_guid );
231
+ $ create_payment_url = $ this ->paybyte_prepare_create_payment_url ($ customer_order , $ order_id , $ callback_guid );
229
232
230
233
// Send this payload to PayByte for processing
231
234
$ response = wp_remote_get ( $ create_payment_url );
@@ -246,28 +249,30 @@ public function process_payment( $order_id ) {
246
249
$ payment_id = $ json_response ['transaction ' ]['payment-id ' ];
247
250
$ tran_status = $ json_response ['transaction ' ]['status ' ];
248
251
$ total_amt = $ json_response ['transaction ' ]['amount ' ];
252
+ $ coin = $ json_response ['transaction ' ]['coin ' ];
249
253
$ amount_received = $ json_response ['transaction ' ]['amount-received ' ];
250
254
$ received_payment_url = $ json_response ['transaction ' ]['payment-url ' ];
251
255
$ response_error = $ json_response ['error ' ];
252
256
253
257
if ($ response_error != "ok " ) {
254
258
wc_add_notice ( $ response_error , 'error ' );
255
259
//Add note to the order for your reference
256
- $ customer_order ->add_order_note ( 'Error: ' . $ response_error );
260
+ $ customer_order ->add_order_note ( 'Error from PayByte : ' . $ response_error );
257
261
258
262
$ customer_order ->update_status ('failed ' , __ ( 'PayByte payment failed ' , 'woocommerce ' ));
259
263
}
260
264
else {
261
265
262
266
global $ wpdb ;
263
- $ insert = $ wpdb ->insert ('wp_paybyte_payment ' ,
267
+ $ insert = $ wpdb ->insert ('paybyte_payment ' ,
264
268
array (
265
269
'user_id ' => get_current_user_id (),
266
270
'order_id ' => $ order_id ,
267
271
'payment_address ' => $ payment_address ,
268
272
'payment_id ' => $ payment_id ,
269
273
'status ' => $ tran_status ,
270
274
'amount ' => $ total_amt ,
275
+ 'coin ' => $ coin ,
271
276
'amount_received ' => $ amount_received ,
272
277
'callback_guid ' => $ callback_guid ,
273
278
'created ' => date ('Y-m-d H:i:s ' )
@@ -280,15 +285,15 @@ public function process_payment( $order_id ) {
280
285
$ customer_order ->add_order_note ( __ ( 'PayByte payment initiated. ' ) );
281
286
282
287
/* Save payment url in meta field*/
283
- update_post_meta ( $ order_id , 'payment_url ' , $ received_payment_url );
288
+ update_post_meta ( $ order_id , 'paybyte_payment_url ' , $ received_payment_url );
284
289
285
290
/* Save coin total in meta field*/
286
- update_post_meta ( $ order_id , 'coin_total ' , $ total_amt );
291
+ update_post_meta ( $ order_id , 'paybyte_coin_total ' , $ total_amt );
287
292
288
293
/* Save coin total in meta field*/
289
- update_post_meta ( $ order_id , 'coin_name ' , $ this ->coin );
294
+ update_post_meta ( $ order_id , 'paybyte_coin_name ' , $ this ->coin );
290
295
291
- $ customer_order ->update_status ('pending ' , __ ( 'Awaiting PayByte payment ' , 'woocommerce ' ));
296
+ $ customer_order ->update_status ('pending ' , __ ( 'Awaiting PayByte payment ' , 'woocommerce ' ));
292
297
293
298
// Reduce stock levels
294
299
$ customer_order ->reduce_order_stock ();
@@ -310,32 +315,36 @@ public function process_payment( $order_id ) {
310
315
}
311
316
}
312
317
313
-
314
318
// Check if we are forcing SSL on checkout pages
315
319
// Custom function not required by the Gateway
316
- public function do_ssl_check () {
320
+ public function paybyte_do_ssl_check () {
317
321
if ( get_option ( 'woocommerce_force_ssl_checkout ' ) == "no " ) {
318
- echo "<div class= \"error \"><p> " . sprintf ( __ ( "<strong>%s</strong> is enabled but WooCommerce is not forcing the SSL certificate on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href= \"%s \">forcing the checkout pages to be secured.</a> " ), $ this ->method_title , admin_url ( 'admin.php?page=wc-settings&tab=checkout ' ) ) ."</p></div> " ;
322
+ echo "<div class= \"error \"><p> " . sprintf ( __ ( "<strong>%s</strong> is enabled but WooCommerce is not forcing the SSL certificate on your checkout page. Please ensure that you have a valid SSL certificate and that you are <a href= \"%s \">forcing the checkout pages to be secured.</a> " , $ this -> domain ), $ this ->method_title , admin_url ( 'admin.php?page=wc-settings&tab=checkout ' ) ) ."</p></div> " ;
319
323
}
320
324
}
321
325
322
-
323
- function handle_callback () {
326
+ /**
327
+ * Handles the callback from PayByte.
328
+ *
329
+ * @param int $order_id
330
+ * @return array
331
+ */
332
+ public function paybyte_handle_callback () {
324
333
325
334
global $ wpdb ;
326
335
327
- $ order_id = $ _GET ['order_id ' ];
328
- $ secret = $ _GET ['secret ' ];
329
- $ payment_id = $ _GET ['payment_id ' ];
330
- $ isTestnet = $ _GET ['testnet ' ];
336
+ $ order_id = sanitize_text_field ( $ _GET ['order_id ' ]) ;
337
+ $ secret = sanitize_text_field ( $ _GET ['secret ' ]) ;
338
+ $ payment_id = sanitize_text_field ( $ _GET ['payment_id ' ]) ;
339
+ $ isTestnet = sanitize_text_field ( $ _GET ['testnet ' ]) ;
331
340
332
- error_log ("callback received: " . $ _GET [ ' order_id ' ] . " , " . $ _GET [ ' payment_id ' ] . " , " . $ _GET [ ' testnet ' ] );
341
+ error_log ("callback received: " . $ order_id . " , " . $ payment_id . " , " . $ isTestnet );
333
342
334
343
// make sure this is a numberic value.
335
344
if (is_numeric ($ order_id )) {
336
345
337
346
/* Check payment address exist in database */
338
- $ db_data = $ wpdb ->get_row ('SELECT * FROM wp_paybyte_payment where order_id=" ' .$ order_id .'" ' , OBJECT , 0 );
347
+ $ db_data = $ wpdb ->get_row ('SELECT * FROM paybyte_payment where order_id=" ' .$ order_id .'" ' , OBJECT , 0 );
339
348
$ db_data = json_decode (json_encode ($ db_data ), True );
340
349
$ db_payment_address = $ db_data ['payment_address ' ];
341
350
$ db_payment_id = $ db_data ['payment_id ' ];
@@ -357,7 +366,7 @@ function handle_callback() {
357
366
$ update_payment_status = $ payment_json_response ['transaction ' ]['status ' ];
358
367
$ amount_received = $ payment_json_response ['transaction ' ]['amount-received ' ];
359
368
360
- $ db_update_status = $ wpdb ->update ('wp_paybyte_payment ' , array ('status ' => $ update_payment_status ,'amount_received ' =>$ amount_received ), array ('payment_address ' => $ db_payment_address ));
369
+ $ db_update_status = $ wpdb ->update ('paybyte_payment ' , array ('status ' => $ update_payment_status ,'amount_received ' =>$ amount_received ), array ('payment_address ' => $ db_payment_address ));
361
370
362
371
switch ($ update_payment_status ) {
363
372
case 'payment_received ' :
@@ -379,42 +388,38 @@ function handle_callback() {
379
388
}
380
389
}
381
390
}
382
-
383
391
}
384
-
385
392
}// end of WC_Gateway_PayByte class
386
393
387
394
/*
388
395
* Enqueue js and css
389
396
*/
390
- function js_added_to_the_head () {
397
+ function paybyte_js_added_to_the_head () {
391
398
392
399
wp_enqueue_script ('jquery ' ); // Enqueue standard jquery file
393
400
wp_register_script ( 'add-bx-custom-js ' , plugins_url ('js/custom.js ' , __FILE__ ), '' , null ,'' );
394
401
wp_enqueue_script ( 'add-bx-custom-js ' );
395
402
wp_register_style ('my_stylesheet ' , plugins_url ('css/sgg_style.css ' , __FILE__ ));
396
403
wp_enqueue_style ('my_stylesheet ' );
397
404
}
398
- add_action ( 'wp_enqueue_scripts ' , 'js_added_to_the_head ' );
399
- /* Ends here */
400
-
405
+ add_action ( 'wp_enqueue_scripts ' , 'paybyte_js_added_to_the_head ' );
401
406
402
407
/*
403
408
* Add coin total row on thankyou page
404
409
*/
405
- add_filter ( 'woocommerce_get_order_item_totals ' , 'add_custom_order_totals_row ' , 30 , 3 );
406
- function add_custom_order_totals_row ( $ total_rows , $ order ) {
410
+ add_filter ( 'woocommerce_get_order_item_totals ' , 'paybyte_add_custom_order_totals_row ' , 30 , 3 );
411
+ function paybyte_add_custom_order_totals_row ( $ total_rows , $ order ) {
407
412
408
413
$ order_id = $ order ->get_order_number ();
409
- $ get_coin_total = get_post_meta ($ order_id );
410
- $ coin_total = $ get_coin_total [ ' coin_total ' ][ 0 ] ;
414
+ $ coinAmount = get_post_meta ( $ order_id, ' paybyte_coin_total ' , true );
415
+ $ coinName = get_post_meta ( $ order_id , ' paybyte_coin_name ' , true ) ;
411
416
412
- $ txt = $ this -> icon . ' total : ' ;
417
+ $ txt = $ coinName . ' Total : ' ;
413
418
414
419
// Insert a new row
415
420
$ total_rows ['recurr_not ' ] = array (
416
- "label " => __ ( $ txt, ' woocommerce ' ) ,
417
- "value " => $ coin_total ,
421
+ "label " => $ txt ,
422
+ "value " => $ coinAmount ,
418
423
);
419
424
420
425
return $ total_rows ;
0 commit comments