Skip to content

Commit ca201d1

Browse files
author
izzycoder18
committed
Minor fixes
1 parent 8599b7c commit ca201d1

File tree

4 files changed

+92
-75
lines changed

4 files changed

+92
-75
lines changed

js/custom.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jQuery(function(){
1616

1717

1818
function usingGateway(){
19-
if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'custom'){
19+
if(jQuery('form[name="checkout"] input[name="payment_method"]:checked').val() == 'paybyte'){
2020
var coin_amt = jQuery("#coin_amt").val();
2121
var coin = jQuery("#coin_name").val();
2222
jQuery('.woocommerce-checkout-review-order-table > tfoot:last').append('<tr id="coin_total"><th>' + coin + ' Total</th><td><b>'+coin_amt+'</b></td></tr>');

uninstall.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
* name of table to be dropped
2424
* prefixed with $wpdb->prefix from the database
2525
*/
26-
$table_name = $wpdb->prefix . 'paybyte_payment';
26+
$table_name = 'paybyte_payment';
2727

2828
// drop the table from the database.
2929
$wpdb->query( "DROP TABLE IF EXISTS $table_name" );

woocommerce-paybyte-gateway.php

+65-60
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
/* PaayByte Payment Gateway Class */
3+
/* PayByte Payment Gateway Class */
44
class WC_Gateway_PayByte extends WC_Payment_Gateway {
55

66
public $domain;
@@ -12,13 +12,14 @@ public function __construct() {
1212

1313
$plugin_dir = plugin_dir_url(__FILE__);
1414

15-
$this->domain = 'custom_payment';
15+
$this->domain = 'paybyte';
1616

17-
$this->id = 'custom';
17+
$this->id = 'paybyte';
1818
$this->icon = apply_filters('woocommerce_custom_gateway_icon', $plugin_dir.'/img/paybyte_logo_2.png');
1919
$this->has_fields = false;
20+
$this->order_button_text = __('Proceed to PayByte', $this->domain );
2021
$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 );
2223

2324
// Load the settings.
2425
$this->init_form_fields();
@@ -40,16 +41,16 @@ public function __construct() {
4041
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, array( $this, 'process_admin_options' ) );
4142

4243
// 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' ) );
4445

4546
add_action('woocommerce_api_' . strtolower(get_class($this)), array(
4647
&$this,
47-
'handle_callback'
48+
'paybyte_handle_callback'
4849
));
4950

5051
add_action('woocommerce_thankyou', function($order_id)
5152
{
52-
$coinAmount = get_post_meta( $order_id, 'coin_total', true );
53+
$coinAmount = get_post_meta( $order_id, 'paybyte_coin_total', true );
5354

5455
if (strcmp($coinAmount, "true") == 0) {
5556
return;
@@ -70,11 +71,10 @@ public function __construct() {
7071
</table>
7172
<?php
7273
});
73-
74-
7574
}
76-
7775

76+
public function __destruct(){}
77+
7878
/**
7979
* Build the administration fields for this specific Gateway
8080
*/
@@ -106,10 +106,10 @@ public function init_form_fields() {
106106
'desc_tip' => false,
107107
),
108108
'isTestnet' => array(
109-
'title' => __( 'Testnet Mode' ),
110-
'label' => __( 'Enable Testnet payments' ),
109+
'title' => __( 'Testnet Mode', $this->domain ),
110+
'label' => __( 'Enable Testnet payments', $this->domain ),
111111
'type' => 'checkbox',
112-
'description' => __( 'Place the payment gateway in test mode.'),
112+
'description' => __( 'Place the payment gateway in test mode.', $this->domain ),
113113
'default' => 'no',
114114
),
115115
'description' => array(
@@ -127,21 +127,21 @@ public function init_form_fields() {
127127
'default' => 'BTC',
128128
'desc_tip' => true,
129129
'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' )
138138
)
139139
),
140140
);
141141
}
142142

143143

144-
/* function to display text and BTC attributes on checkout page*/
144+
/* function to display text and crypto attributes on checkout page*/
145145
public function payment_fields(){
146146
global $total_amt;
147147

@@ -152,7 +152,7 @@ public function payment_fields(){
152152

153153
$url = "https://paybyte.io/api/get-rate?currency=".$currency;
154154

155-
// Send this payload to PayByte for processing
155+
// Send this payload to PayByte for processing
156156
$response = wp_remote_get( $url);
157157

158158
// Retrieve the body's resopnse if no errors found
@@ -178,9 +178,12 @@ public function payment_fields(){
178178
* Prepares the create payment URL.
179179
*
180180
* @param WC_Order $customer_order
181+
* @param int $order_id
182+
* @param string $callaback_guid
183+
*
181184
* @return string
182185
*/
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) {
184187

185188
error_log("preparing create payment url..");
186189
$api_key = $this->api_key;
@@ -203,10 +206,10 @@ public function prepare_create_payment_url($customer_order, $order_id, $callabac
203206
* Create a callback secret.
204207
*
205208
* @param WooCommerceOrder $customer_order
206-
* @return
209+
* @return UniqueIdentifier for this callback.
207210
*/
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);
210213
}
211214

212215
/**
@@ -223,9 +226,9 @@ public function process_payment( $order_id ) {
223226
// who to charge and how much
224227
$customer_order = new WC_Order( $order_id );
225228

226-
$callback_guid = $this->create_callback_guid($customer_order);
229+
$callback_guid = $this->paybyte_create_callback_guid($customer_order);
227230

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);
229232

230233
// Send this payload to PayByte for processing
231234
$response = wp_remote_get( $create_payment_url);
@@ -246,28 +249,30 @@ public function process_payment( $order_id ) {
246249
$payment_id = $json_response['transaction']['payment-id'];
247250
$tran_status = $json_response['transaction']['status'];
248251
$total_amt = $json_response['transaction']['amount'];
252+
$coin = $json_response['transaction']['coin'];
249253
$amount_received = $json_response['transaction']['amount-received'];
250254
$received_payment_url = $json_response['transaction']['payment-url'];
251255
$response_error = $json_response['error'];
252256

253257
if ($response_error != "ok") {
254258
wc_add_notice( $response_error, 'error' );
255259
//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);
257261

258262
$customer_order->update_status('failed', __( 'PayByte payment failed', 'woocommerce' ));
259263
}
260264
else {
261265

262266
global $wpdb;
263-
$insert = $wpdb->insert('wp_paybyte_payment',
267+
$insert = $wpdb->insert('paybyte_payment',
264268
array(
265269
'user_id' => get_current_user_id(),
266270
'order_id' => $order_id,
267271
'payment_address' => $payment_address,
268272
'payment_id' => $payment_id,
269273
'status' => $tran_status,
270274
'amount' => $total_amt,
275+
'coin' => $coin,
271276
'amount_received' => $amount_received,
272277
'callback_guid' => $callback_guid,
273278
'created' => date('Y-m-d H:i:s')
@@ -280,15 +285,15 @@ public function process_payment( $order_id ) {
280285
$customer_order->add_order_note( __( 'PayByte payment initiated.' ) );
281286

282287
/* 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 );
284289

285290
/* 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);
287292

288293
/* 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);
290295

291-
$customer_order->update_status('pending', __( 'Awaiting PayByte payment', 'woocommerce' ));
296+
$customer_order->update_status('pending', __( 'Awaiting PayByte payment', 'woocommerce'));
292297

293298
// Reduce stock levels
294299
$customer_order->reduce_order_stock();
@@ -310,32 +315,36 @@ public function process_payment( $order_id ) {
310315
}
311316
}
312317

313-
314318
// Check if we are forcing SSL on checkout pages
315319
// Custom function not required by the Gateway
316-
public function do_ssl_check() {
320+
public function paybyte_do_ssl_check() {
317321
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>";
319323
}
320324
}
321325

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() {
324333

325334
global $wpdb;
326335

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']);
331340

332-
error_log("callback received: " . $_GET['order_id'] . " , " . $_GET['payment_id'] . " , " . $_GET['testnet']);
341+
error_log("callback received: " . $order_id . " , " . $payment_id . " , " . $isTestnet);
333342

334343
// make sure this is a numberic value.
335344
if (is_numeric($order_id)) {
336345

337346
/* 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);
339348
$db_data = json_decode(json_encode($db_data), True);
340349
$db_payment_address = $db_data['payment_address'];
341350
$db_payment_id = $db_data['payment_id'];
@@ -357,7 +366,7 @@ function handle_callback() {
357366
$update_payment_status = $payment_json_response['transaction']['status'];
358367
$amount_received = $payment_json_response['transaction']['amount-received'];
359368

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));
361370

362371
switch ($update_payment_status) {
363372
case 'payment_received':
@@ -379,42 +388,38 @@ function handle_callback() {
379388
}
380389
}
381390
}
382-
383391
}
384-
385392
}// end of WC_Gateway_PayByte class
386393

387394
/*
388395
* Enqueue js and css
389396
*/
390-
function js_added_to_the_head() {
397+
function paybyte_js_added_to_the_head() {
391398

392399
wp_enqueue_script('jquery'); // Enqueue standard jquery file
393400
wp_register_script( 'add-bx-custom-js', plugins_url('js/custom.js', __FILE__), '', null,'' );
394401
wp_enqueue_script( 'add-bx-custom-js' );
395402
wp_register_style('my_stylesheet', plugins_url('css/sgg_style.css', __FILE__));
396403
wp_enqueue_style('my_stylesheet');
397404
}
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' );
401406

402407
/*
403408
* Add coin total row on thankyou page
404409
*/
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) {
407412

408413
$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 );
411416

412-
$txt = $this->icon . ' total:';
417+
$txt = $coinName . ' Total:';
413418

414419
// Insert a new row
415420
$total_rows['recurr_not'] = array(
416-
"label" => __( $txt, 'woocommerce' ),
417-
"value" => $coin_total,
421+
"label" => $txt,
422+
"value" => $coinAmount,
418423
);
419424

420425
return $total_rows;

0 commit comments

Comments
 (0)