|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* Compatibility class for Pre-Orders. |
|
8
|
|
|
* |
|
9
|
|
|
*/ |
|
10
|
|
|
class WC_Stripe_Pre_Orders_Compat extends WC_Stripe_Payment_Gateway { |
|
11
|
|
|
public $saved_cards; |
|
12
|
|
|
|
|
13
|
|
|
public function __construct() { |
|
14
|
|
|
$this->saved_cards = WC_Stripe_Helper::get_settings( 'stripe', 'saved_cards' ); |
|
15
|
|
|
} |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Is $order_id a pre-order? |
|
19
|
|
|
* @param int $order_id |
|
20
|
|
|
* @return boolean |
|
21
|
|
|
*/ |
|
22
|
|
|
public function is_pre_order( $order_id ) { |
|
23
|
|
|
return WC_Pre_Orders_Order::order_contains_pre_order( $order_id ); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Remove order meta |
|
28
|
|
|
* @param object $order |
|
29
|
|
|
*/ |
|
30
|
|
|
public function remove_order_source_before_retry( $order ) { |
|
31
|
|
|
$order->delete_meta_data( '_stripe_source_id' ); |
|
32
|
|
|
$order->delete_meta_data( '_stripe_card_id' ); |
|
33
|
|
|
$order->save(); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Process the pre-order when pay upon release is used. |
|
38
|
|
|
* @param int $order_id |
|
39
|
|
|
*/ |
|
40
|
|
|
public function process_pre_order( $order_id ) { |
|
41
|
|
|
try { |
|
42
|
|
|
$order = wc_get_order( $order_id ); |
|
43
|
|
|
|
|
44
|
|
|
// This will throw exception if not valid. |
|
45
|
|
|
$this->validate_minimum_order_amount( $order ); |
|
46
|
|
|
|
|
47
|
|
|
$prepared_source = $this->prepare_source( get_current_user_id(), true ); |
|
48
|
|
|
|
|
49
|
|
|
// We need a source on file to continue. |
|
50
|
|
|
if ( empty( $prepared_source->customer ) || empty( $prepared_source->source ) ) { |
|
51
|
|
|
throw new WC_Stripe_Exception( __( 'Unable to store payment details. Please try again.', 'woocommerce-gateway-stripe' ) ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
// Setup the response early to allow later modifications. |
|
55
|
|
|
$response = array( |
|
56
|
|
|
'result' => 'success', |
|
57
|
|
|
'redirect' => $this->get_return_url( $order ), |
|
58
|
|
|
); |
|
59
|
|
|
|
|
60
|
|
|
$this->save_source_to_order( $order, $prepared_source ); |
|
61
|
|
|
|
|
62
|
|
|
// Try setting up a payment intent. |
|
63
|
|
|
$intent_secret = $this->setup_intent( $order, $prepared_source ); |
|
64
|
|
|
if ( ! empty( $intent_secret ) ) { |
|
65
|
|
|
$response['setup_intent_secret'] = $intent_secret; |
|
66
|
|
|
return $response; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
// Remove cart. |
|
70
|
|
|
WC()->cart->empty_cart(); |
|
71
|
|
|
|
|
72
|
|
|
// Is pre ordered! |
|
73
|
|
|
WC_Pre_Orders_Order::mark_order_as_pre_ordered( $order ); |
|
74
|
|
|
|
|
75
|
|
|
// Return thank you page redirect |
|
76
|
|
|
return $response; |
|
77
|
|
|
} catch ( WC_Stripe_Exception $e ) { |
|
78
|
|
|
wc_add_notice( $e->getLocalizedMessage(), 'error' ); |
|
79
|
|
|
WC_Stripe_Logger::log( 'Pre Orders Error: ' . $e->getMessage() ); |
|
80
|
|
|
|
|
81
|
|
|
return array( |
|
82
|
|
|
'result' => 'success', |
|
83
|
|
|
'redirect' => $order->get_checkout_payment_url( true ), |
|
84
|
|
|
); |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Process a pre-order payment when the pre-order is released. |
|
90
|
|
|
* |
|
91
|
|
|
* @param WC_Order $order |
|
92
|
|
|
* @param bool $retry |
|
93
|
|
|
* |
|
94
|
|
|
* @return void |
|
95
|
|
|
*/ |
|
96
|
|
|
public function process_pre_order_release_payment( $order, $retry = true ) { |
|
97
|
|
|
try { |
|
98
|
|
|
$source = $this->prepare_order_source( $order ); |
|
99
|
|
|
$response = $this->create_and_confirm_intent_for_off_session( $order, $source ); |
|
100
|
|
|
|
|
101
|
|
|
$is_authentication_required = $this->is_authentication_required_for_payment( $response ); |
|
102
|
|
|
|
|
103
|
|
|
if ( ! empty( $response->error ) && ! $is_authentication_required ) { |
|
104
|
|
|
if ( ! $retry ) { |
|
105
|
|
|
throw new Exception( $response->error->message ); |
|
106
|
|
|
} |
|
107
|
|
|
$this->remove_order_source_before_retry( $order ); |
|
108
|
|
|
$this->process_pre_order_release_payment( $order, false ); |
|
109
|
|
|
} else if ( $is_authentication_required ) { |
|
110
|
|
|
$charge = end( $response->error->payment_intent->charges->data ); |
|
111
|
|
|
$id = $charge->id; |
|
112
|
|
|
|
|
113
|
|
|
$order->set_transaction_id( $id ); |
|
114
|
|
|
$order->update_status( 'failed', sprintf( __( 'Stripe charge awaiting authentication by user: %s.', 'woocommerce-gateway-stripe' ), $id ) ); |
|
115
|
|
|
if ( is_callable( array( $order, 'save' ) ) ) { |
|
116
|
|
|
$order->save(); |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
WC_Emails::instance(); |
|
120
|
|
|
|
|
121
|
|
|
do_action( 'wc_gateway_stripe_process_payment_authentication_required', $order ); |
|
122
|
|
|
|
|
123
|
|
|
throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
124
|
|
|
} else { |
|
125
|
|
|
// Successful |
|
126
|
|
|
$this->process_response( end( $response->charges->data ), $order ); |
|
127
|
|
|
} |
|
128
|
|
|
} catch ( Exception $e ) { |
|
129
|
|
|
$error_message = is_callable( array( $e, 'getLocalizedMessage' ) ) ? $e->getLocalizedMessage() : $e->getMessage(); |
|
|
|
|
|
|
130
|
|
|
/* translators: error message */ |
|
131
|
|
|
$order_note = sprintf( __( 'Stripe Transaction Failed (%s)', 'woocommerce-gateway-stripe' ), $error_message ); |
|
132
|
|
|
|
|
133
|
|
|
// Mark order as failed if not already set, |
|
134
|
|
|
// otherwise, make sure we add the order note so we can detect when someone fails to check out multiple times |
|
135
|
|
|
if ( ! $order->has_status( 'failed' ) ) { |
|
136
|
|
|
$order->update_status( 'failed', $order_note ); |
|
137
|
|
|
} else { |
|
138
|
|
|
$order->add_order_note( $order_note ); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different sub-classes of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the parent class: