blob: 06744fd38c69bcfc23143a519a4dadb9266722eb [file] [log] [blame]
swissChilif0cbdc32023-01-05 17:21:38 -05001<?php
2if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4}
5
6/**
7 * Class that handles Sofort payment method.
8 *
9 * @extends WC_Gateway_Stripe
10 *
11 * @since 4.0.0
12 */
13class WC_Gateway_Stripe_Sofort extends WC_Stripe_Payment_Gateway {
14
15 const ID = 'stripe_sofort';
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_sofort';
64 $this->method_title = __( 'Stripe Sofort', '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_sofort_supported_currencies',
111 [
112 'EUR',
113 ]
114 );
115 }
116
117 /**
118 * Checks to see if all criteria is met before showing payment method.
119 *
120 * @since 4.0.0
121 * @version 4.0.0
122 * @return bool
123 */
124 public function is_available() {
125 if ( ! in_array( get_woocommerce_currency(), $this->get_supported_currency() ) ) {
126 return false;
127 }
128
129 return parent::is_available();
130 }
131
132 /**
133 * Get_icon function.
134 *
135 * @since 1.0.0
136 * @version 4.0.0
137 * @return string
138 */
139 public function get_icon() {
140 $icons = $this->payment_icons();
141
142 $icons_str = '';
143
144 $icons_str .= isset( $icons['sofort'] ) ? $icons['sofort'] : '';
145
146 return apply_filters( 'woocommerce_gateway_icon', $icons_str, $this->id );
147 }
148
149 /**
150 * Outputs scripts used for stripe payment
151 */
152 public function payment_scripts() {
153 if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) {
154 return;
155 }
156
157 wp_enqueue_style( 'stripe_styles' );
158 wp_enqueue_script( 'woocommerce_stripe' );
159 }
160
161 /**
162 * Initialize Gateway Settings Form Fields.
163 */
164 public function init_form_fields() {
165 $this->form_fields = require WC_STRIPE_PLUGIN_PATH . '/includes/admin/stripe-sofort-settings.php';
166 }
167
168 /**
169 * Payment form on checkout page
170 */
171 public function payment_fields() {
172 global $wp;
173 $user = wp_get_current_user();
174 $total = WC()->cart->total;
175 $description = $this->get_description();
176
177 // If paying from order, we need to get total from order not cart.
178 if ( isset( $_GET['pay_for_order'] ) && ! empty( $_GET['key'] ) ) {
179 $order = wc_get_order( wc_clean( $wp->query_vars['order-pay'] ) );
180 $total = $order->get_total();
181 }
182
183 if ( is_add_payment_method_page() ) {
184 $pay_button_text = __( 'Add Payment', 'woocommerce-gateway-stripe' );
185 $total = '';
186 } else {
187 $pay_button_text = '';
188 }
189
190 echo '<div
191 id="stripe-sofort-payment-data"
192 data-amount="' . esc_attr( WC_Stripe_Helper::get_stripe_amount( $total ) ) . '"
193 data-currency="' . esc_attr( strtolower( get_woocommerce_currency() ) ) . '">';
194
195 if ( $description ) {
196 echo apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id );
197 }
198
199 echo '</div>';
200 }
201
202 /**
203 * Creates the source for charge.
204 *
205 * @since 4.0.0
206 * @version 4.0.0
207 * @param object $order
208 * @return mixed
209 */
210 public function create_source( $order ) {
211 $currency = $order->get_currency();
212 $bank_country = $order->get_billing_country();
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'] = 'sofort';
218 $post_data['owner'] = $this->get_owner_details( $order );
219 $post_data['redirect'] = [ 'return_url' => $return_url ];
220 $post_data['sofort'] = [
221 'country' => $bank_country,
222 'preferred_language' => $this->get_locale(),
223 ];
224
225 if ( ! empty( $this->statement_descriptor ) ) {
226 $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor );
227 }
228
229 WC_Stripe_Logger::log( 'Info: Begin creating Sofort source' );
230
231 return WC_Stripe_API::request( apply_filters( 'wc_stripe_sofort_source', $post_data, $order ), 'sources' );
232 }
233
234 /**
235 * Process the payment
236 *
237 * @param int $order_id Reference.
238 * @param bool $retry Should we retry on fail.
239 * @param bool $force_save_source Force payment source to be saved.
240 *
241 * @throws Exception If payment will not be accepted.
242 *
243 * @return array|void
244 */
245 public function process_payment( $order_id, $retry = true, $force_save_source = false ) {
246 try {
247 $order = wc_get_order( $order_id );
248
249 // This will throw exception if not valid.
250 $this->validate_minimum_order_amount( $order );
251
252 // This comes from the create account checkbox in the checkout page.
253 $create_account = ! empty( $_POST['createaccount'] ) ? true : false;
254
255 if ( $create_account ) {
256 $new_customer_id = $order->get_customer_id();
257 $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id );
258 $new_stripe_customer->create_customer();
259 }
260
261 $response = $this->create_source( $order );
262
263 if ( ! empty( $response->error ) ) {
264 $order->add_order_note( $response->error->message );
265
266 $localized_messages = WC_Stripe_Helper::get_localized_messages();
267
268 if ( 'invalid_sofort_country' === $response->error->code ) {
269 $localized_message = isset( $localized_messages[ $response->error->code ] ) ? $localized_messages[ $response->error->code ] : $response->error->message;
270 } else {
271 $localized_message = isset( $localized_messages[ $response->error->type ] ) ? $localized_messages[ $response->error->type ] : $response->error->message;
272 }
273
274 throw new WC_Stripe_Exception( print_r( $response, true ), $localized_message );
275 }
276
277 $order->update_meta_data( '_stripe_source_id', $response->id );
278 $order->save();
279
280 WC_Stripe_Logger::log( 'Info: Redirecting to Sofort...' );
281
282 return [
283 'result' => 'success',
284 'redirect' => esc_url_raw( $response->redirect->url ),
285 ];
286 } catch ( WC_Stripe_Exception $e ) {
287 wc_add_notice( $e->getLocalizedMessage(), 'error' );
288 WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() );
289
290 do_action( 'wc_gateway_stripe_process_payment_error', $e, $order );
291
292 if ( $order->has_status(
293 apply_filters(
294 'wc_stripe_allowed_payment_processing_statuses',
295 [ 'pending', 'failed' ],
296 $order
297 )
298 ) ) {
299 $this->send_failed_order_email( $order_id );
300 }
301
302 return [
303 'result' => 'fail',
304 'redirect' => '',
305 ];
306 }
307 }
308}