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 Multibanco payment method. |
| 8 | * |
| 9 | * @extends WC_Gateway_Stripe |
| 10 | * |
| 11 | * @since 4.1.0 |
| 12 | */ |
| 13 | class WC_Gateway_Stripe_Multibanco extends WC_Stripe_Payment_Gateway { |
| 14 | /** |
| 15 | * Notices (array) |
| 16 | * |
| 17 | * @var array |
| 18 | */ |
| 19 | public $notices = []; |
| 20 | |
| 21 | /** |
| 22 | * Is test mode active? |
| 23 | * |
| 24 | * @var bool |
| 25 | */ |
| 26 | public $testmode; |
| 27 | |
| 28 | /** |
| 29 | * Alternate credit card statement name |
| 30 | * |
| 31 | * @var bool |
| 32 | */ |
| 33 | public $statement_descriptor; |
| 34 | |
| 35 | /** |
| 36 | * API access secret key |
| 37 | * |
| 38 | * @var string |
| 39 | */ |
| 40 | public $secret_key; |
| 41 | |
| 42 | /** |
| 43 | * Api access publishable key |
| 44 | * |
| 45 | * @var string |
| 46 | */ |
| 47 | public $publishable_key; |
| 48 | |
| 49 | /** |
| 50 | * Should we store the users credit cards? |
| 51 | * |
| 52 | * @var bool |
| 53 | */ |
| 54 | public $saved_cards; |
| 55 | |
| 56 | /** |
| 57 | * Constructor |
| 58 | */ |
| 59 | public function __construct() { |
| 60 | $this->id = 'stripe_multibanco'; |
| 61 | $this->method_title = __( 'Stripe Multibanco', 'woocommerce-gateway-stripe' ); |
| 62 | $this->method_description = sprintf( |
| 63 | /* translators: 1) HTML anchor open tag 2) HTML anchor closing tag */ |
| 64 | __( 'All other general Stripe settings can be adjusted %1$shere%2$s.', 'woocommerce-gateway-stripe' ), |
| 65 | '<a href="' . esc_url( admin_url( 'admin.php?page=wc-settings&tab=checkout§ion=stripe' ) ) . '">', |
| 66 | '</a>' |
| 67 | ); |
| 68 | $this->supports = [ |
| 69 | 'products', |
| 70 | 'refunds', |
| 71 | ]; |
| 72 | |
| 73 | // Load the form fields. |
| 74 | $this->init_form_fields(); |
| 75 | |
| 76 | // Load the settings. |
| 77 | $this->init_settings(); |
| 78 | |
| 79 | $main_settings = get_option( 'woocommerce_stripe_settings' ); |
| 80 | $this->title = $this->get_option( 'title' ); |
| 81 | $this->description = $this->get_option( 'description' ); |
| 82 | $this->enabled = $this->get_option( 'enabled' ); |
| 83 | $this->testmode = ( ! empty( $main_settings['testmode'] ) && 'yes' === $main_settings['testmode'] ) ? true : false; |
| 84 | $this->saved_cards = ( ! empty( $main_settings['saved_cards'] ) && 'yes' === $main_settings['saved_cards'] ) ? true : false; |
| 85 | $this->publishable_key = ! empty( $main_settings['publishable_key'] ) ? $main_settings['publishable_key'] : ''; |
| 86 | $this->secret_key = ! empty( $main_settings['secret_key'] ) ? $main_settings['secret_key'] : ''; |
| 87 | $this->statement_descriptor = ! empty( $main_settings['statement_descriptor'] ) ? $main_settings['statement_descriptor'] : ''; |
| 88 | |
| 89 | if ( $this->testmode ) { |
| 90 | $this->publishable_key = ! empty( $main_settings['test_publishable_key'] ) ? $main_settings['test_publishable_key'] : ''; |
| 91 | $this->secret_key = ! empty( $main_settings['test_secret_key'] ) ? $main_settings['test_secret_key'] : ''; |
| 92 | } |
| 93 | |
| 94 | add_action( 'woocommerce_update_options_payment_gateways_' . $this->id, [ $this, 'process_admin_options' ] ); |
| 95 | add_action( 'wp_enqueue_scripts', [ $this, 'payment_scripts' ] ); |
| 96 | add_action( 'woocommerce_thankyou_stripe_multibanco', [ $this, 'thankyou_page' ] ); |
| 97 | |
| 98 | // Customer Emails |
| 99 | add_action( 'woocommerce_email_before_order_table', [ $this, 'email_instructions' ], 10, 3 ); |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Returns all supported currencies for this payment method. |
| 104 | * |
| 105 | * @since 4.1.0 |
| 106 | * @version 4.1.0 |
| 107 | * @return array |
| 108 | */ |
| 109 | public function get_supported_currency() { |
| 110 | return apply_filters( |
| 111 | 'wc_stripe_multibanco_supported_currencies', |
| 112 | [ |
| 113 | 'EUR', |
| 114 | ] |
| 115 | ); |
| 116 | } |
| 117 | |
| 118 | /** |
| 119 | * Checks to see if all criteria is met before showing payment method. |
| 120 | * |
| 121 | * @since 4.1.0 |
| 122 | * @version 4.1.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.1.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['multibanco'] ) ? $icons['multibanco'] : ''; |
| 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-multibanco-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-multibanco-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 | * Output for the order received page. |
| 205 | * |
| 206 | * @param int $order_id |
| 207 | */ |
| 208 | public function thankyou_page( $order_id ) { |
| 209 | $this->get_instructions( $order_id ); |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * Add content to the WC emails. |
| 214 | * |
| 215 | * @since 4.1.0 |
| 216 | * @version 4.1.0 |
| 217 | * @param WC_Order $order |
| 218 | * @param bool $sent_to_admin |
| 219 | * @param bool $plain_text |
| 220 | */ |
| 221 | public function email_instructions( $order, $sent_to_admin, $plain_text = false ) { |
| 222 | $order_id = $order->get_id(); |
| 223 | |
| 224 | $payment_method = $order->get_payment_method(); |
| 225 | |
| 226 | if ( ! $sent_to_admin && 'stripe_multibanco' === $payment_method && $order->has_status( 'on-hold' ) ) { |
| 227 | WC_Stripe_Logger::log( 'Sending multibanco email for order #' . $order_id ); |
| 228 | |
| 229 | $this->get_instructions( $order, $plain_text ); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
| 234 | * Gets the Multibanco instructions for customer to pay. |
| 235 | * |
| 236 | * @since 4.1.0 |
| 237 | * @version 4.1.0 |
| 238 | * @param int|WC_Order $order |
| 239 | */ |
| 240 | public function get_instructions( $order, $plain_text = false ) { |
| 241 | if ( true === is_int( $order ) ) { |
| 242 | $order = wc_get_order( $order ); |
| 243 | } |
| 244 | |
| 245 | $data = $order->get_meta( '_stripe_multibanco' ); |
| 246 | |
| 247 | if ( $plain_text ) { |
| 248 | esc_html_e( 'MULTIBANCO INFORMAÇÕES DE ENCOMENDA:', 'woocommerce-gateway-stripe' ) . "\n\n"; |
| 249 | echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"; |
| 250 | esc_html_e( 'Montante:', 'woocommerce-gateway-stripe' ) . "\n\n"; |
| 251 | echo $data['amount'] . "\n\n"; |
| 252 | echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"; |
| 253 | esc_html_e( 'Entidade:', 'woocommerce-gateway-stripe' ) . "\n\n"; |
| 254 | echo $data['entity'] . "\n\n"; |
| 255 | echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=\n\n"; |
| 256 | esc_html_e( 'Referencia:', 'woocommerce-gateway-stripe' ) . "\n\n"; |
| 257 | echo $data['reference'] . "\n\n"; |
| 258 | } else { |
| 259 | ?> |
| 260 | <h3><?php esc_html_e( 'MULTIBANCO INFORMAÇÕES DE ENCOMENDA:', 'woocommerce-gateway-stripe' ); ?></h3> |
| 261 | <ul class="woocommerce-order-overview woocommerce-thankyou-order-details order_details"> |
| 262 | <li class="woocommerce-order-overview__order order"> |
| 263 | <?php esc_html_e( 'Montante:', 'woocommerce-gateway-stripe' ); ?> |
| 264 | <strong><?php echo $data['amount']; ?></strong> |
| 265 | </li> |
| 266 | <li class="woocommerce-order-overview__order order"> |
| 267 | <?php esc_html_e( 'Entidade:', 'woocommerce-gateway-stripe' ); ?> |
| 268 | <strong><?php echo $data['entity']; ?></strong> |
| 269 | </li> |
| 270 | <li class="woocommerce-order-overview__order order"> |
| 271 | <?php esc_html_e( 'Referencia:', 'woocommerce-gateway-stripe' ); ?> |
| 272 | <strong><?php echo $data['reference']; ?></strong> |
| 273 | </li> |
| 274 | </ul> |
| 275 | <?php |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | /** |
| 280 | * Saves Multibanco information to the order meta for later use. |
| 281 | * |
| 282 | * @since 4.1.0 |
| 283 | * @version 4.1.0 |
| 284 | * @param object $order |
| 285 | * @param object $source_object |
| 286 | */ |
| 287 | public function save_instructions( $order, $source_object ) { |
| 288 | $data = [ |
| 289 | 'amount' => $order->get_formatted_order_total(), |
| 290 | 'entity' => $source_object->multibanco->entity, |
| 291 | 'reference' => $source_object->multibanco->reference, |
| 292 | ]; |
| 293 | |
| 294 | $order_id = $order->get_id(); |
| 295 | |
| 296 | $order->update_meta_data( '_stripe_multibanco', $data ); |
| 297 | } |
| 298 | |
| 299 | /** |
| 300 | * Creates the source for charge. |
| 301 | * |
| 302 | * @since 4.1.0 |
| 303 | * @version 4.1.0 |
| 304 | * @param object $order |
| 305 | * @return mixed |
| 306 | */ |
| 307 | public function create_source( $order ) { |
| 308 | $currency = $order->get_currency(); |
| 309 | $return_url = $this->get_stripe_return_url( $order ); |
| 310 | $post_data = []; |
| 311 | $post_data['amount'] = WC_Stripe_Helper::get_stripe_amount( $order->get_total(), $currency ); |
| 312 | $post_data['currency'] = strtolower( $currency ); |
| 313 | $post_data['type'] = 'multibanco'; |
| 314 | $post_data['owner'] = $this->get_owner_details( $order ); |
| 315 | $post_data['redirect'] = [ 'return_url' => $return_url ]; |
| 316 | |
| 317 | if ( ! empty( $this->statement_descriptor ) ) { |
| 318 | $post_data['statement_descriptor'] = WC_Stripe_Helper::clean_statement_descriptor( $this->statement_descriptor ); |
| 319 | } |
| 320 | |
| 321 | WC_Stripe_Logger::log( 'Info: Begin creating Multibanco source' ); |
| 322 | |
| 323 | return WC_Stripe_API::request( $post_data, 'sources' ); |
| 324 | } |
| 325 | |
| 326 | /** |
| 327 | * Process the payment |
| 328 | * |
| 329 | * @param int $order_id Reference. |
| 330 | * @param bool $retry Should we retry on fail. |
| 331 | * @param bool $force_save_source Force payment source to be saved. |
| 332 | * |
| 333 | * @throws Exception If payment will not be accepted. |
| 334 | * |
| 335 | * @return array|void |
| 336 | */ |
| 337 | public function process_payment( $order_id, $retry = true, $force_save_source = false ) { |
| 338 | try { |
| 339 | $order = wc_get_order( $order_id ); |
| 340 | |
| 341 | // This will throw exception if not valid. |
| 342 | $this->validate_minimum_order_amount( $order ); |
| 343 | |
| 344 | // This comes from the create account checkbox in the checkout page. |
| 345 | $create_account = ! empty( $_POST['createaccount'] ) ? true : false; |
| 346 | |
| 347 | if ( $create_account ) { |
| 348 | $new_customer_id = $order->get_customer_id(); |
| 349 | $new_stripe_customer = new WC_Stripe_Customer( $new_customer_id ); |
| 350 | $new_stripe_customer->create_customer(); |
| 351 | } |
| 352 | |
| 353 | $response = $this->create_source( $order ); |
| 354 | |
| 355 | if ( ! empty( $response->error ) ) { |
| 356 | $order->add_order_note( $response->error->message ); |
| 357 | |
| 358 | throw new Exception( $response->error->message ); |
| 359 | } |
| 360 | |
| 361 | $order->update_meta_data( '_stripe_source_id', $response->id ); |
| 362 | $order->save(); |
| 363 | |
| 364 | $this->save_instructions( $order, $response ); |
| 365 | |
| 366 | // Mark as on-hold (we're awaiting the payment) |
| 367 | $order->update_status( 'on-hold', __( 'Awaiting Multibanco payment', 'woocommerce-gateway-stripe' ) ); |
| 368 | |
| 369 | // Reduce stock levels |
| 370 | wc_reduce_stock_levels( $order_id ); |
| 371 | |
| 372 | // Remove cart |
| 373 | WC()->cart->empty_cart(); |
| 374 | |
| 375 | WC_Stripe_Logger::log( 'Info: Redirecting to Multibanco...' ); |
| 376 | |
| 377 | return [ |
| 378 | 'result' => 'success', |
| 379 | 'redirect' => esc_url_raw( $response->redirect->url ), |
| 380 | ]; |
| 381 | } catch ( Exception $e ) { |
| 382 | wc_add_notice( $e->getMessage(), 'error' ); |
| 383 | WC_Stripe_Logger::log( 'Error: ' . $e->getMessage() ); |
| 384 | |
| 385 | do_action( 'wc_gateway_stripe_process_payment_error', $e, $order ); |
| 386 | |
| 387 | if ( $order->has_status( |
| 388 | apply_filters( |
| 389 | 'wc_stripe_allowed_payment_processing_statuses', |
| 390 | [ 'pending', 'failed' ], |
| 391 | $order |
| 392 | ) |
| 393 | ) ) { |
| 394 | $this->send_failed_order_email( $order_id ); |
| 395 | } |
| 396 | |
| 397 | return [ |
| 398 | 'result' => 'fail', |
| 399 | 'redirect' => '', |
| 400 | ]; |
| 401 | } |
| 402 | } |
| 403 | } |