class-wc-gateway-stripe-multibanco.php
11.4 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
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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class that handles Multibanco payment method.
*
* @extends WC_Gateway_Stripe
*
* @since 4.1.0
*/
class WC_Gateway_Stripe_Multibanco extends WC_Stripe_Payment_Gateway {
/**
* Notices (array)
*
* @var array
*/
public $notices = [];
/**
* Is test mode active?
*
* @var bool
*/
public $testmode;
/**
* Alternate credit card statement name
*
* @var bool
*/
public $statement_descriptor;
/**
* API access secret key
*
* @var string
*/
public $secret_key;
/**
* Api access publishable key
*
* @var string
*/
public $publishable_key;
/**
* Should we store the users credit cards?
*
* @var bool
*/
public $saved_cards;
/**
* Constructor
*/
public function __construct() {
$this->id = 'stripe_multibanco';
$this->method_title = __( 'Stripe Multibanco', 'woocommerce-gateway-stripe' );
$this->method_description = sprintf(
/* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
__( 'All other general Stripe settings can be adjusted %1$shere%2$s.', 'woocommerce-gateway-stripe' ),
'<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=stripe' ) ) . '">',
'</a>'
);
$this->supports = [
'products',
'refunds',
];
// Load the form fields.
$this->init_form_fields();
// Load the settings.
$this->init_settings();
$main_settings = get_option( 'woocommerce_stripe_settings' );
$this->title = $this->get_option( 'title' );
$this->description = $this->get_option( 'description' );
$this->enabled = $this->get_option( 'enabled' );
$this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
$this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
$this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
$this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
if ( $this->testmode ) {
$this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
$this->secret_key = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
}
add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] );
add_action( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] );
add_action( 'woocommerce_thankyou_stripe_multibanco', [ $this, 'thankyou_page' ] );
// Customer Emails
add_action( 'woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3 );
}
/**
* Returns all supported currencies for this payment method.
*
* @since 4.1.0
* @version 4.1.0
* @return array
*/
public function get_supported_currency() {
return apply_filters(
'wc_stripe_multibanco_supported_currencies',
[
'EUR',
]
);
}
/**
* Checks to see if all criteria is met before showing payment method.
*
* @since 4.1.0
* @version 4.1.0
* @return bool
*/
public function is_available() {
if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
return false;
}
return parent::is_available();
}
/**
* Get_icon function.
*
* @since 1.0.0
* @version 4.1.0
* @return string
*/
public function get_icon() {
$icons = $this->payment_icons();
$icons_str = '';
$icons_str .= isset( $icons['multibanco'] ) ? $icons['multibanco'] : '';
return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
}
/**
* Outputs scripts used for stripe payment
*/
public function payment_scripts() {
if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
return;
}
wp_enqueue_style( 'stripe_styles' );
wp_enqueue_script( 'woocommerce_stripe' );
}
/**
* Initialize Gateway Settings Form Fields.
*/
public function init_form_fields() {
$this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-multibanco-settings.php';
}
/**
* Payment form on checkout page
*/
public function payment_fields() {
global $wp;
$user = wp_get_current_user();
$total = WC()->cart->total;
$description = $this->get_description();
// If paying from order, we need to get total from order not cart.
if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
$order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
$total = $order->get_total();
}
if ( is_add_payment_method_page() ) {
$pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
$total = '';
} else {
$pay_button_text = '';
}
echo '<div
id="stripe-multibanco-payment-data"
data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
if ( $description ) {
echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
}
echo '</div>';
}
/**
* Output for the order received page.
*
* @param int $order_id
*/
public function thankyou_page( $order_id ) {
$this->get_instructions( $order_id );
}
/**
* Add content to the WC emails.
*
* @since 4.1.0
* @version 4.1.0
* @param WC_Order $order
* @param bool $sent_to_admin
* @param bool $plain_text
*/
public function email_instructions( $order, $sent_to_admin, $plain_text = false ) {
$order_id = $order->get_id();
$payment_method = $order->get_payment_method();
if ( ! $sent_to_admin && 'stripe_multibanco' === $payment_method && $order->has_status( 'on-hold' ) ) {
WC_Stripe_Logger::log( 'Sending multibanco email for order #' . $order_id );
$this->get_instructions( $order, $plain_text );
}
}
/**
* Gets the Multibanco instructions for customer to pay.
*
* @since 4.1.0
* @version 4.1.0
* @param int|WC_Order $order
*/
public function get_instructions( $order, $plain_text = false ) {
if ( true === is_int( $order ) ) {
$order = wc_get_order( $order );
}
$data = $order->get_meta( '_stripe_multibanco' );
if ( $plain_text ) {
esc_html_e( 'MULTIBANCO INFORMAÇÕES DE ENCOMENDA:', 'woocommerce-gateway-stripe' ) . "\n\n";
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
esc_html_e( 'Montante:', 'woocommerce-gateway-stripe' ) . "\n\n";
echo $data['amount'] . "\n\n";
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
esc_html_e( 'Entidade:', 'woocommerce-gateway-stripe' ) . "\n\n";
echo $data['entity'] . "\n\n";
echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n";
esc_html_e( 'Referencia:', 'woocommerce-gateway-stripe' ) . "\n\n";
echo $data['reference'] . "\n\n";
} else {
?>
<h3><?php esc_html_e( 'MULTIBANCO INFORMAÇÕES DE ENCOMENDA:', 'woocommerce-gateway-stripe' ); ?></h3>
<ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details">
<li class="woocommerce-order-overview__order order">
<?php esc_html_e( 'Montante:', 'woocommerce-gateway-stripe' ); ?>
<strong><?php echo $data['amount']; ?></strong>
</li>
<li class="woocommerce-order-overview__order order">
<?php esc_html_e( 'Entidade:', 'woocommerce-gateway-stripe' ); ?>
<strong><?php echo $data['entity']; ?></strong>
</li>
<li class="woocommerce-order-overview__order order">
<?php esc_html_e( 'Referencia:', 'woocommerce-gateway-stripe' ); ?>
<strong><?php echo $data['reference']; ?></strong>
</li>
</ul>
<?php
}
}
/**
* Saves Multibanco information to the order meta for later use.
*
* @since 4.1.0
* @version 4.1.0
* @param object $order
* @param object $source_object
*/
public function save_instructions( $order, $source_object ) {
$data = [
'amount' => $order->get_formatted_order_total(),
'entity' => $source_object->multibanco->entity,
'reference' => $source_object->multibanco->reference,
];
$order_id = $order->get_id();
$order->update_meta_data( '_stripe_multibanco', $data );
}
/**
* Creates the source for charge.
*
* @since 4.1.0
* @version 4.1.0
* @param object $order
* @return mixed
*/
public function create_source( $order ) {
$currency = $order->get_currency();
$return_url = $this->get_stripe_return_url( $order );
$post_data = [];
$post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
$post_data['currency'] = strtolower( $currency );
$post_data['type'] = 'multibanco';
$post_data['owner'] = $this->get_owner_details( $order );
$post_data['redirect'] = [ 'return_url' => $return_url ];
if ( ! empty( $this->statement_descriptor ) ) {
$post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
}
WC_Stripe_Logger::log( 'Info: Begin creating Multibanco source' );
return WC_Stripe_API::request( $post_data, 'sources' );
}
/**
* Process the payment
*
* @param int $order_id Reference.
* @param bool $retry Should we retry on fail.
* @param bool $force_save_source Force payment source to be saved.
*
* @throws Exception If payment will not be accepted.
*
* @return array|void
*/
public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
try {
$order = wc_get_order( $order_id );
// This will throw exception if not valid.
$this->validate_minimum_order_amount( $order );
// This comes from the create account checkbox in the checkout page.
$create_account = ! empty( $_POST['createaccount'] ) ? true : false;
if ( $create_account ) {
$new_customer_id = $order->get_customer_id();
$new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
$new_stripe_customer->create_customer();
}
$response = $this->create_source( $order );
if ( ! empty( $response->error ) ) {
$order->add_order_note( $response->error->message );
throw new Exception( $response->error->message );
}
$order->update_meta_data( '_stripe_source_id', $response->id );
$order->save();
$this->save_instructions( $order, $response );
// Mark as on-hold (we're awaiting the payment)
$order->update_status( 'on-hold', __( 'Awaiting Multibanco payment', 'woocommerce-gateway-stripe' ) );
// Reduce stock levels
wc_reduce_stock_levels( $order_id );
// Remove cart
WC()->cart->empty_cart();
WC_Stripe_Logger::log( 'Info: Redirecting to Multibanco...' );
return [
'result' => 'success',
'redirect' => esc_url_raw( $response->redirect->url ),
];
} catch ( Exception $e ) {
wc_add_notice( $e->getMessage(), 'error' );
WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
if ( $order->has_status(
apply_filters(
'wc_stripe_allowed_payment_processing_statuses',
[ 'pending', 'failed' ],
$order
)
) ) {
$this->send_failed_order_email( $order_id );
}
return [
'result' => 'fail',
'redirect' => '',
];
}
}
}