Support PayPal Express Checkout #27 (#46)

This commit is contained in:
longnguyen
2018-04-24 22:36:04 +07:00
committed by Dat Hoang
parent 9c371f6d74
commit 68752ae215
3 changed files with 232 additions and 0 deletions
+36
View File
@@ -239,6 +239,42 @@ class WooViet_Admin_Page {
</td>
</tr>
<tr>
<th scope="row"><?php printf( __( 'Support VND for <a href="%s">the PayPal Express Checkout gateway</a>', 'woo-viet' ), 'https://docs.woocommerce.com/document/paypal-express-checkout/' ) ?></th>
<td>
<input name="settings[vnd_paypal_express_checkout][enabled]" type="hidden" value="no">
<input name="settings[vnd_paypal_express_checkout][enabled]" type="checkbox"
id="vnd_paypal_express_checkout" value="yes"
<?php if ( 'yes' == $settings['vnd_paypal_express_checkout']['enabled'] )
echo 'checked="checked"' ?>>
<label for="vnd_paypal_express_checkout"><?php _e( 'Enabled', 'woo-viet' ) ?></label>
<fieldset><br/>
<select name="settings[vnd_paypal_express_checkout][currency]"
id="vnd_paypal_express_checkout_currency">
<?php
foreach ( $paypal_supported_currencies as $currency ) {
if ( strtoupper( $currency ) == $settings['vnd_paypal_express_checkout']['currency'] ) {
printf( '<option selected="selected" value="%1$s">%1$s</option>', $currency );
} else {
printf( '<option value="%1$s">%1$s</option>', $currency );
}
}
?>
</select>
<label for="vnd_paypal_express_checkout_currency"><?php _e( 'Select a PayPal supported currency (like USD, EUR, etc), which is used to convert VND prices', 'woo-viet' ) ?></label>
<br/>
<br/>
<input name="settings[vnd_paypal_express_checkout][rate]" type="number" step="1" min="100"
id="vnd_paypal_express_checkout_rate" style="width: 70px;"
value="<?php echo $settings['vnd_paypal_express_checkout']['rate'] ?>"
<label for="vnd_paypal_express_checkout_rate"><?php _e( 'Insert the exchange rate of this currency to VND', 'woo-viet' ) ?></label>
</fieldset>
</td>
</tr>
</tbody>
</table>
@@ -0,0 +1,180 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* The class to add the support for VND when using the WooCommerce PayPal Express Checkout gateway
*
* @see https://docs.woocommerce.com/document/paypal-express-checkout/
*
* @author Longkt
* @since 1.4
*
*/
class WooViet_VND_PayPal_Express_Checkout {
/**
* @var int
*/
protected $ppec_exchange_rate;
/**
* @var string
*/
protected $ppec_currency;
/**
* @var string
*/
protected $ppec_description;
/**
* WooViet_VND_PayPal_Express_Checkout constructor
*
* @param int $ppec_exchange_rate
* @param string $ppec_currency
*/
public function __construct( $ppec_exchange_rate = 22770, $ppec_currency = 'USD' ) {
$this->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' ),
"<span style='color:red'> $this->ppec_currency</span>",
"<span style='color:red'> $this->ppec_currency / VND = $this->ppec_exchange_rate</span>"
);
// 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 '<p class="ppec-exchange-rate-info">' . $this->ppec_description . '</p>';
}
/**
* 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'] .= '<br />';
$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'] .= '<br />';
$value['description'] .= $this->ppec_description;
}
return $value;
}
}
+16
View File
@@ -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']
);
}
}
/**