-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgateway-buckaroo-in3.php
302 lines (259 loc) · 7.94 KB
/
gateway-buckaroo-in3.php
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
require_once __DIR__ . '/library/api/paymentmethods/in3/in3.php';
require_once __DIR__ . '/library/api/paymentmethods/in3/in3v2.php';
/**
* @package Buckaroo
*/
class WC_Gateway_Buckaroo_In3 extends WC_Gateway_Buckaroo {
const PAYMENT_CLASS = BuckarooIn3::class;
public const VERSION_FLAG = 'buckaroo_in3_version';
public const VERSION3 = 'v3';
public const VERSION2 = 'v2';
public const IN3_V2_TITLE = 'In3';
public const IN3_V3_TITLE = 'iDEAL In3';
public $type;
public $vattype;
public $country;
public function __construct() {
$this->id = 'buckaroo_in3';
$this->has_fields = false;
$this->method_title = 'Buckaroo In3';
$this->title = $this->getTitleForVersion();
$this->setCountry();
parent::__construct();
$this->set_icons();
$this->addRefundSupport();
}
private function getTitleForVersion() {
return $this->get_option( 'api_version' ) === self::VERSION2 ? self::IN3_V2_TITLE : self::IN3_V3_TITLE;
}
/** @inheritDoc */
protected function setProperties() {
parent::setProperties();
$this->type = 'in3';
$this->vattype = $this->get_option( 'vattype' );
}
/**
* Can the order be refunded
*
* @param integer $order_id
* @param integer $amount defaults to null
* @param string $reason
* @return callable|string function or error
*/
public function process_refund( $order_id, $amount = null, $reason = '', $line_item_qtys = null, $line_item_totals = null, $line_item_tax_totals = null, $originalTransactionKey = null ) {
return $this->processDefaultRefund( $order_id, $amount, $reason );
}
/**
* Validate payment fields on the frontend.
*
* @access public
* @return void
*/
public function validate_fields() {
$birthdate = $this->request( 'buckaroo-in3-birthdate' );
$country = $this->request( 'billing_country' );
if ( $country === null ) {
$country = $this->country;
}
if ( $country === 'NL' && ! $this->validateDate( $birthdate, 'd-m-Y' ) ) {
wc_add_notice( __( 'You must be at least 18 years old to use this payment method. Please enter your correct date of birth. Or choose another payment method to complete your order.', 'wc-buckaroo-bpe-gateway' ), 'error' );
}
if (
$this->request( 'billing_phone' ) === null &&
$this->request( 'buckaroo-in3-phone' ) === null
) {
wc_add_notice(
sprintf(
__( 'Please fill in a phone number for %s. This is required in order to use this payment method.', 'wc-buckaroo-bpe-gateway' ),
$this->getTitleForVersion()
),
'error'
);
}
parent::validate_fields();
}
/**
* Process payment
*
* @param integer $order_id
* @return callable|void fn_buckaroo_process_response() or void
*/
public function process_payment( $order_id ) {
$this->setOrderCapture( $order_id, 'In3' );
$order = getWCOrder( $order_id );
$version = $this->get_option( 'api_version' );
update_post_meta(
$order->get_id(),
self::VERSION_FLAG,
$version
);
if ( $version === self::VERSION2 ) {
return $this->pay_with_v2( $order );
}
$order_details = new Buckaroo_Order_Details( $order );
/** @var BuckarooIn3 */
$in3 = $this->createDebitRequest( $order );
$in3->setData(
$order_details,
$this->get_products_for_payment( $order_details ),
new Buckaroo_Http_Request()
);
$response = $in3->pay();
return fn_buckaroo_process_response( $this, $response );
}
/**
* Set icons based on version
*
* @return void
*/
private function set_icons() {
if (
$this->get_option( 'api_version' ) === 'v2'
) {
$this->setIcon( 'svg/in3.svg', 'svg/in3.svg' );
return;
}
$this->setIcon( 'svg/in3-ideal.svg', 'svg/in3-ideal.svg' );
}
/**
* Pay with old version
*
* @param WC_Order $order
*
* @return void
*/
private function pay_with_v2( $order ) {
/** @var BuckarooIn3v2 */
$in3 = $this->createDebitRequest( $order );
$order_details = new Buckaroo_Order_Details( $order );
$birthdate = date( 'Y-m-d', strtotime( $this->request( 'buckaroo-in3-birthdate' ) ) );
$in3 = $this->get_billing_info( $order_details, $in3, $birthdate );
$response = $in3->PayIn3(
$this->get_products_for_payment( $order_details ),
'PayInInstallments'
);
return fn_buckaroo_process_response( $this, $response, $this->mode );
}
/**
* Get billing info for pay request
*
* @param Buckaroo_Order_Details $order_details
* @param BuckarooIn3 $method
* @param string $birthdate
*
* @return BuckarooIn3v2 $method
*/
protected function get_billing_info( $order_details, $method, $birthdate ) {
/** @var BuckarooIn3v2 */
$method = $this->set_billing( $method, $order_details );
$method->BillingInitials = $order_details->getInitials(
$order_details->getBilling( 'first_name' )
);
$method->BillingBirthDate = date( 'Y-m-d', strtotime( $birthdate ) );
$phone = $this->request( 'buckaroo-in3-phone' );
if ( is_scalar( $phone ) && trim( strlen( (string) $phone ) ) > 0 ) {
$method->BillingPhoneNumber = $phone;
}
return $method;
}
/**
* Add fields to the form_fields() array, specific to this page.
*
* @access public
*/
public function init_form_fields() {
parent::init_form_fields();
$this->add_financial_warning_field();
$this->form_fields['api_version'] = array(
'title' => __( 'Api version', 'wc-buckaroo-bpe-gateway' ),
'type' => 'select',
'description' => __( 'Chose the api version for this payment method.', 'wc-buckaroo-bpe-gateway' ),
'options' => array(
self::VERSION3 => __( 'V3 (iDEAL In3)' ),
self::VERSION2 => __( 'V2 (Capayabel/In3)' ),
),
'default' => self::VERSION3,
);
}
/**
* Create custom logo selector
*
* @param mixed $key
* @param mixed $data
*
* @return void
*/
public function generate_in3_logo_html( $key, $data ) {
$field_key = $this->get_field_key( $key );
$defaults = array(
'title' => '',
'disabled' => false,
'class' => '',
'css' => '',
'placeholder' => '',
'type' => 'text',
'desc_tip' => false,
'description' => '',
'custom_attributes' => array(),
'options' => array(),
);
$data = wp_parse_args( $data, $defaults );
$value = $this->get_option( $key );
ob_start();
?>
<tr valign="top">
<th scope="row" class="titledesc">
<label for="<?php echo esc_attr( $field_key ); ?>"><?php echo wp_kses_post( $data['title'] ); ?> <?php
echo $this->get_tooltip_html( $data ); // WPCS: XSS ok.
?>
</label>
</th>
<td>
<fieldset>
<div class="bk-in3-logo-wrap">
<legend class="screen-reader-text"><span><?php echo wp_kses_post( $data['title'] ); ?></span></legend>
<?php foreach ( (array) $data['options'] as $option_key => $option_value ) : ?>
<label class="bk-in3-logo" for="bk-logo-<?php echo esc_attr( $option_key ); ?>">
<input type="radio" id="bk-logo-<?php echo esc_attr( $option_key ); ?>" name="<?php echo esc_attr( $field_key ); ?>" value="<?php echo esc_attr( $option_key ); ?>" <?php checked( (string) $option_key, esc_attr( $value ) ); ?>>
<img src="<?php echo esc_url( $option_value ); ?>" / alt="<?php echo esc_attr( $option_key ); ?>">
</label>
<?php endforeach; ?>
</div>
<?php
echo $this->get_description_html( $data ); // WPCS: XSS ok.
?>
</fieldset>
</td>
</tr>
<?php
return ob_get_clean();
}
/**
* Select the correct class in order to do the request
*
* @param WC_Order $order
* @param boolean $isRefund
*
* @return void
*/
protected function get_payment_class( $order, $isRefund = false ) {
if ( $isRefund ) {
$orderIn3Version = get_post_meta(
$order->get_id(),
self::VERSION_FLAG,
true
);
if ( $orderIn3Version === self::VERSION3 ) {
return BuckarooIn3::class;
}
return BuckarooIn3v2::class;
}
if (
$this->get_option( 'api_version' ) === self::VERSION2
) {
return BuckarooIn3v2::class;
}
return BuckarooIn3::class;
}
}