← All changes
|
includes/Admin/Elementor/Form_Actions/Stripe_Integration.php
+17
-11
2.3.0
→
2.4.1
View file →
| @@ -4,8 +4,9 @@ | ||
| 4 | 4 | |
| 5 | 5 | use Better_Payment\Lite\Admin\DB; |
| 6 | 6 | use Better_Payment\Lite\Campaign\CampaignStats; |
| 7 | 7 | use Better_Payment\Lite\Classes\Handler; |
| 8 | +use Better_Payment\Lite\Classes\PaymentRequestGuard; | |
| 8 | 9 | use Elementor\Controls_Manager; |
| 9 | 10 | use ElementorPro\Modules\Forms\Classes\Action_Base; |
| 10 | 11 | use ElementorPro\Modules\Forms\Classes\Ajax_Handler; |
| 11 | 12 | use ElementorPro\Modules\Forms\Classes\Form_Record; |
| @@ -166,19 +167,22 @@ | ||
| 166 | 167 | $ajax_handler->add_error_message( 'Amount field is required!' ); |
| 167 | 168 | return false; |
| 168 | 169 | } |
| 169 | 170 | |
| 170 | - $amount = isset( $sent_data['payment_amount'] ) ? floatval($sent_data['payment_amount']) : 0; | |
| 171 | - if ( empty( $amount ) && ! empty( $sent_data['primary_payment_amount_radio'] ) ) { | |
| 172 | - $amount = floatval( $sent_data['primary_payment_amount_radio'] ); | |
| 171 | + // Amount and quantity come from the form's field settings, never from the | |
| 172 | + // request — see PaymentRequestGuard. | |
| 173 | + $guarded = ( new PaymentRequestGuard() )->resolve_elementor_form_payment( (array) $el_form_form_settings, (array) $sent_data ); | |
| 174 | + | |
| 175 | + if ( is_wp_error( $guarded ) ) { | |
| 176 | + $ajax_handler->add_error_message( $guarded->get_error_message() ); | |
| 177 | + return false; | |
| 173 | 178 | } |
| 174 | - $quantity = 1; | |
| 175 | - if ( !empty( $sent_data[ 'pay_quantity' ] ) ) { | |
| 176 | - $quantity = intval( $sent_data[ 'pay_quantity' ][ 'value' ] ); | |
| 177 | - $amount *= $quantity; | |
| 178 | - } | |
| 179 | 179 | |
| 180 | - $order_id = 'stripe_' . uniqid(); | |
| 180 | + $unit_amount = $guarded['unit_amount']; | |
| 181 | + $quantity = $guarded['quantity']; | |
| 182 | + $amount = $guarded['amount']; | |
| 183 | + | |
| 184 | + $order_id = Handler::new_order_id( 'stripe' ); | |
| 181 | 185 | $request = [ |
| 182 | 186 | 'success_url' => add_query_arg( [ |
| 183 | 187 | 'better_payment_stripe_status' => 'success', |
| 184 | 188 | 'better_payment_stripe_id' => $order_id, |
| @@ -197,9 +201,11 @@ | ||
| 197 | 201 | 'order_id' => $order_id |
| 198 | 202 | ], |
| 199 | 203 | 'line_items' => [ |
| 200 | 204 | [ |
| 201 | - 'amount' => ( $amount * 100 ), | |
| 205 | + // Per unit: Stripe multiplies by `quantity` itself. Sending the | |
| 206 | + // quantity-multiplied total here charged quantity² units. | |
| 207 | + 'amount' => (int) round( $unit_amount * 100 ), | |
| 202 | 208 | 'currency' => sanitize_text_field($record->get_form_settings( 'better_payment_form_stripe_currency' )), |
| 203 | 209 | 'name' => sanitize_text_field( $record->get_form_settings( 'form_name' ) ), |
| 204 | 210 | 'quantity' => $quantity |
| 205 | 211 | ] |
| @@ -264,9 +270,9 @@ | ||
| 264 | 270 | 'source' => 'stripe' |
| 265 | 271 | ]; |
| 266 | 272 | |
| 267 | 273 | // Get campaign_id from form data if available |
| 268 | - $campaign_id = ! empty( $sent_data['campaign_id'] ) ? sanitize_text_field( $sent_data['campaign_id'] ) : ''; | |
| 274 | + $campaign_id = $guarded['campaign_id']; | |
| 269 | 275 | |
| 270 | 276 | Handler::payment_create( |
| 271 | 277 | [ |
| 272 | 278 | 'amount' => $amount, |