Handle card smart button for free trial (1$ auth + void)

Disabling this funding source also disables DCC
This commit is contained in:
Alex P 2022-04-25 15:24:37 +03:00
parent a5191b04ff
commit 4a4f131325
13 changed files with 74 additions and 5 deletions

View file

@ -31,8 +31,17 @@ const bootstrap = () => {
const onSmartButtonClick = (data, actions) => {
window.ppcpFundingSource = data.fundingSource;
const form = document.querySelector('form.woocommerce-checkout');
if (form) {
jQuery('#ppcp-funding-source-form-input').remove();
form.insertAdjacentHTML(
'beforeend',
`<input type="hidden" name="ppcp-funding-source" value="${data.fundingSource}" id="ppcp-funding-source-form-input">`
)
}
const isFreeTrial = PayPalCommerceGateway.is_free_trial_cart;
if (isFreeTrial) {
if (isFreeTrial && data.fundingSource !== 'card') {
freeTrialHandler.handle();
return actions.reject();
}

View file

@ -20,6 +20,7 @@ class CartActionHandler {
nonce: this.config.ajax.create_order.nonce,
purchase_units: [],
payment_method: PaymentMethods.PAYPAL,
funding_source: window.ppcpFundingSource,
bn_code:bnCode,
payer,
context:this.config.context

View file

@ -33,6 +33,7 @@ class CheckoutActionHandler {
context:this.config.context,
order_id:this.config.order_id,
payment_method: getCurrentPaymentMethod(),
funding_source: window.ppcpFundingSource,
form:formValues,
createaccount: createaccount
})

View file

@ -86,6 +86,7 @@ class SingleProductActionHandler {
payer,
bn_code:bnCode,
payment_method: PaymentMethods.PAYPAL,
funding_source: window.ppcpFundingSource,
context:this.config.context
})
}).then(function (res) {

View file

@ -28,6 +28,7 @@ use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
use WooCommerce\PayPalCommerce\WcGateway\Exception\NotFoundException;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
/**
@ -182,6 +183,7 @@ class CreateOrderEndpoint implements EndpointInterface {
$data = $this->request_data->read_request( $this->nonce() );
$this->parsed_request_data = $data;
$payment_method = $data['payment_method'] ?? '';
$funding_source = $data['funding_source'] ?? '';
$wc_order = null;
if ( 'pay-now' === $data['context'] ) {
$wc_order = wc_get_order( (int) $data['order_id'] );
@ -200,7 +202,12 @@ class CreateOrderEndpoint implements EndpointInterface {
$this->purchase_units = $this->cart_repository->all();
// The cart does not have any info about payment method, so we must handle free trial here.
if ( CreditCardGateway::ID === $payment_method && $this->is_free_trial_cart() ) {
if ( (
CreditCardGateway::ID === $payment_method
|| ( PayPalGateway::ID === $payment_method && 'card' === $funding_source )
)
&& $this->is_free_trial_cart()
) {
$this->purchase_units[0]->set_amount(
new Amount(
new Money( 1.0, $this->purchase_units[0]->amount()->currency_code() ),