swissChili | f0cbdc3 | 2023-01-05 17:21:38 -0500 | [diff] [blame^] | 1 | <?php |
| 2 | if ( ! defined( 'ABSPATH' ) ) { |
| 3 | exit; |
| 4 | } |
| 5 | |
| 6 | /** |
| 7 | * Class that handles Bancontact payment method. |
| 8 | * |
| 9 | * @extends WC_Gateway_Stripe |
| 10 | * |
| 11 | * @since 4.0.0 |
| 12 | */ |
| 13 | class WC_Gateway_Stripe_Bancontact extends WC_Stripe_Payment_Gateway { |
| 14 | |
| 15 | const ID = 'stripe_bancontact'; |
| 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 = self::ID; |
| 64 | $this->method_title = __( 'Stripe Bancontact', '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§ion=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_bancontact_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['bancontact'] ) ? $icons['bancontact'] : ''; |
| 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-bancontact-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-bancontact-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 | $return_url = $this->get_stripe_return_url( $order ); |
| 213 | $post_data = []; |
| 214 | $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); |
| 215 | $post_data['currency'] = strtolower( $currency ); |
| 216 | $post_data['type'] = 'bancontact'; |
| 217 | $post_data['owner'] = $this->get_owner_details( $order ); |
| 218 | $post_data['redirect'] = [ 'return_url' => $return_url ]; |
| 219 | $post_data['bancontact'] = [ 'preferred_language' => $this->get_locale() ]; |
| 220 | |
| 221 | if ( ! empty( $this->statement_descriptor ) ) { |
| 222 | $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor ); |
| 223 | } |
| 224 | |
| 225 | WC_Stripe_Logger::log( 'Info: Begin creating Bancontact source' ); |
| 226 | |
| 227 | return WC_Stripe_API::request( apply_filters( 'wc_stripe_bancontact_source', $post_data, $order ), 'sources' ); |
| 228 | } |
| 229 | |
| 230 | /** |
| 231 | * Process the payment |
| 232 | * |
| 233 | * @param int $order_id Reference. |
| 234 | * @param bool $retry Should we retry on fail. |
| 235 | * @param bool $force_save_source Force payment source to be saved. |
| 236 | * |
| 237 | * @throws Exception If payment will not be accepted. |
| 238 | * |
| 239 | * @return array|void |
| 240 | */ |
| 241 | public function process_payment( $order_id, $retry = true, $force_save_source = false ) { |
| 242 | try { |
| 243 | $order = wc_get_order( $order_id ); |
| 244 | |
| 245 | // This will throw exception if not valid. |
| 246 | $this->validate_minimum_order_amount( $order ); |
| 247 | |
| 248 | // This comes from the create account checkbox in the checkout page. |
| 249 | $create_account = ! empty( $_POST['createaccount'] ) ? true : false; |
| 250 | |
| 251 | if ( $create_account ) { |
| 252 | $new_customer_id = $order->get_customer_id(); |
| 253 | $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id ); |
| 254 | $new_stripe_customer->create_customer(); |
| 255 | } |
| 256 | |
| 257 | $response = $this->create_source( $order ); |
| 258 | |
| 259 | if ( ! empty( $response->error ) ) { |
| 260 | $order->add_order_note( $response->error->message ); |
| 261 | |
| 262 | throw new WC_Stripe_Exception( print_r( $response, true ), $response->error->message ); |
| 263 | } |
| 264 | |
| 265 | $order->update_meta_data( '_stripe_source_id', $response->id ); |
| 266 | $order->save(); |
| 267 | |
| 268 | WC_Stripe_Logger::log( 'Info: Redirecting to Bancontact...' ); |
| 269 | |
| 270 | return [ |
| 271 | 'result' => 'success', |
| 272 | 'redirect' => esc_url_raw( $response->redirect->url ), |
| 273 | ]; |
| 274 | } catch ( WC_Stripe_Exception $e ) { |
| 275 | wc_add_notice( $e->getLocalizedMessage(), 'error' ); |
| 276 | WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
| 277 | |
| 278 | do_action( 'wc_gateway_stripe_process_payment_error', $e, $order ); |
| 279 | |
| 280 | if ( $order->has_status( |
| 281 | apply_filters( |
| 282 | 'wc_stripe_allowed_payment_processing_statuses', |
| 283 | [ 'pending', 'failed' ], |
| 284 | $order |
| 285 | ) |
| 286 | ) ) { |
| 287 | $this->send_failed_order_email( $order_id ); |
| 288 | } |
| 289 | |
| 290 | return [ |
| 291 | 'result' => 'fail', |
| 292 | 'redirect' => '', |
| 293 | ]; |
| 294 | } |
| 295 | } |
| 296 | } |