forked from jaafit/bitpayWoocommerce
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckout.php
213 lines (180 loc) · 6.84 KB
/
checkout.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
<?php
/*
Plugin Name: Bitpay Woocommerce
Plugin URI: http://www.bitpay.com
Description: This plugin adds the Bitpay payment gateway to your Woocommerce plugin. Woocommerce is required.
Version: 1.0
Author: Japhet Stevens
Author URI: http://www.bitpay.com
License:
*/
/**
* Check if WooCommerce is active
**/
if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) )
{
function bplog($contents)
{
$file = plugin_dir_path(__FILE__).'bplog.txt';
file_put_contents($file, date('m-d H:i:s').": ", FILE_APPEND);
if (is_array($contents))
file_put_contents($file, var_export($contents, true)."\n", FILE_APPEND);
else if (is_object($contents))
file_put_contents($file, json_encode($contents)."\n", FILE_APPEND);
else
file_put_contents($file, $contents."\n", FILE_APPEND);
}
function declareWooBitpay()
{
if ( ! class_exists( 'WC_Payment_Gateways' ) )
return;
class WC_Bitpay extends WC_Payment_Gateway
{
public function __construct()
{
$this->id = 'bitpay';
$this->icon = plugin_dir_url(__FILE__).'bitpay.png';
$this->has_fields = false;
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
// Define user set variables
$this->title = $this->settings['title'];
$this->description = $this->settings['description'];
// Actions
add_action('woocommerce_update_options_payment_gateways', array(&$this, 'process_admin_options'));
//add_action('woocommerce_thankyou_cheque', array(&$this, 'thankyou_page'));
// Customer Emails
add_action('woocommerce_email_before_order_table', array(&$this, 'email_instructions'), 10, 2);
}
function init_form_fields()
{
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woothemes' ),
'type' => 'checkbox',
'label' => __( 'Enable Bitpay Payment', 'woothemes' ),
'default' => 'yes'
),
'title' => array(
'title' => __( 'Title', 'woothemes' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woothemes' ),
'default' => __( 'Bitcoins', 'woothemes' )
),
'description' => array(
'title' => __( 'Customer Message', 'woothemes' ),
'type' => 'textarea',
'description' => __( 'Message to explain how the customer will be paying for the purchase.', 'woothemes' ),
'default' => 'You will be redirected to bitpay.com to complete your purchase.'
),
'apiKey' => array(
'title' => __('API Key', 'woothemes'),
'type' => 'text',
'description' => __('Enter the API key you created at bitpay.com'),
),
'transactionSpeed' => array(
'title' => __('Transaction Speed', 'woothemes'),
'type' => 'select',
'description' => 'Choose a transaction speed. For details, see the API documentation at bitpay.com',
'options' => array(
'high' => 'High',
'medium' => 'Medium',
'low' => 'Low',
),
'default' => 'high',
),
'fullNotifications' => array(
'title' => __('Full Notifications', 'woothemes'),
'type' => 'checkbox',
'description' => 'Yes: receive an email for each status update on a payment. No: receive an email only when payment is confirmed.',
'default' => 'no',
),
'fbaEnabled' => array(
'title' => __('Fullfullment By Amazon Enabled', 'woothemes'),
'type' => 'checkbox',
'description' => 'FBA account requred. Fill in account info at ./fba_options.php.',
'default' => 'no',
),
);
}
public function admin_options() {
?>
<h3><?php _e('Bitcoin Payment', 'woothemes'); ?></h3>
<p><?php _e('Allows bitcoin payments via bitpay.com.', 'woothemes'); ?></p>
<table class="form-table">
<?php
// Generate the HTML For the settings form.
$this->generate_settings_html();
?>
</table>
<?php
} // End admin_options()
public function email_instructions( $order, $sent_to_admin ) {
return;
}
function payment_fields() {
if ($this->description) echo wpautop(wptexturize($this->description));
}
function thankyou_page() {
if ($this->description) echo wpautop(wptexturize($this->description));
}
function process_payment( $order_id ) {
require 'bp_lib.php';
global $woocommerce, $wpdb;
$order = &new WC_Order( $order_id );
// Mark as on-hold (we're awaiting the coins)
$order->update_status('on-hold', __('Awaiting payment notification from bitpay.com', 'woothemes'));
// invoice options
$redirect = add_query_arg('key', $order->order_key, add_query_arg('order', $order_id, get_permalink(get_option('woocommerce_thanks_page_id'))));
$notificationURL = get_option('siteurl')."/?bitpay_callback=1";
$currency = get_woocommerce_currency();
$prefix = 'billing_';
$options = array(
'apiKey' => $this->settings['apiKey'],
'transactionSpeed' => $this->settings['transactionSpeed'],
'currency' => $currency,
'redirectURL' => $redirect,
'notificationURL' => $notificationURL,
'fullNotifications' => ($this->settings['fullNotifications'] == 'yes') ? true : false,
'buyerName' => $order->{$prefix.first_name}.' '.$order->{$prefix.last_name},
'buyerAddress1' => $order->{$prefix.address_1},
'buyerAddress2' => $order->{$prefix.address_2},
'buyerCity' => $order->{$prefix.city},
'buyerState' => $order->{$prefix.state},
'buyerZip' => $order->{$prefix.postcode},
'buyerCountry' => $order->{$prefix.country},
'buyerPhone' => $order->billing_phone,
'buyerEmail' => $order->billing_email,
);
if (strlen($order->{$prefix.company}))
$options['buyerName'] = $order->{$prefix.company}.' c/o '.$options['buyerName'];
foreach(array('buyerName', 'buyerAddress1', 'buyerAddress2', 'buyerCity', 'buyerState', 'buyerZip', 'buyerCountry', 'buyerPhone', 'buyerEmail') as $trunc)
$options[$trunc] = substr($options[$trunc], 0, 100); // api specifies max 100-char len
$invoice = bpCreateInvoice($order_id, $order->order_total, $order_id, $options );
if (isset($invoice['error']))
{
bplog($invoice);
$order->add_order_note(var_export($invoice['error']));
$woocommerce->add_error(__('Error creating bitpay invoice. Please try again or try another payment method.'));
}
else
{
$woocommerce->cart->empty_cart();
return array(
'result' => 'success',
'redirect' => $invoice['url'],
);
}
}
}
}
include plugin_dir_path(__FILE__).'callback.php';
function add_bitpay_gateway( $methods ) {
$methods[] = 'WC_Bitpay';
return $methods;
}
add_filter('woocommerce_payment_gateways', 'add_bitpay_gateway' );
add_action('plugins_loaded', 'declareWooBitpay', 0);
}