1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Give - Stripe Core | Payment Method API |
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_Method exists. |
20
|
|
|
* |
21
|
|
|
* @since 2.5.0 |
22
|
|
|
*/ |
23
|
|
|
if ( ! class_exists( 'Give_Stripe_Payment_Method' ) ) { |
24
|
|
|
|
25
|
|
|
class Give_Stripe_Payment_Method { |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Create Payment Method. |
29
|
|
|
* |
30
|
|
|
* @param array $args Payment Method Arguments. |
31
|
|
|
* |
32
|
|
|
* @since 2.5.0 |
33
|
|
|
* |
34
|
|
|
* @return \Stripe\PaymentMethod |
35
|
|
|
*/ |
36
|
|
View Code Duplication |
public function create( $args ) { |
|
|
|
|
37
|
|
|
|
38
|
|
|
try { |
39
|
|
|
|
40
|
|
|
give_stripe_set_app_info(); |
41
|
|
|
|
42
|
|
|
$payment_method = \Stripe\PaymentMethod::create( $args, give_stripe_get_connected_account_options() ); |
43
|
|
|
|
44
|
|
|
} catch( Exception $e ) { |
45
|
|
|
give_record_gateway_error( |
46
|
|
|
__( 'Stripe Payment Method Error', 'give' ), |
47
|
|
|
sprintf( |
48
|
|
|
/* translators: %s Exception Message Body */ |
49
|
|
|
__( 'The Stripe Gateway returned an error while creating the payment method. Details: %s', 'give' ), |
50
|
|
|
$e |
51
|
|
|
) |
52
|
|
|
); |
53
|
|
|
give_set_error( 'stripe_error', __( 'An occurred while creating the payment method. Please try again.', 'give' ) ); |
54
|
|
|
give_send_back_to_checkout( '?payment-mode=' . give_clean( $_GET['payment-mode']) ); |
55
|
|
|
return false; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $payment_method; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Retrieves the payment method. |
63
|
|
|
* |
64
|
|
|
* @param string $id Payment Intent ID. |
65
|
|
|
* |
66
|
|
|
* @return \Stripe\PaymentMethod |
67
|
|
|
*/ |
68
|
|
View Code Duplication |
public function retrieve( $id ) { |
|
|
|
|
69
|
|
|
|
70
|
|
|
try { |
71
|
|
|
|
72
|
|
|
give_stripe_set_app_info(); |
73
|
|
|
|
74
|
|
|
$payment_method_details = \Stripe\PaymentMethod::retrieve( $id, give_stripe_get_connected_account_options() ); |
75
|
|
|
|
76
|
|
|
} catch( Exception $e ) { |
77
|
|
|
give_record_gateway_error( |
78
|
|
|
__( 'Stripe Payment Method Error', 'give' ), |
79
|
|
|
sprintf( |
80
|
|
|
/* translators: %s Exception Message Body */ |
81
|
|
|
__( 'The Stripe Gateway returned an error while retrieving the payment method of the customer. Details: %s', 'give' ), |
82
|
|
|
$e->getMessage() |
83
|
|
|
) |
84
|
|
|
); |
85
|
|
|
give_set_error( 'stripe_error', __( 'An occurred while retrieving the payment method of the customer. Please try again.', 'give' ) ); |
86
|
|
|
give_send_back_to_checkout( '?payment-mode=' . give_clean( $_GET['payment-mode']) ); |
87
|
|
|
return false; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $payment_method_details; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Fetch all payment methods of the customer. |
95
|
|
|
* |
96
|
|
|
* @param string $customer_id Stripe Customer ID. |
97
|
|
|
* @param string $type Stripe Payment Type. |
98
|
|
|
* |
99
|
|
|
* @since 2.5.0 |
100
|
|
|
* |
101
|
|
|
* @return \Stripe\PaymentMethod |
102
|
|
|
*/ |
103
|
|
|
public function list_all( $customer_id, $type = 'card' ) { |
104
|
|
|
|
105
|
|
|
try { |
106
|
|
|
|
107
|
|
|
give_stripe_set_app_info(); |
108
|
|
|
|
109
|
|
|
$all_payment_methods = \Stripe\PaymentMethod::all( |
110
|
|
|
array( |
111
|
|
|
'customer' => $customer_id, |
112
|
|
|
'type' => $type, |
113
|
|
|
'limit' => 100, |
114
|
|
|
), |
115
|
|
|
give_stripe_get_connected_account_options() |
116
|
|
|
); |
117
|
|
|
|
118
|
|
|
} catch( Exception $e ) { |
119
|
|
|
give_record_gateway_error( |
120
|
|
|
__( 'Stripe Payment Method Error', 'give' ), |
121
|
|
|
sprintf( |
122
|
|
|
/* translators: %s Exception Message Body */ |
123
|
|
|
__( 'The Stripe Gateway returned an error while fetching the list of payment methods of the customer. Details: %s', 'give' ), |
124
|
|
|
$e->getMessage() |
125
|
|
|
) |
126
|
|
|
); |
127
|
|
|
give_set_error( 'stripe_error', __( 'An occurred while fetching the list of payment methods of the customer. Please try again.', 'give' ) ); |
128
|
|
|
give_send_back_to_checkout( '?payment-mode=' . give_clean( $_GET['payment-mode']) ); |
129
|
|
|
return false; |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
return $all_payment_methods; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|
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.