trait-wc-stripe-pre-orders.php
7.22 KB
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Trait for Pre-Orders compatibility.
*/
trait WC_Stripe_Pre_Orders_Trait {
/**
* Initialize pre-orders hook.
*
* @since 5.8.0
*/
public function maybe_init_pre_orders() {
if ( ! $this->is_pre_orders_enabled() ) {
return;
}
$this->supports[] = 'pre-orders';
add_action( 'wc_pre_orders_process_pre_order_completion_payment_' . $this->id, [ $this, 'process_pre_order_release_payment' ] );
}
/**
* Checks if pre-orders are enabled on the site.
*
* @since 5.8.0
*
* @return bool
*/
public function is_pre_orders_enabled() {
return class_exists( 'WC_Pre_Orders' );
}
/**
* Is $order_id a pre-order?
*
* @since 5.8.0
*
* @param int $order_id
* @return bool
*/
public function has_pre_order( $order_id ) {
return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Order' ) && WC_Pre_Orders_Order::order_contains_pre_order( $order_id );
}
/**
* Returns boolean on whether current cart contains a pre-order item.
*
* @since 5.8.0
*
* @return bool
*/
public function is_pre_order_item_in_cart() {
return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Cart' ) && WC_Pre_Orders_Cart::cart_contains_pre_order();
}
/**
* Returns pre-order product from cart.
*
* @since 5.8.0
*
* @return object|null
*/
public function get_pre_order_product_from_cart() {
if ( ! $this->is_pre_orders_enabled() || ! class_exists( 'WC_Pre_Orders_Cart' ) ) {
return false;
}
return WC_Pre_Orders_Cart::get_pre_order_product();
}
/**
* Returns pre-order product from order.
*
* @since 5.8.0
*
* @param int $order_id
*
* @return object|null
*/
public function get_pre_order_product_from_order( $order_id ) {
if ( ! $this->is_pre_orders_enabled() || ! class_exists( 'WC_Pre_Orders_Order' ) ) {
return false;
}
return WC_Pre_Orders_Order::get_pre_order_product( $order_id );
}
/**
* Returns boolean on whether product is charged upon release.
*
* @since 5.8.0
*
* @param object $product
*
* @return bool
*/
public function is_pre_order_product_charged_upon_release( $product ) {
return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Product' ) && WC_Pre_Orders_Product::product_is_charged_upon_release( $product );
}
/**
* Returns boolean on whether product is charged upfront.
*
* @since 5.8.0
*
* @param object $product
*
* @return bool
*/
public function is_pre_order_product_charged_upfront( $product ) {
return $this->is_pre_orders_enabled() && class_exists( 'WC_Pre_Orders_Product' ) && WC_Pre_Orders_Product::product_is_charged_upfront( $product );
}
/**
* Checks if we need to process pre-orders when
* a pre-order product is in the cart.
*
* @since 5.8.0
*
* @param int $order_id
*
* @return bool
*/
public function maybe_process_pre_orders( $order_id ) {
return (
$this->has_pre_order( $order_id ) &&
WC_Pre_Orders_Order::order_requires_payment_tokenization( $order_id ) &&
! is_wc_endpoint_url( 'order-pay' )
);
}
/**
* Remove order meta.
*
* @param object $order
*/
public function remove_order_source_before_retry( $order ) {
$order->delete_meta_data( '_stripe_source_id' );
$order->delete_meta_data( '_stripe_card_id' );
$order->save();
}
/**
* Marks the order as pre-ordered.
* The native function is wrapped so we can call it separately and more easily mock it in our tests.
*
* @param object $order
*/
public function mark_order_as_pre_ordered( $order ) {
if ( ! class_exists( 'WC_Pre_Orders_Order' ) ) {
return;
}
WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order );
}
/**
* Process the pre-order when pay upon release is used.
*
* @param int $order_id
*
* @return array
*/
public function process_pre_order( $order_id ) {
try {
$order = wc_get_order( $order_id );
// This will throw exception if not valid.
$this->validate_minimum_order_amount( $order );
$prepared_source = $this->prepare_source( get_current_user_id(), true );
// We need a source on file to continue.
if ( empty( $prepared_source->customer ) || empty( $prepared_source->source ) ) {
throw new WC_Stripe_Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) );
}
// Setup the response early to allow later modifications.
$response = [
'result' => 'success',
'redirect' => $this->get_return_url( $order ),
];
$this->save_source_to_order( $order, $prepared_source );
// Try setting up a payment intent.
$intent_secret = $this->setup_intent( $order, $prepared_source );
if ( ! empty( $intent_secret ) ) {
$response['setup_intent_secret'] = $intent_secret;
return $response;
}
// Remove cart.
WC()->cart->empty_cart();
// Is pre ordered!
$this->mark_order_as_pre_ordered( $order );
// Return thank you page redirect
return $response;
} catch ( WC_Stripe_Exception $e ) {
wc_add_notice( $e->getLocalizedMessage(), 'error' );
WC_Stripe_Logger::log( 'Pre Orders Error: ' . $e->getMessage() );
return [
'result' => 'success',
'redirect' => $order->get_checkout_payment_url( true ),
];
}
}
/**
* Process a pre-order payment when the pre-order is released.
*
* @param WC_Order $order
* @param bool $retry
*
* @return void
*/
public function process_pre_order_release_payment( $order, $retry = true ) {
try {
$source = $this->prepare_order_source( $order );
$response = $this->create_and_confirm_intent_for_off_session( $order, $source );
$is_authentication_required = $this->is_authentication_required_for_payment( $response );
if ( ! empty( $response->error ) && ! $is_authentication_required ) {
if ( ! $retry ) {
throw new Exception( $response->error->message );
}
$this->remove_order_source_before_retry( $order );
$this->process_pre_order_release_payment( $order, false );
} elseif ( $is_authentication_required ) {
$charge = end( $response->error->payment_intent->charges->data );
$id = $charge->id;
$order->set_transaction_id( $id );
/* translators: %s is the charge Id */
$order->update_status( 'failed', sprintf( __( 'Stripe charge awaiting authentication by user: %s.', 'woocommerce-gateway-stripe' ), $id ) );
if ( is_callable( [ $order, 'save' ] ) ) {
$order->save();
}
WC_Emails::instance();
do_action( 'wc_gateway_stripe_process_payment_authentication_required', $order );
throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
} else {
// Successful
$this->process_response( end( $response->charges->data ), $order );
}
} catch ( Exception $e ) {
$error_message = is_callable( [ $e, 'getLocalizedMessage' ] ) ? $e->getLocalizedMessage() : $e->getMessage();
/* translators: error message */
$order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $error_message );
// Mark order as failed if not already set,
// otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times
if ( ! $order->has_status( 'failed' ) ) {
$order->update_status( 'failed', $order_note );
} else {
$order->add_order_note( $order_note );
}
}
}
}