| @@ -1,0 +1,169 @@ | ||
| 1 | +<?php | |
| 2 | + | |
| 3 | +namespace ABlocks\Ajax; | |
| 4 | + | |
| 5 | +if ( ! defined( 'ABSPATH' ) ) { | |
| 6 | + exit; | |
| 7 | +} | |
| 8 | + | |
| 9 | +use ABlocks\Classes\AbstractAjaxHandler; | |
| 10 | +use ABlocks\Classes\FileUpload; | |
| 11 | +use ABlocks\Helper; | |
| 12 | +use ABlocks\Classes\Sanitizer; | |
| 13 | +use ABlocks\Blocks\FormBuilder\Actions\Email; | |
| 14 | +use ABlocks\Blocks\FormBuilder\Actions\Email2; | |
| 15 | +use ABlocks\Blocks\FormBuilder\Actions\Submission; | |
| 16 | + | |
| 17 | +use ABlocks\Blocks\StripeButton\StripePayment; | |
| 18 | + | |
| 19 | +class StripePaymentAjax extends AbstractAjaxHandler { | |
| 20 | + public function __construct() { | |
| 21 | + $this->actions = [ | |
| 22 | + 'stripe_get_tax_rates' => [ | |
| 23 | + 'callback' => [ $this, 'get_tax_rates' ], | |
| 24 | + 'capability' => 'edit_posts', | |
| 25 | + 'fields' => array( | |
| 26 | + 'api_key' => 'string', | |
| 27 | + ) | |
| 28 | + ], | |
| 29 | + 'stripe_payment_process' => [ | |
| 30 | + 'callback' => [ $this, 'payment_process' ], | |
| 31 | + 'allow_visitor_action' => true, | |
| 32 | + 'fields' => array( | |
| 33 | + 'current_post_id' => 'integer', | |
| 34 | + 'block_id' => 'string', | |
| 35 | + ) | |
| 36 | + ], | |
| 37 | + ]; | |
| 38 | + } | |
| 39 | + | |
| 40 | + public function get_tax_rates( $payload ) { | |
| 41 | + | |
| 42 | + $api_key = $payload['api_key'] ?? ''; | |
| 43 | + if ( | |
| 44 | + empty( $api_key ) | |
| 45 | + ) { | |
| 46 | + wp_send_json_error([ | |
| 47 | + 'message' => __( 'API KEY is required.', 'ablocks' ) | |
| 48 | + ]); | |
| 49 | + } | |
| 50 | + | |
| 51 | + $data = ( new StripePayment( $api_key, [] ) )->taxe_rates(); | |
| 52 | + $error = ( $data['error']['message'] ?? false ); | |
| 53 | + if ( $error ) { | |
| 54 | + wp_send_json_error([ | |
| 55 | + 'message' => sanitize_text_field( $error ) | |
| 56 | + ]); | |
| 57 | + } | |
| 58 | + | |
| 59 | + wp_send_json_success( $data ); | |
| 60 | + } | |
| 61 | + public function payment_process( $payload ) { | |
| 62 | + $block_data = Helper::get_block_attributes( | |
| 63 | + $payload['current_post_id'], | |
| 64 | + $payload['block_id'], | |
| 65 | + 'ablocks/stripe-button' | |
| 66 | + ); | |
| 67 | + | |
| 68 | + // check api key exists or not | |
| 69 | + $api_key = $block_data['parentAttributes']['stripeApi'] ?? ''; | |
| 70 | + if ( | |
| 71 | + empty( $api_key ) | |
| 72 | + ) { | |
| 73 | + wp_send_json_error([ | |
| 74 | + 'message' => __( 'API KEY is required.', 'ablocks' ) | |
| 75 | + ]); | |
| 76 | + } | |
| 77 | + | |
| 78 | + $data = []; | |
| 79 | + | |
| 80 | + // product name | |
| 81 | + $product_name = $block_data['parentAttributes']['itemName'] ?? ''; | |
| 82 | + if ( | |
| 83 | + ! empty( $product_name ) | |
| 84 | + ) { | |
| 85 | + $data['product_name'] = $product_name; | |
| 86 | + } | |
| 87 | + // currency | |
| 88 | + $currency = $block_data['parentAttributes']['currency'] ?? ''; | |
| 89 | + if ( | |
| 90 | + ! empty( $currency ) | |
| 91 | + ) { | |
| 92 | + $data['currency'] = $currency; | |
| 93 | + } | |
| 94 | + // unit price | |
| 95 | + $price = floatval( $block_data['parentAttributes']['price'] ?? 0 ); | |
| 96 | + if ( | |
| 97 | + is_float( $price ) | |
| 98 | + ) { | |
| 99 | + $data['unit_amount'] = $price * 100; // convert to cent | |
| 100 | + } | |
| 101 | + // quatity | |
| 102 | + $quantity = intval( $block_data['parentAttributes']['quantity'] ?? 0 ); | |
| 103 | + if ( | |
| 104 | + is_int( $quantity ) | |
| 105 | + ) { | |
| 106 | + $data['quantity'] = $quantity; | |
| 107 | + } | |
| 108 | + // shpping charge | |
| 109 | + $shipping_charge = floatval( $block_data['parentAttributes']['shippingPrice'] ?? 0 ); | |
| 110 | + if ( | |
| 111 | + is_float( $shipping_charge ) | |
| 112 | + ) { | |
| 113 | + $data['shipping_charge'] = $shipping_charge * 100; // convert to cent | |
| 114 | + } | |
| 115 | + // tax rate | |
| 116 | + $tax = $block_data['parentAttributes']['taxId'] ?? ''; | |
| 117 | + if ( | |
| 118 | + ! empty( $tax ) | |
| 119 | + ) { | |
| 120 | + $data['tax_rate'] = $tax; | |
| 121 | + } | |
| 122 | + // redirection after payment | |
| 123 | + $success_url = ( $block_data['parentAttributes']['redirectionAfterPayment'] ?? '' ); | |
| 124 | + if ( | |
| 125 | + ! empty( $success_url ) | |
| 126 | + ) { | |
| 127 | + $data['success_url'] = esc_url( $success_url ); | |
| 128 | + } else { | |
| 129 | + $data['success_url'] = esc_url( get_permalink( $payload['current_post_id'] ) ); | |
| 130 | + } | |
| 131 | + | |
| 132 | + // check if data is insufficient or empty | |
| 133 | + if ( | |
| 134 | + empty( $data ) || | |
| 135 | + ! isset( $data['unit_amount'] ) || | |
| 136 | + ! isset( $data['quantity'] ) | |
| 137 | + ) { | |
| 138 | + wp_send_json_error([ | |
| 139 | + 'message' => __( 'Error. [1]', 'ablocks' ), | |
| 140 | + ]); | |
| 141 | + } | |
| 142 | + | |
| 143 | + $res = ( new StripePayment( $api_key, $data ) )->process(); | |
| 144 | + $error = $res['error']['message'] ?? false; | |
| 145 | + if ( empty( $res ) || ( $error ) ) { | |
| 146 | + // check if custom error msg is defined or not | |
| 147 | + $msg = $block_data['parentAttributes']['redirectionAfterPayment']['errorMessage'] ?? false; | |
| 148 | + if ( | |
| 149 | + ( $block_data['parentAttributes']['redirectionAfterPayment']['customMessage'] ?? false ) !== false && | |
| 150 | + ! empty( $msg ) | |
| 151 | + ) { | |
| 152 | + wp_send_json_error([ | |
| 153 | + 'message' => sanitize_text_field( $msg ) | |
| 154 | + ]); | |
| 155 | + } | |
| 156 | + | |
| 157 | + wp_send_json_error([ | |
| 158 | + 'message' => __( 'Error. [2]', 'ablocks' ) | |
| 159 | + ]); | |
| 160 | + } | |
| 161 | + | |
| 162 | + wp_send_json_success([ | |
| 163 | + 'message' => __( 'Success!', 'ablocks' ), | |
| 164 | + 'redirect_url' => $res['url'] | |
| 165 | + ]); | |
| 166 | + } | |
| 167 | + | |
| 168 | + | |
| 169 | +} | |