class-upe-payment-method.php
2.91 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
<?php
/**
* Abstract UPE Payment Method class
*
* Handles general functionality for UPE payment methods
*
* @package WCPay\Payment_Methods
*/
namespace WCPay\Payment_Methods;
use WP_User;
use WC_Payments_Token_Service;
use WC_Payment_Token_CC;
use WC_Payment_Token_WCPay_SEPA;
use WC_Payments_Subscriptions_Utilities;
/**
* Extendable abstract class for payment methods.
*/
abstract class UPE_Payment_Method {
use WC_Payments_Subscriptions_Utilities;
/**
* Stripe key name
*
* @var string
*/
protected $stripe_id;
/**
* Display title
*
* @var string
*/
protected $title;
/**
* Can payment method be saved or reused?
*
* @var bool
*/
protected $is_reusable;
/**
* Instance of WC Payments Token Service to save payment method
*
* @var WC_Payments_Token_Service
*/
protected $token_service;
/**
* Supported presentment currencies for which charges for a payment method can be processed
* Empty if all currencies are supported
*
* @var array
*/
protected $currencies;
/**
* Create instance of payment method
*
* @param WC_Payments_Token_Service $token_service Instance of WC_Payments_Token_Service.
*/
public function __construct( $token_service ) {
$this->token_service = $token_service;
}
/**
* Returns payment method ID
*
* @return string
*/
public function get_id() {
return $this->stripe_id;
}
/**
* Returns payment method title
*
* @param array|bool $payment_details Optional payment details from charge object.
*
* @return string
*/
public function get_title( $payment_details = false ) {
return $this->title;
}
/**
* Returns payment method currencies
*
* @return array
*/
public function get_currencies() {
return $this->currencies;
}
/**
* Returns boolean dependent on whether payment method
* can be used at checkout
*
* @return bool
*/
public function is_enabled_at_checkout() {
if ( $this->is_subscription_item_in_cart() ) {
return $this->is_reusable();
}
return true;
}
/**
* Returns boolean dependent on whether payment method
* will support saved payments/subscription payments
*
* @return bool
*/
public function is_reusable() {
return $this->is_reusable;
}
/**
* Returns boolean dependent on whether payment method will accept charges
* with chosen currency
*
* @return bool
*/
public function is_currency_valid() {
return empty( $this->currencies ) || in_array( get_woocommerce_currency(), $this->currencies, true );
}
/**
* Add payment method to user and return WC payment token
*
* @param WP_User $user User to get payment token from.
* @param string $payment_method_id Stripe payment method ID string.
*
* @return WC_Payment_Token_CC|WC_Payment_Token_WCPay_SEPA WC object for payment token.
*/
public function get_payment_token_for_user( $user, $payment_method_id ) {
return $this->token_service->add_payment_method_to_user( $payment_method_id, $user );
}
}