-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathxprcheckout.php
184 lines (144 loc) · 6.14 KB
/
xprcheckout.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
<?php
/*
* Plugin Name: XPRCheckout - WebAuth Gateway for e-commerce
* Description: Allow user to pay securely with with multiple crypto currencies through WebAuth
* Author: Metallicus Team
* Author URI: https://www.metallicus.com/
* Version: ##VERSION_TAG##
* slug: xprcheckout-webauth-gateway-for-e-commerce
* Text Domain: xprcheckout-webauth-gateway-for-e-commerce
* Domain Path: /i18n/languages/
* License: GPLv2 or later
* Requires at least: 6.0
* Requires PHP: 7.0
* Requires Plugins: woocommerce
*/
define('XPRCHECKOUT_VERSION', '##VERSION_TAG##');
define('XPRCHECKOUT_ROOT_DIR', plugin_dir_path(__FILE__));
define('XPRCHECKOUT_ROOT_URL', plugin_dir_url(__FILE__));
define('XPRCHECKOUT_MAINNET', 'mainnet');
define('XPRCHECKOUT_TESTNET', 'testnet');
define('XPRCHECKOUT_MAINNET_ENDPOINT', 'https://api.rockerone.io');
define('XPRCHECKOUT_TESTNET_ENDPOINT', 'https://testnet.rockerone.io');
define('XPRCHECKOUT_MAINNET_BLOCK_EXPLORER', 'https://explorer.xprnetwork.org');
define('XPRCHECKOUT_TESTNET_BLOCK_EXPLORER', 'https://testnet.explorer.xprnetwork.org');
define('XPRCHECKOUT_MAINNET_CHAIN_ID', '384da888112027f0321850a169f737c33e53b388aad48b5adace4bab97f437e0');
define('XPRCHECKOUT_TESTNET_CHAIN_ID', '71ee83bcf52142d61019d95f9cc5427ba6a0d7ff8accd9e2088ae2abeaf3d3dd');
define('XPRCHECKOUT_PRICE_RATE_API_ENDPOINT', 'https://api.freecurrencyapi.com/v1/latest');
define('XPRCHECKOUT_PRICE_RATE_API_KEY', 'fca_live_10WmLkCu6Xdz9WV8zmkXAuCAkrrbkEoMPG6gaMcu');
define('XPRCHECKOUT_TABLE_TOKEN_RATES', 'token_rates');
define('XPRCHECKOUT_TABLE_FIAT_RATES', 'fiat_rates');
define('XPRCHECKOUT_CHECKOUT_APP_HANDLE', 'xprcheckout_public');
define('XPRCHECKOUT_REFUND_APP_HANDLE', 'xprcheckout_admin_refund');
define('XPRCHECKOUT_REGSTORE_APP_HANDLE', 'xprcheckout_admin_regstore');
function xprcheckout_install(){
global $wpdb;
global $jal_db_version;
$tokenTableName = $wpdb->prefix . XPRCHECKOUT_TABLE_TOKEN_RATES;
$fiatTableName = $wpdb->prefix . XPRCHECKOUT_TABLE_FIAT_RATES;
$charset_collate = $wpdb->get_charset_collate();
$tokenRatesSql = "CREATE TABLE $tokenTableName (
symbol tinytext NOT NULL,
contract text NOT NULL,
token_precision int DEFAULT 0 NOT NULL,
rate float DEFAULT 0 NOT NULL,
updated datetime DEFAULT NOW() NOT NULL,
PRIMARY KEY (symbol(12))
) $charset_collate;";
$fiatRatesSql = "CREATE TABLE $fiatTableName (
symbol tinytext NOT NULL,
rate float DEFAULT 0 NOT NULL,
updated datetime DEFAULT NOW() NOT NULL,
PRIMARY KEY (symbol(12))
) $charset_collate;";
require_once ABSPATH . 'wp-admin/includes/upgrade.php';
dbDelta( $tokenRatesSql );
dbDelta( $fiatRatesSql );
add_option( 'xprcheckout_db_version', XPRCHECKOUT_VERSION );
}
register_activation_hook( __FILE__, 'xprcheckout_install' );
include_once XPRCHECKOUT_ROOT_DIR . '/includes/xprcheckout-gateway.core.php';
function xprcheckout_gateway_start()
{
if ( class_exists( 'WooCommerce' ) ) {
$plugin = new XPRCheckout_WCGateway();
$plugin->run();
}else {
add_action( 'admin_notices', 'xprcheckout_admin_notice_wc_required' );
}
}
function xprcheckout_admin_notice_wc_required() {
?>
<div class="notice notice-error">
<p><b><?php esc_html_e( 'XPRCheckout - Webauth Gateway for Woocommerce require WooCommerce to work!', 'xprcheckout-webauth-gateway-for-e-commerce' ); ?></b></p>
<a href="/wp-admin/plugin-install.php?s=woo&tab=search&type=term">Install Woocommerce </a>
<p></p>
</div>
<?php
}
function xprcheckout_register_endpoint (){
global $wp_rewrite;
add_rewrite_endpoint('xprcheckout', EP_ROOT, 'xprcheckout' );
add_rewrite_endpoint('payments', EP_PERMALINK, 'paymentKey' );
add_rewrite_rule(
'xprcheckout/payments/(([a-z0-9])*)/?$',
'index.php?xprcheckout=payments&paymentKey=$matches[1]',
'top'
);
$wp_rewrite->flush_rules(true);
}
add_action( 'init', 'xprcheckout_register_endpoint' );
function xprcheckout_register_query_vars($vars){
$vars[] = 'paymentKey';
return $vars;
}
add_filter( 'query_vars', 'xprcheckout_register_query_vars' );
function xprcheckout_template_redirect ($template){
global $wp_query;
$mutatedTemplate = $template;
if (isset($wp_query->query_vars['xprcheckout'])){
if($wp_query->query_vars['xprcheckout'] == "payments"){
$filePath = XPRCHECKOUT_ROOT_DIR.'includes/templates/template-payments.php';
$fileEx = file_exists($filePath);
if ($fileEx){
$mutatedTemplate = $filePath;
}
}
}
return $mutatedTemplate;
}
add_filter( 'template_include', 'xprcheckout_template_redirect',99 );
function xprcheckout_redirect_to_payment (){
global $wp_query;
$paymentGateway = WC()->payment_gateways->payment_gateways()['xprcheckout'];
if( is_wc_endpoint_url( 'order-received' )) {
if(isset($wp_query->query_vars['order-received'])){
$orderId = $wp_query->query_vars['order-received'];
$order = wc_get_order($orderId);
$paymentKey = $order->get_meta('_payment_key');
if ($order->get_payment_method() == "xprcheckout"){
if (empty($paymentKey)){
$serializedOrder = wp_json_encode($order);
$paymentKey = hash('sha256', $serializedOrder . time());
$order->update_meta_data('_payment_key', $paymentKey);
}
$order->update_meta_data('_network',$paymentGateway->get_option('network'));
$order->set_status('pending');
$order->set_date_modified( time() );
$order->save();
wp_redirect(home_url('/xprcheckout/payments/'.$paymentKey));
exit;
}
}
}
}
add_action( 'template_redirect', 'xprcheckout_redirect_to_payment' );
add_action( 'woocommerce_blocks_loaded', 'xprcheckout_webauth_gateway_block_support');
function xprcheckout_webauth_gateway_block_support(){
require_once XPRCHECKOUT_ROOT_DIR . 'includes/supports/block-support.php';
}
add_action( 'woocommerce_blocks_payment_method_type_registration', 'xprcheckout_webauth_gateway_block_method_type_registration');
function xprcheckout_webauth_gateway_block_method_type_registration ($payment_method_registry){
$payment_method_registry->register( new XPRCheckout_BlocksSupport() );
}
xprcheckout_gateway_start();