diff --git a/inc/class-wooviet-admin-page.php b/inc/class-wooviet-admin-page.php index 4f10bc4..08c33e4 100644 --- a/inc/class-wooviet-admin-page.php +++ b/inc/class-wooviet-admin-page.php @@ -239,6 +239,42 @@ class WooViet_Admin_Page { + + the PayPal Express Checkout gateway', 'woo-viet' ), 'https://docs.woocommerce.com/document/paypal-express-checkout/' ) ?> + + + > + + +

+ + +
+
+ + +
+ + diff --git a/inc/class-wooviet-vnd-paypal-express-checkout.php b/inc/class-wooviet-vnd-paypal-express-checkout.php new file mode 100644 index 0000000..435afb0 --- /dev/null +++ b/inc/class-wooviet-vnd-paypal-express-checkout.php @@ -0,0 +1,180 @@ +ppec_exchange_rate = (int) $ppec_exchange_rate; + $this->ppec_currency = $ppec_currency; + + $this->ppec_description = sprintf( __( 'The prices will be converted to %1$s in the PayPal Express Checkout pages with the exchange rate %2$s.', 'woo-viet' ), + " $this->ppec_currency", + " $this->ppec_currency / VND = $this->ppec_exchange_rate" + ); + + // Match response currency of PayPal with local order + add_action( 'woocommerce_paypal_express_checkout_valid_ipn_request', array( $this, 'ppec_match_currency_order' ) ); + + // Add exchange rate before send request to PayPal + add_filter( 'woocommerce_paypal_express_checkout_request_body', array( $this, 'ppec_convert_prices' ) ); + + // Load the method to add the exchange rate info for this gateway + $this->ppec_exchange_rate_info(); + + } + + /** + * Match response currency from PayPal with the order + * + * @param $posted_data + */ + public function ppec_match_currency_order( $posted_data ) { + + if( $posted_data['mc_currency'] ) { + $posted_data['mc_currency'] = $order->get_currency(); + } + + } + + /** + * @param $params + * @return mixed + */ + public function ppec_convert_prices( $params ) { + + if( isset( $params['PAYMENTREQUEST_0_CURRENCYCODE'] ) ) { + + $params['PAYMENTREQUEST_0_CURRENCYCODE'] = $this->ppec_currency; + + if( isset( $params['PAYMENTREQUEST_0_AMT'] ) ) { + $params['PAYMENTREQUEST_0_AMT'] = round( $params['PAYMENTREQUEST_0_AMT'] / $this->ppec_exchange_rate, 2 ); + } + + if( isset( $params['PAYMENTREQUEST_0_ITEMAMT'] ) ) { + $params['PAYMENTREQUEST_0_ITEMAMT'] = round( $params['PAYMENTREQUEST_0_ITEMAMT'] / $this->ppec_exchange_rate, 2 ); + } + + if( isset( $params['PAYMENTREQUEST_0_SHIPPINGAMT'] ) ) { + $params['PAYMENTREQUEST_0_SHIPPINGAMT'] = round( $params['PAYMENTREQUEST_0_SHIPPINGAMT'] / $this->ppec_exchange_rate, 2 ); + } + + if( isset( $params['PAYMENTREQUEST_0_TAXAMT'] ) ) { + $params['PAYMENTREQUEST_0_TAXAMT'] = round( $params['PAYMENTREQUEST_0_TAXAMT'] / $this->ppec_exchange_rate, 2 ); + } + + if( isset( $params['PAYMENTREQUEST_0_SHIPDISCAMT'] ) ) { + $params['PAYMENTREQUEST_0_SHIPDISCAMT'] = round( $params['PAYMENTREQUEST_0_SHIPDISCAMT'] / $this->ppec_exchange_rate, 2 ); + } + + $count = 0; + + while( isset( $params['L_PAYMENTREQUEST_0_AMT' . $count] ) ) { + $params['L_PAYMENTREQUEST_0_AMT' . $count] = round( $params['L_PAYMENTREQUEST_0_AMT' . $count] / $this->ppec_exchange_rate, 2 ); + $count++; + } + } + + return $params; + + } + + /** + * Add the exchange rate info in the suitable locations before proceeding in the PayPal pages + */ + public function ppec_exchange_rate_info() { + + // Check if "Checkout on cart page" is enabled. + if( 'yes' === wc_gateway_ppec()->settings->cart_checkout_enabled ) { + add_action( 'woocommerce_proceed_to_checkout', array( $this, 'add_ppec_button_exchange_rate_info' ), 30 ); + } + + // Check if "Checkout on Single Product" is enabled. + if( 'yes' === wc_gateway_ppec()->settings->checkout_on_single_product_enabled ) { + add_action( 'woocommerce_after_add_to_cart_form', array( $this, 'add_ppec_button_exchange_rate_info' ), 30 ); + } + + // Check if "Enable PayPal Credit" is enabled. + if( 'yes' === wc_gateway_ppec()->settings->credit_enabled ) { + add_filter( 'woocommerce_paypal_express_checkout_settings', array( $this, 'add_paypal_credit_exchange_rate_info' ), 11 ); + } + + // Add the exchange rate info for PPEC in Checkout page + add_filter( 'option_woocommerce_ppec_paypal_settings', array( $this, 'add_ppec_checkout_exchange_rate_info' ), 11 ); + + } + + /** + * Display the exchange rate info of PPEC in Cart and Single Product page + */ + public function add_ppec_button_exchange_rate_info() { + + echo '

' . $this->ppec_description . '

'; + + } + + /** + * Display the exchange rate info of PP Credit in Checkout page + * + * @param $value + * @return mixed + */ + public function add_paypal_credit_exchange_rate_info( $value ) { + + if ( ! is_admin() ) { + $value['description']['default'] .= '
'; + $value['description']['default'] .= $this->ppec_description; + } + + return $value; + } + + /** + * Display the exchange rate info of PPEC in Checkout page + * + * @param $value + * @return mixed + */ + public function add_ppec_checkout_exchange_rate_info( $value ) { + + if ( ! is_admin() ) { + $value['description'] .= '
'; + $value['description'] .= $this->ppec_description; + } + return $value; + + } + +} \ No newline at end of file diff --git a/woo-viet.php b/woo-viet.php index 50fbb10..8c895b1 100644 --- a/woo-viet.php +++ b/woo-viet.php @@ -64,6 +64,12 @@ class WooViet { array( 'enabled' => 'yes', ), + 'vnd_paypal_express_checkout' => + array( + 'enabled' => 'yes', + 'currency' => 'USD', + 'rate' => '22770', + ), ); /** * The properties to manage all classes under the "inc/" folder @@ -76,6 +82,7 @@ class WooViet { protected $Currency; protected $VND_PayPal_Standard; protected $Admin_Page; + protected $VND_PayPal_Express_Checkout; /** * Setup class. @@ -199,6 +206,15 @@ class WooViet { ); } + // Check if "Support VND for the PayPal Express Checkout gateway" is enabled + if ( 'yes' == $settings['vnd_paypal_express_checkout']['enabled'] ) { + include( WOO_VIET_DIR . 'inc/class-wooviet-vnd-paypal-express-checkout.php' ); + $this->VND_PayPal_Express_Checkout = new WooViet_VND_PayPal_Express_Checkout( + $settings['vnd_paypal_express_checkout']['rate'], + $settings['vnd_paypal_express_checkout']['currency'] + ); + } + } /**