1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Give - Stripe Core Payment Intent |
4
|
|
|
* |
5
|
|
|
* @since 2.5.0 |
6
|
|
|
* |
7
|
|
|
* @package Give |
8
|
|
|
* @subpackage Stripe Core |
9
|
|
|
* @copyright Copyright (c) 2019, GiveWP |
10
|
|
|
* @license https://opensource.org/licenses/gpl-license GNU Public License |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
// Exit, if accessed directly. |
14
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
15
|
|
|
exit; |
16
|
|
|
} |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Check for class Give_Stripe_Payment_Intent exists. |
20
|
|
|
* |
21
|
|
|
* @since 2.5.0 |
22
|
|
|
*/ |
23
|
|
|
if ( ! class_exists( 'Give_Stripe_Payment_Intent' ) ) { |
24
|
|
|
|
25
|
|
|
class Give_Stripe_Payment_Intent { |
26
|
|
|
|
27
|
|
|
public function __construct() { |
28
|
|
|
|
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* This function is used to create payment intent in Stripe. |
33
|
|
|
* |
34
|
|
|
* @param array $args List of parameters required to create payment intent. |
35
|
|
|
* |
36
|
|
|
* @since 2.5.0 |
37
|
|
|
* @access public |
38
|
|
|
* |
39
|
|
|
* @return \Stripe\PaymentIntent |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function create( $args ) { |
|
|
|
|
42
|
|
|
|
43
|
|
|
// Add application fee, if the Stripe premium add-on is not active. |
44
|
|
|
if ( ! defined( 'GIVE_STRIPE_VERSION' ) ) { |
45
|
|
|
$args['application_fee_amount'] = give_stripe_get_application_fee_amount( $args['amount'] ); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
// Set Stripe Application Info. |
49
|
|
|
give_stripe_set_app_info(); |
50
|
|
|
|
51
|
|
|
try { |
52
|
|
|
return \Stripe\PaymentIntent::create( |
53
|
|
|
$args, |
54
|
|
|
give_stripe_get_connected_account_options() |
55
|
|
|
); |
56
|
|
|
} catch ( Exception $e ) { |
57
|
|
|
|
58
|
|
|
give_record_gateway_error( |
59
|
|
|
__( 'Stripe Payment Intent Error', 'give' ), |
60
|
|
|
sprintf( |
61
|
|
|
/* translators: %s Exception Error Message */ |
62
|
|
|
__( 'Unable to create a payment intent. Details: %s', 'give' ), |
63
|
|
|
$e->getMessage() |
64
|
|
|
) |
65
|
|
|
); |
66
|
|
|
|
67
|
|
|
give_set_error( 'stripe_payment_intent_error', __( 'Error creating payment intent with Stripe. Please try again.', 'give' ) ); |
68
|
|
|
} // End try(). |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* This function is used to retrieve payment intent in Stripe. |
73
|
|
|
* |
74
|
|
|
* @param string $client_secret Client Secret represents unique string for the payment intent. |
75
|
|
|
* |
76
|
|
|
* @since 2.5.0 |
77
|
|
|
* @access public |
78
|
|
|
* |
79
|
|
|
* @return \Stripe\PaymentIntent |
80
|
|
|
*/ |
81
|
|
|
public function retrieve( $client_secret ) { |
82
|
|
|
|
83
|
|
|
// Set Application Info. |
84
|
|
|
give_stripe_set_app_info(); |
85
|
|
|
|
86
|
|
|
try { |
87
|
|
|
return \Stripe\PaymentIntent::retrieve( |
88
|
|
|
$client_secret, |
89
|
|
|
give_stripe_get_connected_account_options() |
90
|
|
|
); |
91
|
|
|
} catch ( Exception $e ) { |
92
|
|
|
|
93
|
|
|
give_record_gateway_error( |
94
|
|
|
__( 'Stripe Payment Intent Error', 'give' ), |
95
|
|
|
sprintf( |
96
|
|
|
/* translators: %s Exception Error Message */ |
97
|
|
|
__( 'Unable to retrieve a payment intent. Details: %s', 'give' ), |
98
|
|
|
$e |
99
|
|
|
) |
100
|
|
|
); |
101
|
|
|
|
102
|
|
|
give_set_error( 'stripe_payment_intent_error', __( 'Error retrieving payment intent with Stripe. Please try again.', 'give' ) ); |
103
|
|
|
} // End try(). |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
/** |
107
|
|
|
* This function is used to update existing payment intent in Stripe. |
108
|
|
|
* |
109
|
|
|
* @param string $client_secret Client Secret represents unique string for the payment intent. |
110
|
|
|
* @param array $args List of parameters required to create payment intent. |
111
|
|
|
* |
112
|
|
|
* @since 2.5.0 |
113
|
|
|
* @access public |
114
|
|
|
* |
115
|
|
|
* @return \Stripe\PaymentIntent |
116
|
|
|
*/ |
117
|
|
View Code Duplication |
public function update( $client_secret, $args ) { |
|
|
|
|
118
|
|
|
|
119
|
|
|
// Add application fee, if the Stripe premium add-on is not active. |
120
|
|
|
if ( ! defined( GIVE_STRIPE_VERSION ) ) { |
121
|
|
|
$args['application_fee_amount'] = give_stripe_get_application_fee_amount( $args['amount'] ); |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
// Set Stripe Application Info. |
125
|
|
|
give_stripe_set_app_info(); |
126
|
|
|
|
127
|
|
|
try { |
128
|
|
|
return \Stripe\PaymentIntent::update( |
129
|
|
|
$client_secret, |
130
|
|
|
$args, |
131
|
|
|
give_stripe_get_connected_account_options() |
132
|
|
|
); |
133
|
|
|
} catch ( Exception $e ) { |
134
|
|
|
|
135
|
|
|
give_record_gateway_error( |
136
|
|
|
__( 'Stripe Payment Intent Error', 'give' ), |
137
|
|
|
sprintf( |
138
|
|
|
/* translators: %s Exception Error Message */ |
139
|
|
|
__( 'Unable to update a payment intent. Details: %s', 'give' ), |
140
|
|
|
$e->getMessage() |
141
|
|
|
) |
142
|
|
|
); |
143
|
|
|
|
144
|
|
|
give_set_error( 'stripe_payment_intent_error', __( 'Error updating payment intent with Stripe. Please try again.', 'give' ) ); |
145
|
|
|
} // End try(). |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
} |
149
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.