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 Boleto payment method. |
| 8 | * |
| 9 | * @extends WC_Gateway_Stripe |
| 10 | * |
| 11 | * @since 5.8.0 |
| 12 | */ |
| 13 | class WC_Gateway_Stripe_Boleto extends WC_Stripe_Payment_Gateway_Voucher { |
| 14 | |
| 15 | /** |
| 16 | * ID used by UPE |
| 17 | * |
| 18 | * @var string |
| 19 | */ |
| 20 | const ID = 'stripe_boleto'; |
| 21 | |
| 22 | /** |
| 23 | * ID used by WooCommerce to identify the payment method |
| 24 | * |
| 25 | * @var string |
| 26 | */ |
| 27 | public $id = 'stripe_boleto'; |
| 28 | |
| 29 | /** |
| 30 | * ID used by stripe |
| 31 | */ |
| 32 | protected $stripe_id = 'boleto'; |
| 33 | |
| 34 | /** |
| 35 | * List of accepted currencies |
| 36 | * |
| 37 | * @var array |
| 38 | */ |
| 39 | protected $supported_currencies = [ 'BRL' ]; |
| 40 | |
| 41 | /** |
| 42 | * List of accepted countries |
| 43 | */ |
| 44 | protected $supported_countries = [ 'BR' ]; |
| 45 | |
| 46 | /** |
| 47 | * Constructor |
| 48 | * |
| 49 | * @since 5.8.0 |
| 50 | */ |
| 51 | public function __construct() { |
| 52 | $this->method_title = __( 'Stripe Boleto', 'woocommerce-gateway-stripe' ); |
| 53 | parent::__construct(); |
| 54 | |
| 55 | add_filter( 'wc_stripe_allowed_payment_processing_statuses', [ $this, 'add_allowed_payment_processing_statuses' ], 10, 2 ); |
| 56 | } |
| 57 | |
| 58 | /** |
| 59 | * Add payment gateway voucher expiration to API request body. |
| 60 | * |
| 61 | * @param array $body API request body. |
| 62 | * @return array |
| 63 | */ |
| 64 | protected function update_request_body_on_create_or_update_payment_intent( $body ) { |
| 65 | $body['payment_method_options'] = [ |
| 66 | 'boleto' => [ |
| 67 | 'expires_after_days' => $this->get_option( 'expiration' ), |
| 68 | ], |
| 69 | ]; |
| 70 | return $body; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Add payment gateway voucher expiration. |
| 75 | * |
| 76 | * @param array $settings Settings array. |
| 77 | * @return array |
| 78 | */ |
| 79 | public function get_unique_settings( $settings ) { |
| 80 | $settings[ $this->id . '_expiration' ] = $this->get_option( 'expiration' ); |
| 81 | return $settings; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Updates payment gateway voucher expiration. |
| 86 | * |
| 87 | * @param WP_REST_Request $request Request object. |
| 88 | * @return void |
| 89 | */ |
| 90 | public function update_unique_settings( WP_REST_Request $request ) { |
| 91 | $field_name = $this->id . '_expiration'; |
| 92 | $expiration = $request->get_param( $field_name ); |
| 93 | |
| 94 | if ( null === $expiration ) { |
| 95 | return; |
| 96 | } |
| 97 | |
| 98 | $value = absint( $expiration ); |
| 99 | $value = min( 60, $value ); |
| 100 | $value = max( 0, $value ); |
| 101 | $this->update_option( 'expiration', $value ); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Adds on-hold as accepted status during webhook handling on orders paid with voucher |
| 106 | * |
| 107 | * @param $allowed_statuses |
| 108 | * @param $order |
| 109 | * |
| 110 | * @return mixed |
| 111 | */ |
| 112 | public function add_allowed_payment_processing_statuses( $allowed_statuses, $order ) { |
| 113 | if ( $this->stripe_id === $order->get_meta( '_stripe_upe_payment_type' ) && ! in_array( 'on-hold', $allowed_statuses ) ) { |
| 114 | $allowed_statuses[] = 'on-hold'; |
| 115 | } |
| 116 | |
| 117 | return $allowed_statuses; |
| 118 | } |
| 119 | |
| 120 | /** |
| 121 | * Payment_scripts function. |
| 122 | * |
| 123 | * @since 5.8.0 |
| 124 | */ |
| 125 | public function payment_scripts() { |
| 126 | if ( ! is_cart() && ! is_checkout() && ! isset( $_GET['pay_for_order'] ) && ! is_add_payment_method_page() ) { |
| 127 | return; |
| 128 | } |
| 129 | |
| 130 | parent::payment_scripts(); |
| 131 | wp_enqueue_script( 'jquery-mask', plugins_url( 'assets/js/jquery.mask.min.js', WC_STRIPE_MAIN_FILE ), [], WC_STRIPE_VERSION ); |
| 132 | } |
| 133 | |
| 134 | /** |
| 135 | * Payment form on checkout page |
| 136 | * |
| 137 | * @since 5.8.0 |
| 138 | */ |
| 139 | public function payment_fields() { |
| 140 | $description = $this->get_description(); |
| 141 | apply_filters( 'wc_stripe_description', wpautop( wp_kses_post( $description ) ), $this->id ) |
| 142 | |
| 143 | ?> |
| 144 | <label>CPF/CNPJ: <abbr class="required" title="required">*</abbr></label><br> |
| 145 | <input id="stripe_boleto_tax_id" name="stripe_boleto_tax_id" type="text"><br><br> |
| 146 | <div class="stripe-source-errors" role="alert"></div> |
| 147 | |
| 148 | <div id="stripe-boleto-payment-data"><?php echo $description; ?></div> |
| 149 | <?php |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Validates the minimum and maximum amount. Throws exception when out of range value is added |
| 154 | * |
| 155 | * @since 5.8.0 |
| 156 | * |
| 157 | * @param $amount |
| 158 | * |
| 159 | * @throws WC_Stripe_Exception |
| 160 | */ |
| 161 | protected function validate_amount_limits( $amount ) { |
| 162 | |
| 163 | if ( $amount < 5.00 ) { |
| 164 | /* translators: 1) amount (including currency symbol) */ |
| 165 | throw new WC_Stripe_Exception( sprintf( __( 'Sorry, the minimum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( 5.00 ) ) ); |
| 166 | } elseif ( $amount > 49999.99 ) { |
| 167 | /* translators: 1) amount (including currency symbol) */ |
| 168 | throw new WC_Stripe_Exception( sprintf( __( 'Sorry, the maximum allowed order total is %1$s to use this payment method.', 'woocommerce-gateway-stripe' ), wc_price( 49999.99 ) ) ); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Gather the data necessary to confirm the payment via javascript |
| 174 | * Override this when extending the class |
| 175 | * |
| 176 | * @param WC_Order $order |
| 177 | * |
| 178 | * @return array |
| 179 | */ |
| 180 | protected function get_confirm_payment_data( $order ) { |
| 181 | return [ |
| 182 | 'payment_method' => [ |
| 183 | 'boleto' => [ |
| 184 | 'tax_id' => isset( $_POST['stripe_boleto_tax_id'] ) ? wc_clean( wp_unslash( $_POST['stripe_boleto_tax_id'] ) ) : null, |
| 185 | ], |
| 186 | 'billing_details' => [ |
| 187 | 'name' => $order->get_formatted_billing_full_name(), |
| 188 | 'email' => $order->get_billing_email(), |
| 189 | 'address' => [ |
| 190 | 'line1' => $order->get_billing_address_1(), |
| 191 | 'city' => $order->get_billing_city(), |
| 192 | 'state' => $order->get_billing_state(), |
| 193 | 'postal_code' => $order->get_billing_postcode(), |
| 194 | 'country' => $order->get_billing_country(), |
| 195 | ], |
| 196 | ], |
| 197 | ], |
| 198 | ]; |
| 199 | } |
| 200 | } |