Fix issue #65 (#67)

* An idea to fix issue 65

Needs to refine the code before publishing it

* Polish the fix for issue #65

* Needs to remove the filter valid-paypal-standard-ipn-request also
This commit is contained in:
Dat Hoang
2019-10-14 18:01:35 +07:00
parent ed92de526a
commit 195f246f22
2 changed files with 45 additions and 76 deletions
+25 -76
View File
@@ -25,8 +25,6 @@ class WooViet_VND_PayPal_Standard {
*/ */
protected $paypal_currency = 'USD'; protected $paypal_currency = 'USD';
//@todo - declare two vars
/** /**
* WooViet_VND_PayPal_Standard constructor. * WooViet_VND_PayPal_Standard constructor.
* *
@@ -46,12 +44,11 @@ class WooViet_VND_PayPal_Standard {
// Add the exchange rate info for this gateway in the checkout page before proceeding in the PayPal pages // Add the exchange rate info for this gateway in the checkout page before proceeding in the PayPal pages
add_filter( 'option_woocommerce_paypal_settings', array( $this, 'add_exchange_rate_info' ), 11 ); add_filter( 'option_woocommerce_paypal_settings', array( $this, 'add_exchange_rate_info' ), 11 );
// Match currency and amount between Paypal and WC Order
add_action( 'valid-paypal-standard-ipn-request', array( $this, 'match_order_currency_and_amount' ), 5 );
// Restore currency and amount for WC Order // Remove checks on currency and gross (total) values
add_action( 'valid-paypal-standard-ipn-request', array( $this, 'restore_order_currency_and_amount' ), 15 ); add_action( 'woocommerce_api_wc_gateway_paypal', array( $this, 'remove_currency_and_total_checks' ), 5 );
} }
/** /**
@@ -116,79 +113,31 @@ class WooViet_VND_PayPal_Standard {
return $value; return $value;
} }
/*
* Match currency and amount from Paypal IPN with the order
*
* Topic https://wordpress.org/support/topic/loi-order-bi-on-hold/
*
* @author htdat
* @since 1.4.3
*/
public function match_order_currency_and_amount($posted) {
$order = ! empty( $posted['custom'] ) ? $this->get_paypal_order( $posted['custom'] ) : false; /**
* Remove currency and total (gross) amount check in class WC_Gateway_Paypal_IPN_Handler
*
* @since 1.4.5
* @author htdat
*/
public function remove_currency_and_total_checks() {
if ( $order ) { // Remove these filters https://github.com/woocommerce/woocommerce/blob/3.7.1/includes/gateways/paypal/includes/class-wc-gateway-paypal-ipn-handler.php#L34-L35
$this->original_order_currency = $order->get_currency(); remove_all_filters( 'woocommerce_api_wc_gateway_paypal', 10 );
$this->original_order_total = $order->get_total(); remove_all_filters( 'valid-paypal-standard-ipn-request', 10 );
$order->set_currency( $posted['mc_currency'] ); // Get values for PayPal Standard settings
$order->set_total( $posted['mc_gross'] ); // Ref: https://github.com/woocommerce/woocommerce/blob/3.7.1/includes/gateways/paypal/class-wc-gateway-paypal.php#L58-L61
$paypal_options = get_option( 'woocommerce_paypal_settings' );
$testmode = 'yes' === $paypal_options['testmode'];
$receiver_email = is_null( $paypal_options['receiver_email'] ) ? $paypal_options['email'] : $paypal_options['receiver_email'];
$order->save(); // Replace it by a child class of WC_Gateway_Paypal_IPN_Handler,
} // which overrides/removes validate_currency() and validate_amount()
require_once dirname(__FILE__) . '/paypal-standard/class-woo-viet-wc-gateway-paypal-ipn-handler.php.php';
$handler = new Woo_Viet_WC_Gateway_Paypal_IPN_Handler( $testmode, $receiver_email ); //@todo needs to handle the correct variables
$handler->check_response();
} }
/*
* Restore currency and amount of the order after the 'match_order_currency_and_amount' action
*
* @author htdat
* @since 1.4.3
*/
public function restore_order_currency_and_amount($posted) {
$order = ! empty( $posted['custom'] ) ? $this->get_paypal_order( $posted['custom'] ) : false;
if ( $order ) {
$order->set_currency( $this->original_order_currency );
$order->set_total( $this->original_order_total );
$order->save();
}
}
/**
* @see Grab this code from - can not call it directly https://github.com/woocommerce/woocommerce/blob/f5c2f89af6a9421af8edc2a4aa20d372e5be40f8/includes/gateways/paypal/includes/class-wc-gateway-paypal-response.php#L30
*
* @since 1.4.3
* @author htdat
*/
protected function get_paypal_order( $raw_custom ) {
// We have the data in the correct format, so get the order.
$custom = json_decode( $raw_custom );
if ( $custom && is_object( $custom ) ) {
$order_id = $custom->order_id;
$order_key = $custom->order_key;
} else {
// Nothing was found.
WC_Gateway_Paypal::log( 'Order ID and key were not found in "custom".', 'error' );
return false;
}
$order = wc_get_order( $order_id );
if ( ! $order ) {
// We have an invalid $order_id, probably because invoice_prefix has changed.
$order_id = wc_get_order_id_by_order_key( $order_key );
$order = wc_get_order( $order_id );
}
if ( ! $order || $order->get_order_key() !== $order_key ) {
WC_Gateway_Paypal::log( 'Order Keys do not match.', 'error' );
return false;
}
return $order;
}
} }
@@ -0,0 +1,20 @@
<?php
/**
* Override/remove two validate functions in the parent class
*
* @author htdat
* @since 1.4.5
*
*/
if ( class_exists( 'WC_Gateway_Paypal_IPN_Handler' ) ) {
class Woo_Viet_WC_Gateway_Paypal_IPN_Handler extends WC_Gateway_Paypal_IPN_Handler {
protected function validate_currency( $order, $currency ) {}
protected function validate_amount( $order, $amount ) {}
}
}