1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Stripe Payment Request API |
4
|
|
|
* |
5
|
|
|
* @package WooCommerce_Stripe/Classes/Payment_Request |
6
|
|
|
* @since 3.1.0 |
7
|
|
|
* @version 3.1.0 |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
11
|
|
|
exit; |
12
|
|
|
} |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* WC_Stripe_Payment_Request class. |
16
|
|
|
*/ |
17
|
|
|
class WC_Stripe_Payment_Request { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Initialize class actions. |
21
|
|
|
*/ |
22
|
|
|
public function __construct() { |
23
|
|
|
add_action( 'wp_enqueue_scripts', array( $this, 'scripts' ) ); |
24
|
|
|
|
25
|
|
|
add_action( 'wc_ajax_wc_stripe_get_cart_details', array( $this, 'ajax_get_cart_details' ) ); |
26
|
|
|
add_action( 'wc_ajax_wc_stripe_get_shipping_options', array( $this, 'ajax_get_shipping_options' ) ); |
27
|
|
|
add_action( 'wc_ajax_wc_stripe_update_shipping_method', array( $this, 'ajax_update_shipping_method' ) ); |
28
|
|
|
add_action( 'wc_ajax_wc_stripe_create_order', array( $this, 'ajax_create_order' ) ); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Check if Stripe gateway is enabled. |
33
|
|
|
* |
34
|
|
|
* @return bool |
35
|
|
|
*/ |
36
|
|
|
protected function is_activated() { |
37
|
|
|
$options = get_option( 'woocommerce_stripe_settings', array() ); |
38
|
|
|
$enabled = isset( $options['enabled'] ) && 'yes' === $options['enabled']; |
39
|
|
|
$stripe_checkout = isset( $options['stripe_checkout'] ) && 'yes' !== $options['stripe_checkout']; |
40
|
|
|
$request_payment_api = isset( $options['request_payment_api'] ) && 'yes' === $options['request_payment_api']; |
41
|
|
|
|
42
|
|
|
return $enabled && $stripe_checkout && $request_payment_api && is_ssl(); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Get publishable key. |
47
|
|
|
* |
48
|
|
|
* @return string |
49
|
|
|
*/ |
50
|
|
|
protected function get_publishable_key() { |
51
|
|
|
$options = get_option( 'woocommerce_stripe_settings', array() ); |
52
|
|
|
|
53
|
|
|
if ( empty( $options ) ) { |
54
|
|
|
return ''; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
return 'yes' === $options['testmode'] ? $options['test_publishable_key'] : $options['publishable_key']; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Load public scripts. |
62
|
|
|
*/ |
63
|
|
|
public function scripts() { |
64
|
|
|
// Load PaymentRequest only on cart for now. |
65
|
|
|
if ( ! is_cart() ) { |
66
|
|
|
return; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
if ( ! $this->is_activated() ) { |
70
|
|
|
return; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
$suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
74
|
|
|
|
75
|
|
|
wp_enqueue_script( 'stripe', 'https://js.stripe.com/v2/', '', '1.0', true ); |
76
|
|
|
wp_enqueue_script( 'wc-stripe-payment-request', plugins_url( 'assets/js/payment-request' . $suffix . '.js', WC_STRIPE_MAIN_FILE ), array( 'jquery', 'stripe' ), WC_STRIPE_VERSION, true ); |
77
|
|
|
|
78
|
|
|
wp_localize_script( |
79
|
|
|
'wc-stripe-payment-request', |
80
|
|
|
'wcStripePaymentRequestParams', |
81
|
|
|
array( |
82
|
|
|
'ajax_url' => WC_AJAX::get_endpoint( '%%endpoint%%' ), |
83
|
|
|
'stripe' => array( |
84
|
|
|
'key' => $this->get_publishable_key(), |
85
|
|
|
'allow_prepaid_card' => apply_filters( 'wc_stripe_allow_prepaid_card', true ) ? 'yes' : 'no', |
86
|
|
|
), |
87
|
|
|
'nonce' => array( |
88
|
|
|
'payment' => wp_create_nonce( 'wc-stripe-payment-request' ), |
89
|
|
|
'shipping' => wp_create_nonce( 'wc-stripe-payment-request-shipping' ), |
90
|
|
|
'update_shipping' => wp_create_nonce( 'wc-stripe-update-shipping-method' ), |
91
|
|
|
'checkout' => wp_create_nonce( 'woocommerce-process_checkout' ), |
92
|
|
|
), |
93
|
|
|
'i18n' => array( |
94
|
|
|
'no_prepaid_card' => __( 'Sorry, we\'re not accepting prepaid cards at this time.', 'woocommerce-gateway-stripe' ), |
95
|
|
|
/* translators: Do not translate the [option] placeholder */ |
96
|
|
|
'unknown_shipping' => __( 'Unknown shipping option "[option]".', 'woocommerce-gateway-stripe' ), |
97
|
|
|
), |
98
|
|
|
) |
99
|
|
|
); |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get cart details. |
104
|
|
|
*/ |
105
|
|
|
public function ajax_get_cart_details() { |
106
|
|
|
check_ajax_referer( 'wc-stripe-payment-request', 'security' ); |
107
|
|
|
|
108
|
|
|
if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
109
|
|
|
define( 'WOOCOMMERCE_CART', true ); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
WC()->cart->calculate_totals(); |
113
|
|
|
|
114
|
|
|
$currency = get_woocommerce_currency(); |
115
|
|
|
|
116
|
|
|
// Set mandatory payment details. |
117
|
|
|
$data = array( |
118
|
|
|
'shipping_required' => WC()->cart->needs_shipping(), |
119
|
|
|
'order_data' => array( |
120
|
|
|
'total' => array( |
121
|
|
|
'label' => __( 'Total', 'woocommerce-gateway-stripe' ), |
122
|
|
|
'amount' => array( |
123
|
|
|
'value' => max( 0, apply_filters( 'woocommerce_calculated_total', round( WC()->cart->cart_contents_total + WC()->cart->fee_total, WC()->cart->dp ), WC()->cart ) ), |
124
|
|
|
'currency' => $currency, |
125
|
|
|
), |
126
|
|
|
), |
127
|
|
|
), |
128
|
|
|
); |
129
|
|
|
|
130
|
|
|
wp_send_json( $data ); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Get shipping options. |
135
|
|
|
* |
136
|
|
|
* @see WC_Cart::get_shipping_packages(). |
137
|
|
|
* @see WC_Shipping::calculate_shipping(). |
138
|
|
|
* @see WC_Shipping::get_packages(). |
139
|
|
|
*/ |
140
|
|
|
public function ajax_get_shipping_options() { |
141
|
|
|
check_ajax_referer( 'wc-stripe-payment-request-shipping', 'security' ); |
142
|
|
|
|
143
|
|
|
// Set the shipping package. |
144
|
|
|
$posted = filter_input_array( INPUT_POST, array( |
145
|
|
|
'country' => FILTER_SANITIZE_ENCODED, |
146
|
|
|
'state' => FILTER_SANITIZE_STRING, |
147
|
|
|
'postcode' => FILTER_SANITIZE_ENCODED, |
148
|
|
|
'city' => FILTER_SANITIZE_STRING, |
149
|
|
|
'address' => FILTER_SANITIZE_STRING, |
150
|
|
|
'address_2' => FILTER_SANITIZE_STRING, |
151
|
|
|
) ); |
152
|
|
|
$packages = array(); |
153
|
|
|
|
154
|
|
|
$packages[0]['contents'] = WC()->cart->get_cart(); |
155
|
|
|
$packages[0]['contents_cost'] = 0; |
156
|
|
|
$packages[0]['applied_coupons'] = WC()->cart->applied_coupons; |
157
|
|
|
$packages[0]['user']['ID'] = get_current_user_id(); |
158
|
|
|
$packages[0]['destination']['country'] = $posted['country']; |
159
|
|
|
$packages[0]['destination']['state'] = $posted['state']; |
160
|
|
|
$packages[0]['destination']['postcode'] = $posted['postcode']; |
161
|
|
|
$packages[0]['destination']['city'] = $posted['city']; |
162
|
|
|
$packages[0]['destination']['address'] = $posted['address']; |
163
|
|
|
$packages[0]['destination']['address_2'] = $posted['address_2']; |
164
|
|
|
|
165
|
|
|
foreach ( WC()->cart->get_cart() as $item ) { |
166
|
|
|
if ( $item['data']->needs_shipping() ) { |
167
|
|
|
if ( isset( $item['line_total'] ) ) { |
168
|
|
|
$packages[0]['contents_cost'] += $item['line_total']; |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
$packages = apply_filters( 'woocommerce_cart_shipping_packages', $packages ); |
174
|
|
|
|
175
|
|
|
WC()->shipping->calculate_shipping( $packages ); |
176
|
|
|
|
177
|
|
|
// Set the shipping options. |
178
|
|
|
$currency = get_woocommerce_currency(); |
179
|
|
|
$data = array(); |
180
|
|
|
foreach ( WC()->shipping->get_packages() as $package_key => $package ) { |
181
|
|
|
foreach ( $package['rates'] as $key => $rate ) { |
182
|
|
|
$data[] = array( |
183
|
|
|
'id' => $rate->id, |
184
|
|
|
'label' => $rate->label, |
185
|
|
|
'amount' => array( |
186
|
|
|
'currency' => $currency, |
187
|
|
|
'value' => $rate->cost, |
188
|
|
|
), |
189
|
|
|
'selected' => false, |
190
|
|
|
); |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
// Auto select when have only one shipping method available. |
195
|
|
|
if ( 1 === count( $data ) ) { |
196
|
|
|
$data[0]['selected'] = true; |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
wp_send_json( $data ); |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Update shipping method. |
204
|
|
|
*/ |
205
|
|
|
public function ajax_update_shipping_method() { |
206
|
|
|
check_ajax_referer( 'wc-stripe-update-shipping-method', 'security' ); |
207
|
|
|
|
208
|
|
|
if ( ! defined( 'WOOCOMMERCE_CART' ) ) { |
209
|
|
|
define( 'WOOCOMMERCE_CART', true ); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
$chosen_shipping_methods = WC()->session->get( 'chosen_shipping_methods' ); |
213
|
|
|
$shipping_method = filter_input( INPUT_POST, 'shipping_method', FILTER_DEFAULT, FILTER_REQUIRE_ARRAY ); |
214
|
|
|
|
215
|
|
|
if ( is_array( $shipping_method ) ) { |
216
|
|
|
foreach ( $shipping_method as $i => $value ) { |
217
|
|
|
$chosen_shipping_methods[ $i ] = wc_clean( $value ); |
218
|
|
|
} |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
WC()->session->set( 'chosen_shipping_methods', $chosen_shipping_methods ); |
222
|
|
|
|
223
|
|
|
WC()->cart->calculate_totals(); |
224
|
|
|
|
225
|
|
|
// Send back the new cart total. |
226
|
|
|
$currency = get_woocommerce_currency(); |
227
|
|
|
$tax_total = max( 0, round( WC()->cart->tax_total + WC()->cart->shipping_tax_total, WC()->cart->dp ) ); |
228
|
|
|
$data = array( |
229
|
|
|
'total' => WC()->cart->total, |
230
|
|
|
); |
231
|
|
|
|
232
|
|
|
// Include fees and taxes as displayItems. |
233
|
|
|
foreach ( WC()->cart->fees as $key => $fee ) { |
234
|
|
|
$data['items'][] = array( |
235
|
|
|
'label' => $fee->name, |
236
|
|
|
'amount' => array( |
237
|
|
|
'currency' => $currency, |
238
|
|
|
'value' => $fee->amount, |
239
|
|
|
), |
240
|
|
|
); |
241
|
|
|
} |
242
|
|
|
if ( 0 < $tax_total ) { |
243
|
|
|
$data['items'][] = array( |
244
|
|
|
'label' => __( 'Tax', 'woocommerce-gateway-stripe' ), |
245
|
|
|
'amount' => array( |
246
|
|
|
'currency' => $currency, |
247
|
|
|
'value' => $tax_total, |
248
|
|
|
), |
249
|
|
|
); |
250
|
|
|
} |
251
|
|
|
|
252
|
|
|
wp_send_json( $data ); |
253
|
|
|
} |
254
|
|
|
|
255
|
|
|
/** |
256
|
|
|
* Create order. |
257
|
|
|
*/ |
258
|
|
|
public function ajax_create_order() { |
259
|
|
|
if ( WC()->cart->is_empty() ) { |
260
|
|
|
wp_send_json_error( __( 'Empty cart', 'woocommerce-gateway-stripe' ) ); |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
if ( ! defined( 'WOOCOMMERCE_CHECKOUT' ) ) { |
264
|
|
|
define( 'WOOCOMMERCE_CHECKOUT', true ); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
WC()->checkout()->process_checkout(); |
268
|
|
|
|
269
|
|
|
die( 0 ); |
270
|
|
|
} |
271
|
|
|
} |
272
|
|
|
|
273
|
|
|
new WC_Stripe_Payment_Request(); |
274
|
|
|
|