-
Notifications
You must be signed in to change notification settings - Fork 0
/
tokenpay.php
246 lines (210 loc) · 10.8 KB
/
tokenpay.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
<?php
/*
Plugin Name: WooCommerce Payment Gateway - OrdinalsBot
Plugin URI: https://ordinalsbot.com
Description: Accept Bitcoin Instantly via OrdinalsBot
Version: 0.0.3
Author: OrdinalsBot
Author URI: https://ordinalsbot.com
*/
add_action('plugins_loaded', 'ordinalsbot_init');
define('ORDINALSBOT_WOOCOMMERCE_VERSION', '0.0.3');
define('ORDINALSBOT_CHECKOUT_PATH', 'https://ordinalsbot.com/tokenpay/checkout/');
function ordinalsbot_init()
{
if (!class_exists('WC_Payment_Gateway')) {
return;
};
define('PLUGIN_DIR', plugins_url(basename(plugin_dir_path(__FILE__)), basename(__FILE__)) . '/');
require_once(__DIR__ . '/lib/ordinalsbot/init.php');
class WC_Gateway_OrdinalsBot extends WC_Payment_Gateway
{
public function __construct()
{
global $woocommerce;
$this->id = 'ordinalsbot';
$this->has_fields = false;
$this->method_title = 'OrdinalsBot';
$this->init_form_fields();
$this->init_settings();
$this->title = $this->get_option('title');
$this->description = $this->get_option('description');
$this->api_secret = $this->get_option('api_secret');
$this->api_auth_token = (empty($this->get_option('api_auth_token')) ? $this->get_option('api_secret') : $this->get_option('api_auth_token'));
$this->checkout_url = $this->get_option('checkout_url');
add_action('woocommerce_update_options_payment_gateways_' . $this->id, array($this, 'process_admin_options'));
add_action('woocommerce_thankyou_ordinalsbot', array($this, 'thankyou'));
add_action('woocommerce_api_wc_gateway_ordinalsbot', array($this, 'payment_callback'));
}
public function admin_options()
{
?>
<h3><?php _e('OrdinalsBot', 'woothemes'); ?></h3>
<p><?php _e('Accept Bitcoin instantly through OrdinalsBot.com.', 'woothemes'); ?></p>
<table class="form-table">
<?php $this->generate_settings_html(); ?>
</table>
<?php
}
public function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => __('Enable OrdinalsBot', 'woocommerce'),
'label' => __('Enable Bitcoin payments via OrdinalsBot', 'woocommerce'),
'type' => 'checkbox',
'description' => '',
'default' => 'no',
),
'title' => array(
'title' => __('Title', 'woocommerce'),
'type' => 'text',
'description' => __('The payment method title which a customer sees at the checkout of your store.', 'woocommerce'),
'default' => __('Tokenpay: on-chain bitcoin and runes', 'woocommerce'),
),
'description' => array(
'title' => __('Description', 'woocommerce'),
'type' => 'textarea',
'description' => __('The payment method description which a customer sees at the checkout of your store.', 'woocommerce'),
'default' => __('Powered by OrdinalsBot'),
),
'api_auth_token' => array(
'title' => __('API Auth Token', 'woocommerce'),
'type' => 'text',
'description' => __('Your personal API Key. Generate one <a href="https://docs.ordinalsbot.com" target="_blank">here</a>. ', 'woocommerce'),
'default' => (empty($this->get_option('api_secret')) ? '' : $this->get_option('api_secret')),
),
'checkout_url' => array(
'title' => __('Checkout URL', 'woocommerce'),
'description' => __('URL for the checkout', 'woocommerce'),
'type' => 'text',
'default' => ORDINALSBOT_CHECKOUT_PATH,
),
);
}
public function thankyou()
{
if ($description = $this->get_description()) {
echo wpautop(wptexturize($description));
}
}
public function process_payment($order_id)
{
$order = wc_get_order($order_id);
$this->init_ordinalsbot();
$ordinalsbot_order_id = get_post_meta($order->get_id(), 'ordinalsbot_order_id', true);
if (empty($ordinalsbot_order_id)) {
// send a request to https://api.ordinalsbot.com/fxrate to get dogusd fx rate=
$fxrate_response = wp_remote_get('https://api.ordinalsbot.com/fxrate');
if (is_wp_error($fxrate_response)) {
error_log('Failed to get fx rate: ' . $fxrate_response->get_error_message());
return;
}
$fxrate_body = wp_remote_retrieve_body($fxrate_response);
$fxrate_data = json_decode($fxrate_body, true);
if (!isset($fxrate_data['dog']['usd']) || empty($fxrate_data['dog']['usd'])) {
error_log('Invalid fx rate response: missing DOG to USD rate');
return;
}
// Calculate price in DOG using the retrieved fx rate
$dogusd_rate = $fxrate_data['dog']['usd'];
$usd_price = $order->get_total();
$dog_price = $usd_price / $dogusd_rate;
$dog_price = ceil($dog_price * 1.1);
error_log('total price in $DOG ' . $dog_price);
// Prepare tokenpayParams
$tokenpayParams = array(
'amount' => $dog_price,
'token' => 'DOG•GO•TO•THE•MOON',
// 'additionalFee' => 100, // this is optional and can be added later
'description' => 'WooCommerce - #' . $order->get_id(),
'order_id' => $order->get_id(),
'name' => $order->get_formatted_billing_full_name(),
'email' => $order->get_billing_email(),
'webhookUrl' => trailingslashit(get_bloginfo('wpurl')) . '?wc-api=wc_gateway_ordinalsbot&order_id=' . $order->get_id(),
'successUrl' => $order->get_checkout_order_received_url(),
);
$ordinalsbot_order = \OrdinalsBot\Merchant\Order::create($tokenpayParams);
$ordinalsbot_order_id = $ordinalsbot_order->id;
error_log('OrdinalsBot Order ID: ' . $ordinalsbot_order_id);
error_log('local order ID: ' . $order->get_id());
update_post_meta($order_id, 'ordinalsbot_order_id', $ordinalsbot_order_id);
return array(
'result' => 'success',
'redirect' => $this->checkout_url . $ordinalsbot_order_id,
);
}
else {
return array(
'result' => 'success',
'redirect' => $this->checkout_url . $ordinalsbot_order_id,
);
}
}
public function payment_callback()
{
$request = $_REQUEST;
$order = wc_get_order($request['order_id']);
// error_log('Order: ' . print_r($order, true));
try {
if (!$order || !$order->get_id()) {
throw new Exception('Order #' . $request['order_id'] . ' does not exists');
}
$token = get_post_meta($order->get_id(), 'ordinalsbot_order_id', true);
error_log('OrdinalsBot ID: ' . $token);
if (empty($token) ) {
throw new Exception('Order has no OrdinalsBot ID associated');
}
$request2 = json_decode(file_get_contents('php://input'), true);
error_log('OrdinalsBot Callback request2: ' . print_r($request2, true));
$webhookSecretToken = wc_get_order($request2['webhookSecretToken']);
error_log('webhookSecretToken: ' . $webhookSecretToken);
$this->init_ordinalsbot();
$cgOrder = \OrdinalsBot\Merchant\Order::find($token);
if (!$cgOrder) {
throw new Exception('OrdinalsBot Order #' . $order->get_id() . ' does not exists');
}
error_log('OrdinalsBot Order ->state: ' . print_r($cgOrder->state, true));
switch ($cgOrder->state) {
case 'completed':
error_log('Payment completed');
$statusWas = "wc-" . $order->get_status();
$order->add_order_note(__('Payment is settled and has been credited to your OrdinalsBot account. Purchased goods/services can be securely delivered to the customer.', 'ordinalsbot'));
$order->payment_complete();
// error_log('Order $statusWas: ' . $statusWas);
error_log('Order status: ' . $order->get_status());
if ($order->get_status() === 'processing' && ($statusWas === 'wc-expired' || $statusWas === 'wc-canceled')) {
WC()->mailer()->emails['WC_Email_Customer_Processing_Order']->trigger($order->get_id());
}
if (($order->get_status() === 'processing' || $order->get_status() == 'completed') && ($statusWas === 'wc-expired' || $statusWas === 'wc-canceled')) {
WC()->mailer()->emails['WC_Email_New_Order']->trigger($order->get_id());
}
break;
case 'error':
error_log('Payment failed');
$order->add_order_note(__('Payment failed', 'ordinalsbot'));
$order->update_status('cancelled');
break;
}
} catch (Exception $e) {
die(get_class($e) . ': ' . $e->getMessage());
}
}
private function init_ordinalsbot()
{
\OrdinalsBot\OrdinalsBot::config(
array(
'auth_token' => (empty($this->api_auth_token) ? $this->api_secret : $this->api_auth_token),
'environment' => 'live',
'user_agent' => ('OrdinalsBot - WooCommerce v' . WOOCOMMERCE_VERSION . ' Plugin v' . ORDINALSBOT_WOOCOMMERCE_VERSION)
)
);
}
}
function add_ordinalsbot_gateway($methods)
{
$methods[] = 'WC_Gateway_OrdinalsBot';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_ordinalsbot_gateway');
}