|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
exit; |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
/** |
|
7
|
|
|
* WC_Stripe_Session class. |
|
8
|
|
|
* |
|
9
|
|
|
* Represents a Stripe Session. |
|
10
|
|
|
*/ |
|
11
|
|
|
class WC_Stripe_Session { |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* Stripe session ID |
|
15
|
|
|
* @var string |
|
16
|
|
|
*/ |
|
17
|
|
|
private $id = ''; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Constructor |
|
21
|
|
|
*/ |
|
22
|
|
|
public function __construct() { |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Get the Stripe session ID. |
|
27
|
|
|
* @return string |
|
28
|
|
|
*/ |
|
29
|
|
|
public function get_id() { |
|
30
|
|
|
return $this->id; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Set the Stripe session ID. |
|
35
|
|
|
* @param string $id Stripe session ID. |
|
36
|
|
|
*/ |
|
37
|
|
|
public function set_id( $id ) { |
|
38
|
|
|
$this->id = wc_clean( $id ); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* Generates the session request, used for both creating and updating sessions. |
|
43
|
|
|
* |
|
44
|
|
|
* @param array $args Additional arguments (optional). |
|
45
|
|
|
* @return array |
|
46
|
|
|
*/ |
|
47
|
|
|
protected function generate_session_request( $args = array() ) { |
|
48
|
|
|
$defaults = [ |
|
49
|
|
|
'payment_method_types' => [ 'card', 'ideal' ], |
|
50
|
|
|
'line_items' => [ |
|
51
|
|
|
[ |
|
52
|
|
|
'price_data' => [ |
|
53
|
|
|
'currency' => 'eur', |
|
54
|
|
|
'product_data' => [ |
|
55
|
|
|
'name' => 'T-shirt', |
|
56
|
|
|
], |
|
57
|
|
|
'unit_amount' => 2000, |
|
58
|
|
|
], |
|
59
|
|
|
'quantity' => 1, |
|
60
|
|
|
] |
|
61
|
|
|
], |
|
62
|
|
|
'mode' => 'payment', |
|
63
|
|
|
'success_url' => '', |
|
64
|
|
|
'cancel_url' => wc_get_checkout_url() |
|
65
|
|
|
]; |
|
66
|
|
|
|
|
67
|
|
|
return wp_parse_args( $args, $defaults ); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Create a session via API. |
|
72
|
|
|
* @return WP_Error|string Session ID |
|
73
|
|
|
*/ |
|
74
|
|
|
public function create_session( $args ) { |
|
75
|
|
|
error_log( 'in create_session' ); |
|
76
|
|
|
$args = $this->generate_session_request( $args ); |
|
77
|
|
|
$response = WC_Stripe_API::request( apply_filters( 'wc_stripe_create_session_args', $args ), 'checkout/sessions' ); |
|
78
|
|
|
|
|
79
|
|
|
if ( ! empty( $response->error ) ) { |
|
80
|
|
|
throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
$this->set_id( $response->id ); |
|
84
|
|
|
|
|
85
|
|
|
do_action( 'woocommerce_stripe_add_customer', $args, $response ); |
|
86
|
|
|
|
|
87
|
|
|
return $this->get_id(); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Updates the Stripe session through the API. |
|
92
|
|
|
* |
|
93
|
|
|
* @return string Session ID |
|
94
|
|
|
* |
|
95
|
|
|
* @throws WC_Stripe_Exception |
|
96
|
|
|
*/ |
|
97
|
|
|
public function update_customer( $args = array() ) { |
|
98
|
|
|
if ( empty( $this->get_id() ) ) { |
|
99
|
|
|
throw new WC_Stripe_Exception( 'id_required_to_update_session', __( 'Attempting to update a Stripe session without a session ID.', 'woocommerce-gateway-stripe' ) ); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
$args = $this->generate_session_request( $args ); |
|
103
|
|
|
$args = apply_filters( 'wc_stripe_update_session_args', $args ); |
|
104
|
|
|
$response = WC_Stripe_API::request( $args, 'sessions/' . $this->get_id() ); |
|
105
|
|
|
|
|
106
|
|
|
if ( ! empty( $response->error ) ) { |
|
107
|
|
|
if ( $this->is_no_such_session_error( $response->error ) ) { |
|
|
|
|
|
|
108
|
|
|
// This can happen when switching the main Stripe account or importing users from another site. |
|
109
|
|
|
// If not already retrying, recreate the customer and then try updating it again. |
|
110
|
|
|
$this->recreate_session(); |
|
|
|
|
|
|
111
|
|
|
return $this->update_session( $args ); // TODO retry limit |
|
|
|
|
|
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
|
115
|
|
|
} |
|
116
|
|
|
|
|
117
|
|
|
do_action( 'woocommerce_stripe_update_session', $args, $response ); |
|
118
|
|
|
|
|
119
|
|
|
return $this->get_id(); |
|
120
|
|
|
} |
|
121
|
|
|
} |
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.