blob: c673f5f364021abac91cf74f1966fd8323e51b1d [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4}
5
6/**
7 * Class that handles P24 payment method.
8 *
9 * @extends WC_Gateway_Stripe
10 *
11 * @since 4.0.0
12 */
13class WC_Gateway_Stripe_P24 extends WC_Stripe_Payment_Gateway {
14
15 const ID = 'stripe_p24';
16
17 /**
18 * Notices (array)
19 *
20 * @var array
21 */
22 public $notices = [];
23
24 /**
25 * Is test mode active?
26 *
27 * @var bool
28 */
29 public $testmode;
30
31 /**
32 * Alternate credit card statement name
33 *
34 * @var bool
35 */
36 public $statement_descriptor;
37
38 /**
39 * API access secret key
40 *
41 * @var string
42 */
43 public $secret_key;
44
45 /**
46 * Api access publishable key
47 *
48 * @var string
49 */
50 public $publishable_key;
51
52 /**
53 * Should we store the users credit cards?
54 *
55 * @var bool
56 */
57 public $saved_cards;
58
59 /**
60 * Constructor
61 */
62 public function __construct() {
63 $this->id = 'stripe_p24';
64 $this->method_title = __( 'Stripe P24', 'woocommerce-gateway-stripe' );
65 $this->method_description = sprintf(
66 /* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */
67 __( 'All other general Stripe settings can be adjusted %1$shere%2$s.', 'woocommerce-gateway-stripe' ),
68 '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout&section=stripe' ) ) . '">',
69 '</a>'
70 );
71 $this->supports = [
72 'products',
73 'refunds',
74 ];
75
76 // Load the form fields.
77 $this->init_form_fields();
78
79 // Load the settings.
80 $this->init_settings();
81
82 $main_settings = get_option( 'woocommerce_stripe_settings' );
83 $this->title = $this->get_option( 'title' );
84 $this->description = $this->get_option( 'description' );
85 $this->enabled = $this->get_option( 'enabled' );
86 $this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false;
87 $this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false;
88 $this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : '';
89 $this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : '';
90 $this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : '';
91
92 if ( $this->testmode ) {
93 $this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : '';
94 $this->secret_key = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : '';
95 }
96
97 add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] );
98 add_action( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] );
99 }
100
101 /**
102 * Returns all supported currencies for this payment method.
103 *
104 * @since 4.0.0
105 * @version 4.0.0
106 * @return array
107 */
108 public function get_supported_currency() {
109 return apply_filters(
110 'wc_stripe_p24_supported_currencies',
111 [
112 'EUR',
113 'PLN',
114 ]
115 );
116 }
117
118 /**
119 * Checks to see if all criteria is met before showing payment method.
120 *
121 * @since 4.0.0
122 * @version 4.0.0
123 * @return bool
124 */
125 public function is_available() {
126 if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
127 return false;
128 }
129
130 return parent::is_available();
131 }
132
133 /**
134 * Get_icon function.
135 *
136 * @since 1.0.0
137 * @version 4.0.0
138 * @return string
139 */
140 public function get_icon() {
141 $icons = $this->payment_icons();
142
143 $icons_str = '';
144
145 $icons_str .= isset( $icons['p24'] ) ? $icons['p24'] : '';
146
147 return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
148 }
149
150 /**
151 * Outputs scripts used for stripe payment
152 */
153 public function payment_scripts() {
154 if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
155 return;
156 }
157
158 wp_enqueue_style( 'stripe_styles' );
159 wp_enqueue_script( 'woocommerce_stripe' );
160 }
161
162 /**
163 * Initialize Gateway Settings Form Fields.
164 */
165 public function init_form_fields() {
166 $this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-p24-settings.php';
167 }
168
169 /**
170 * Payment form on checkout page
171 */
172 public function payment_fields() {
173 global $wp;
174 $user = wp_get_current_user();
175 $total = WC()->cart->total;
176 $description = $this->get_description();
177
178 // If paying from order, we need to get total from order not cart.
179 if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
180 $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
181 $total = $order->get_total();
182 }
183
184 if ( is_add_payment_method_page() ) {
185 $pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
186 $total = '';
187 } else {
188 $pay_button_text = '';
189 }
190
191 echo '<div
192 id="stripe-p24-payment-data"
193 data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
194 data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
195
196 if ( $description ) {
197 echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
198 }
199
200 echo '</div>';
201 }
202
203 /**
204 * Creates the source for charge.
205 *
206 * @since 4.0.0
207 * @version 4.0.0
208 * @param object $order
209 * @return mixed
210 */
211 public function create_source( $order ) {
212 $currency = $order->get_currency();
213 $return_url = $this->get_stripe_return_url( $order );
214 $post_data = [];
215 $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency );
216 $post_data['currency'] = strtolower( $currency );
217 $post_data['type'] = 'p24';
218 $post_data['owner'] = $this->get_owner_details( $order );
219 $post_data['redirect'] = [ 'return_url' => $return_url ];
220
221 WC_Stripe_Logger::log( 'Info: Begin creating P24 source' );
222
223 return WC_Stripe_API::request( apply_filters( 'wc_stripe_p24_source', $post_data, $order ), 'sources' );
224 }
225
226 /**
227 * Process the payment
228 *
229 * @param int $order_id Reference.
230 * @param bool $retry Should we retry on fail.
231 * @param bool $force_save_source Force payment source to be saved.
232 *
233 * @throws Exception If payment will not be accepted.
234 *
235 * @return array|void
236 */
237 public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
238 try {
239 $order = wc_get_order( $order_id );
240
241 // This will throw exception if not valid.
242 $this->validate_minimum_order_amount( $order );
243
244 // This comes from the create account checkbox in the checkout page.
245 $create_account = ! empty( $_POST['createaccount'] ) ? true : false;
246
247 if ( $create_account ) {
248 $new_customer_id = $order->get_customer_id();
249 $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
250 $new_stripe_customer->create_customer();
251 }
252
253 $response = $this->create_source( $order );
254
255 if ( ! empty( $response->error ) ) {
256 $order->add_order_note( $response->error->message );
257
258 throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message );
259 }
260
261 $order->update_meta_data( '_stripe_source_id', $response->id );
262 $order->save();
263
264 WC_Stripe_Logger::log( 'Info: Redirecting to P24...' );
265
266 return [
267 'result' => 'success',
268 'redirect' => esc_url_raw( $response->redirect->url ),
269 ];
270 } catch ( WC_Stripe_Exception $e ) {
271 wc_add_notice( $e->getLocalizedMessage(), 'error' );
272 WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
273
274 do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
275
276 if ( $order->has_status(
277 apply_filters(
278 'wc_stripe_allowed_payment_processing_statuses',
279 [ 'pending', 'failed' ],
280 $order
281 )
282 ) ) {
283 $this->send_failed_order_email( $order_id );
284 }
285
286 return [
287 'result' => 'fail',
288 'redirect' => '',
289 ];
290 }
291 }
292}