X

Nifty Paste

Frontpage Show raw
Expiry: Never
<?php
/**
 * Plugin Name: [Forminator Pro] - Paypal fix invalid payment amount error.
 * Description: [Forminator Pro] - Paypal fix invalid payment amount error.
 * Jira: SLS-2188
 * Author: Thobk @ WPMUDEV
 * Author URI: https://premium.wpmudev.org
 * License: GPLv2 or later
 */
if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; }

add_action( 'after_setup_theme', 'wpmudev_fix_paypal_invalid_payment_amount_issue', 100 );

function wpmudev_fix_paypal_invalid_payment_amount_issue() {
	if ( class_exists( 'Forminator' )  ) {
		add_filter( 'forminator_field_paypal_payment_amount', function( $payment_amount, $field, $custom_form, $submitted_data, $pseudo_submitted_data ){
			if( $payment_amount === 0.0 ){
				$amount_type     = $field['amount_type'];
				if( $amount_type === 'variable' ){
					$amount_variable = $field['variable'];


					$form_field = $custom_form->get_field( $amount_variable, false );
					if ( $form_field ) {
						$form_field        = $form_field->to_formatted_array();
						$fields_collection = forminator_fields_to_array();
						if ( isset( $form_field['type'] ) ) {
							if ( 'calculation' === $form_field['type'] ) {

								// Calculation field get the amount from pseudo_submit_data
								if ( isset( $pseudo_submitted_data[ $amount_variable ] ) ) {
									$payment_amount = $pseudo_submitted_data[ $amount_variable ];
								}
							} elseif ( 'currency' === $form_field['type'] ) {
								// Currency field get the amount from submitted_data
								$field_id = $form_field['element_id'];
								if ( isset( $submitted_data[ $field_id ] ) ) {
									$payment_amount = $submitted_data[ $field_id ];
								}
							} else {
								if ( isset( $fields_collection[ $form_field['type'] ] ) ) {
									/** @var Forminator_Field $field_object */
									$field_object = $fields_collection[ $form_field['type'] ];

									$field_id             = $form_field['element_id'];
									$submitted_field_data = isset( $submitted_data[ $field_id ] ) ? $submitted_data[ $field_id ] : null;
									$payment_amount       = $field_object->get_calculable_value( $submitted_field_data, $form_field );
								}
							}
						}
					}
				}
			}

			if( $payment_amount ){
				$payment_amount = str_replace(',', '', $payment_amount );
			}
			return $payment_amount;
		}, 999, 5 );


		add_filter( 'http_response', function( $response, $params, $url ){
			if( strpos( $url, 'paypal.com/v2/checkout/orders/') && 200 === wp_remote_retrieve_response_code( $response ) ){
				$body = json_decode( wp_remote_retrieve_body( $response ) );
				if( isset( $body->purchase_units[0]->amount->value ) && $body->purchase_units[0]->amount->value > 999 ){
					$forminator_paypal_field_path = WP_PLUGIN_DIR .'/forminator/library/fields/paypal.php';
					if( @file_exists( $forminator_paypal_field_path ) ){
						$paypal_field_content = @file_get_contents( $forminator_paypal_field_path );

						if( strpos( $paypal_field_content, 'number_format( $charge_amount, 2 )') ){
							$body->purchase_units[0]->amount->value = number_format( $body->purchase_units[0]->amount->value, 2 );
							$response['body'] = json_encode( $body );
						}
					}
				}
			}
			return $response;
		}, 10, 3 );
	}
}