class-wc-gateway-stripe-boleto.php
5.09 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class that handles Boleto payment method.
*
* @extends WC_Gateway_Stripe
*
* @since 5.8.0
*/
class WC_Gateway_Stripe_Boleto extends WC_Stripe_Payment_Gateway_Voucher {
/**
* ID used by UPE
*
* @var string
*/
const ID = 'stripe_boleto';
/**
* ID used by WooCommerce to identify the payment method
*
* @var string
*/
public $id = 'stripe_boleto';
/**
* ID used by stripe
*/
protected $stripe_id = 'boleto';
/**
* List of accepted currencies
*
* @var array
*/
protected $supported_currencies = [ 'BRL' ];
/**
* List of accepted countries
*/
protected $supported_countries = [ 'BR' ];
/**
* Constructor
*
* @since 5.8.0
*/
public function __construct() {
$this->method_title = __( 'Stripe Boleto', 'woocommerce-gateway-stripe' );
parent::__construct();
add_filter( 'wc_stripe_allowed_payment_processing_statuses', [ $this, 'add_allowed_payment_processing_statuses' ], 10, 2 );
}
/**
* Add payment gateway voucher expiration to API request body.
*
* @param array $body API request body.
* @return array
*/
protected function update_request_body_on_create_or_update_payment_intent( $body ) {
$body['payment_method_options'] = [
'boleto' => [
'expires_after_days' => $this->get_option( 'expiration' ),
],
];
return $body;
}
/**
* Add payment gateway voucher expiration.
*
* @param array $settings Settings array.
* @return array
*/
public function get_unique_settings( $settings ) {
$settings[ $this->id . '_expiration' ] = $this->get_option( 'expiration' );
return $settings;
}
/**
* Updates payment gateway voucher expiration.
*
* @param WP_REST_Request $request Request object.
* @return void
*/
public function update_unique_settings( WP_REST_Request $request ) {
$field_name = $this->id . '_expiration';
$expiration = $request->get_param( $field_name );
if ( null === $expiration ) {
return;
}
$value = absint( $expiration );
$value = min( 60, $value );
$value = max( 0, $value );
$this->update_option( 'expiration', $value );
}
/**
* Adds on-hold as accepted status during webhook handling on orders paid with voucher
*
* @param $allowed_statuses
* @param $order
*
* @return mixed
*/
public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) {
if ( $this->stripe_id === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( 'on-hold', $allowed_statuses ) ) {
$allowed_statuses[] = 'on-hold';
}
return $allowed_statuses;
}
/**
* Payment_scripts function.
*
* @since 5.8.0
*/
public function payment_scripts() {
if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
return;
}
parent::payment_scripts();
wp_enqueue_script( 'jquery-mask', plugins_url( 'assets/js/jquery.mask.min.js', WC_STRIPE_MAIN_FILE ), [], WC_STRIPE_VERSION );
}
/**
* Payment form on checkout page
*
* @since 5.8.0
*/
public function payment_fields() {
$description = $this->get_description();
apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id )
?>
<label>CPF/CNPJ: <abbr class="required" title="required">*</abbr></label><br>
<input id="stripe_boleto_tax_id" name="stripe_boleto_tax_id" type="text"><br><br>
<div class="stripe-source-errors" role="alert"></div>
<div id="stripe-boleto-payment-data"><?php echo $description; ?></div>
<?php
}
/**
* Validates the minimum and maximum amount. Throws exception when out of range value is added
*
* @since 5.8.0
*
* @param $amount
*
* @throws WC_Stripe_Exception
*/
protected function validate_amount_limits( $amount ) {
if ( $amount < 5.00 ) {
/* translators: 1) amount (including currency symbol) */
throw new WC_Stripe_Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( 5.00 ) ) );
} elseif ( $amount > 49999.99 ) {
/* translators: 1) amount (including currency symbol) */
throw new WC_Stripe_Exception( sprintf( __( 'Sorry, the maximum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( 49999.99 ) ) );
}
}
/**
* Gather the data necessary to confirm the payment via javascript
* Override this when extending the class
*
* @param WC_Order $order
*
* @return array
*/
protected function get_confirm_payment_data( $order ) {
return [
'payment_method' => [
'boleto' => [
'tax_id' => isset( $_POST['stripe_boleto_tax_id'] ) ? wc_clean( wp_unslash( $_POST['stripe_boleto_tax_id'] ) ) : null,
],
'billing_details' => [
'name' => $order->get_formatted_billing_full_name(),
'email' => $order->get_billing_email(),
'address' => [
'line1' => $order->get_billing_address_1(),
'city' => $order->get_billing_city(),
'state' => $order->get_billing_state(),
'postal_code' => $order->get_billing_postcode(),
'country' => $order->get_billing_country(),
],
],
],
];
}
}