← All changes
|
includes/Admin/Elementor/Form_Actions/Paystack_Integration.php
+56
-18
1.0.3
→
2.4.1
View file →
| @@ -2,9 +2,12 @@ | ||
| 2 | 2 | |
| 3 | 3 | namespace Better_Payment\Lite\Admin\Elementor\Form_Actions; |
| 4 | 4 | |
| 5 | 5 | use Better_Payment\Lite\Admin\DB; |
| 6 | +use Better_Payment\Lite\Campaign\CampaignStats; | |
| 6 | 7 | use Better_Payment\Lite\Classes\Handler; |
| 8 | +use Better_Payment\Lite\Classes\PaymentRequestGuard; | |
| 9 | +use Better_Payment\Lite\Traits\Helper; | |
| 7 | 10 | use Elementor\Controls_Manager; |
| 8 | 11 | use ElementorPro\Modules\Forms\Classes\Action_Base; |
| 9 | 12 | use ElementorPro\Modules\Forms\Classes\Ajax_Handler; |
| 10 | 13 | use ElementorPro\Modules\Forms\Classes\Form_Record; |
| @@ -14,8 +17,10 @@ | ||
| 14 | 17 | * |
| 15 | 18 | * @since 0.0.1 |
| 16 | 19 | */ |
| 17 | 20 | class Paystack_Integration extends Action_Base { |
| 21 | + use Helper; | |
| 22 | + | |
| 18 | 23 | private $better_payment_global_settings = []; |
| 19 | 24 | |
| 20 | 25 | public function __construct() { |
| 21 | 26 | wp_enqueue_script( 'better-payment' ); |
| @@ -36,9 +41,8 @@ | ||
| 36 | 41 | /** |
| 37 | 42 | * @param \Elementor\Widget_Base $widget |
| 38 | 43 | */ |
| 39 | 44 | public function register_settings_section( $widget ) { |
| 40 | - | |
| 41 | 45 | $this->better_payment_global_settings = DB::get_settings(); |
| 42 | 46 | |
| 43 | 47 | $widget->start_controls_section( |
| 44 | 48 | 'section_paystack_payment', |
| @@ -62,8 +66,20 @@ | ||
| 62 | 66 | 'options' => $better_payment_helper->get_currency_list(), |
| 63 | 67 | ] |
| 64 | 68 | ); |
| 65 | 69 | |
| 70 | + $widget->add_control( | |
| 71 | + 'better_payment_paystack_currecny_support_notice', | |
| 72 | + [ | |
| 73 | + 'type' => Controls_Manager::RAW_HTML, | |
| 74 | + 'raw' => __( 'Currency is not supported by Paystack!', 'better-payment' ), | |
| 75 | + 'content_classes' => 'elementor-panel-alert elementor-panel-alert-info', | |
| 76 | + 'condition' => [ | |
| 77 | + 'better_payment_form_paystack_currency!' => $this->bp_supported_currencies( 'paystack' ), | |
| 78 | + ], | |
| 79 | + ] | |
| 80 | + ); | |
| 81 | + | |
| 66 | 82 | $better_payment_is_paystack_live = ! empty( $this->better_payment_global_settings['better_payment_settings_payment_paystack_live_mode'] ) && 'yes' === $this->better_payment_global_settings['better_payment_settings_payment_paystack_live_mode'] ? 1 : 0; |
| 67 | 83 | $paystack_live_key = ! empty( $this->better_payment_global_settings['better_payment_settings_payment_paystack_live_public'] ) ? $this->better_payment_global_settings['better_payment_settings_payment_paystack_live_public'] : ''; |
| 68 | 84 | $paystack_test_key = ! empty( $this->better_payment_global_settings['better_payment_settings_payment_paystack_test_public'] ) ? $this->better_payment_global_settings['better_payment_settings_payment_paystack_test_public'] : ''; |
| 69 | 85 | $paystack_live_secret = ! empty( $this->better_payment_global_settings['better_payment_settings_payment_paystack_live_secret'] ) ? $this->better_payment_global_settings['better_payment_settings_payment_paystack_live_secret'] : ''; |
| @@ -72,9 +88,9 @@ | ||
| 72 | 88 | $widget->add_control( |
| 73 | 89 | 'better_payment_paystack_public_key', |
| 74 | 90 | [ |
| 75 | 91 | 'label' => __( 'Public Key', 'better-payment' ), |
| 76 | - 'type' => Controls_Manager::TEXT, | |
| 92 | + 'type' => current_user_can('manage_options') ? Controls_Manager::TEXT : Controls_Manager::HIDDEN, | |
| 77 | 93 | 'dynamic' => [ |
| 78 | 94 | 'active' => true, |
| 79 | 95 | ], |
| 80 | 96 | 'label_block' => true, |
| @@ -88,9 +104,9 @@ | ||
| 88 | 104 | $widget->add_control( |
| 89 | 105 | 'better_payment_paystack_secret_key', |
| 90 | 106 | [ |
| 91 | 107 | 'label' => __( 'Secret Key', 'better-payment' ), |
| 92 | - 'type' => Controls_Manager::TEXT, | |
| 108 | + 'type' => current_user_can('manage_options') ? Controls_Manager::TEXT : Controls_Manager::HIDDEN, | |
| 93 | 109 | 'input_type' => 'password', |
| 94 | 110 | 'dynamic' => [ |
| 95 | 111 | 'active' => true, |
| 96 | 112 | ], |
| @@ -153,23 +169,24 @@ | ||
| 153 | 169 | $ajax_handler->add_error_message( 'Amount field is required!' ); |
| 154 | 170 | return false; |
| 155 | 171 | } |
| 156 | 172 | |
| 157 | - $amount = floatval( $sent_data['payment_amount'] ); | |
| 158 | - if ( empty( $amount ) && ! empty( $sent_data['primary_payment_amount_radio'] ) ) { | |
| 159 | - $amount = floatval( $sent_data['primary_payment_amount_radio'] ); | |
| 173 | + // Amount and quantity come from the form's field settings, never from the | |
| 174 | + // request — see PaymentRequestGuard. | |
| 175 | + $guarded = ( new PaymentRequestGuard() )->resolve_elementor_form_payment( (array) $el_form_form_settings, (array) $sent_data ); | |
| 176 | + | |
| 177 | + if ( is_wp_error( $guarded ) ) { | |
| 178 | + $ajax_handler->add_error_message( $guarded->get_error_message() ); | |
| 179 | + return false; | |
| 160 | 180 | } |
| 161 | 181 | |
| 162 | - $quantity = 1; | |
| 163 | - if ( !empty( $sent_data[ 'pay_quantity' ] ) ) { | |
| 164 | - $quantity = intval( $sent_data[ 'pay_quantity' ][ 'value' ] ); | |
| 165 | - $amount *= $quantity; | |
| 166 | - } | |
| 182 | + $quantity = $guarded['quantity']; | |
| 183 | + $amount = $guarded['amount']; | |
| 167 | 184 | |
| 168 | - $order_id = 'paystack_' . uniqid(); | |
| 185 | + $order_id = Handler::new_order_id( 'paystack' ); | |
| 169 | 186 | |
| 170 | - $redirection_url_success = ! empty( $el_settings['better_payment_form_success_page_url']['url'] ) ? esc_url( $el_settings['better_payment_form_success_page_url']['url'] ) : get_permalink( $page_id ); | |
| 171 | - $redirection_url_error = ! empty( $el_settings['better_payment_form_error_page_url']['url'] ) ? esc_url( $el_settings['better_payment_form_error_page_url']['url'] ) : get_permalink( $page_id ); | |
| 187 | + $redirection_url_success = get_permalink( $page_id ); | |
| 188 | + $redirection_url_error = get_permalink( $page_id ); | |
| 172 | 189 | |
| 173 | 190 | $request = [ |
| 174 | 191 | 'amount' => ( $amount * 100 ), |
| 175 | 192 | 'cart_id' => $order_id, |
| @@ -211,15 +228,28 @@ | ||
| 211 | 228 | ) |
| 212 | 229 | ); |
| 213 | 230 | |
| 214 | 231 | $response_ar = json_decode( $response['body'] ); |
| 232 | + | |
| 233 | + if ( ! empty( $response_ar->error ) ) { | |
| 234 | + $ajax_handler->add_error_message( sanitize_text_field( $response_ar->error->message ) ); | |
| 235 | + return false; | |
| 236 | + } | |
| 215 | 237 | |
| 238 | + $paystack_supported_currencies = $this->bp_supported_currencies( 'paystack' ); | |
| 239 | + $currency_code = sanitize_text_field( $record->get_form_settings( 'better_payment_form_paystack_currency' ) ); | |
| 240 | + | |
| 241 | + if ( is_array( $paystack_supported_currencies ) && ! in_array( $currency_code, $paystack_supported_currencies ) ) { | |
| 242 | + $ajax_handler->add_error_message( 'Currency is not supported by Paystack!' ); | |
| 243 | + return false; | |
| 244 | + } | |
| 245 | + | |
| 216 | 246 | if ( ! ( empty( $response_ar->status ) || empty( $response_ar->data ) ) ) { |
| 217 | 247 | |
| 218 | 248 | //Form fields data to send via email |
| 219 | 249 | $better_form_fields = [ |
| 220 | 250 | 'email' => ! empty( $sent_data['email'] ) ? sanitize_email( $sent_data['email'] ) : '', |
| 221 | - 'amount' => sanitize_text_field($record->get_form_settings( 'better_payment_form_paystack_currency' )) . floatval( $amount ), | |
| 251 | + 'amount' => $currency_code . floatval( $amount ), | |
| 222 | 252 | 'el_form_fields' => maybe_serialize($sent_data), |
| 223 | 253 | 'referer_page_id' => $page_id, |
| 224 | 254 | 'referer_widget_id' => $widget_id, |
| 225 | 255 | 'source' => 'paystack' |
| @@ -224,8 +254,11 @@ | ||
| 224 | 254 | 'referer_widget_id' => $widget_id, |
| 225 | 255 | 'source' => 'paystack' |
| 226 | 256 | ]; |
| 227 | 257 | |
| 258 | + // Get campaign_id from form data if available | |
| 259 | + $campaign_id = $guarded['campaign_id']; | |
| 260 | + | |
| 228 | 261 | Handler::payment_create( |
| 229 | 262 | [ |
| 230 | 263 | 'amount' => $amount, |
| 231 | 264 | 'order_id' => $order_id, |
| @@ -230,17 +263,22 @@ | ||
| 230 | 263 | 'amount' => $amount, |
| 231 | 264 | 'order_id' => $order_id, |
| 232 | 265 | 'payment_date' => date( 'Y-m-d H:i:s' ), |
| 233 | 266 | 'source' => 'paystack', |
| 234 | - 'transaction_id' => '', | |
| 267 | + // Paystack's reference, so verification can bind it to this order. | |
| 268 | + 'transaction_id' => ! empty( $response_ar->data->reference ) ? sanitize_text_field( $response_ar->data->reference ) : '', | |
| 235 | 269 | 'customer_info' => maybe_serialize( $response_ar ), |
| 236 | 270 | 'form_fields_info' => maybe_serialize( $better_form_fields ), |
| 237 | - 'obj_id' => sanitize_text_field($response_ar->id), | |
| 271 | + 'obj_id' => !empty($response_ar->id) ? sanitize_text_field($response_ar->id) : '', | |
| 238 | 272 | 'status' => 'unpaid', |
| 239 | - 'currency' => sanitize_text_field($record->get_form_settings( 'better_payment_form_paystack_currency' )), | |
| 273 | + 'currency' => $currency_code, | |
| 240 | 274 | 'referer' => "elementor-form", |
| 275 | + 'campaign_id' => $campaign_id, | |
| 241 | 276 | ] |
| 242 | 277 | ); |
| 278 | + if ( $campaign_id ) { | |
| 279 | + CampaignStats::bust_cache( (int) $campaign_id ); | |
| 280 | + } | |
| 243 | 281 | |
| 244 | 282 | $authorization_url = ! empty( $response_ar->data->authorization_url ) ? esc_url_raw( $response_ar->data->authorization_url ) : ''; |
| 245 | 283 | |
| 246 | 284 | $ajax_handler->add_response_data( 'better_paystack_data', [ |