relocate dcc fields to gateway description, use \WC_Payment_Gateway_CC, reutilize theme styles for dcc fields as much as possible

This commit is contained in:
David Remer 2020-09-14 14:17:39 +03:00
parent e4b6a412ea
commit 084eb2a908
8 changed files with 132 additions and 151 deletions

View file

@ -19,7 +19,9 @@ use Psr\Container\ContainerInterface;
/**
* Class CreditCardGateway
*/
class CreditCardGateway extends PayPalGateway {
class CreditCardGateway extends \WC_Payment_Gateway_CC {
use ProcessPaymentTrait;
const ID = 'ppcp-credit-card-gateway';
@ -76,6 +78,7 @@ class CreditCardGateway extends PayPalGateway {
'subscription_payment_method_change_customer',
'subscription_payment_method_change_admin',
'multiple_subscriptions',
'credit_card_form_cvc_on_saved_method',
);
}

View file

@ -23,6 +23,8 @@ use Psr\Container\ContainerInterface;
*/
class PayPalGateway extends \WC_Payment_Gateway {
use ProcessPaymentTrait;
const ID = 'ppcp-gateway';
const CAPTURED_META_KEY = '_ppcp_paypal_captured';
const INTENT_META_KEY = '_ppcp_paypal_intent';
@ -168,71 +170,6 @@ class PayPalGateway extends \WC_Payment_Gateway {
}
}
/**
* Process a payment for an WooCommerce order.
*
* @param int $order_id The WooCommerce order id.
*
* @return array|null
*/
public function process_payment( $order_id ) {
global $woocommerce;
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, \WC_Order::class ) ) {
return null;
}
/**
* If the WC_Order is payed through the approved webhook.
*/
//phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wc_order->has_status( 'processing' ) ) {
$this->session_handler->destroy_session_data();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
//phpcs:enable WordPress.Security.NonceVerification.Recommended
try {
if ( $this->order_processor->process( $wc_order, $woocommerce ) ) {
$this->session_handler->destroy_session_data();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
} catch ( PayPalApiException $error ) {
if ( $error->has_detail( 'INSTRUMENT_DECLINED' ) ) {
$this->session_handler->increment_insufficient_funding_tries();
$host = $this->config->has( 'sandbox_on' ) && $this->config->get( 'sandbox_on' ) ?
'https://www.sandbox.paypal.com/' : 'https://www.paypal.com/';
$url = $host . 'checkoutnow?token=' . $this->session_handler->order()->id();
if ( $this->session_handler->insufficient_funding_tries() >= 3 ) {
$this->session_handler->destroy_session_data();
wc_add_notice(
__( 'Please use a different payment method.', 'paypal-payments-for-woocommerce' ),
'error'
);
return null;
}
return array(
'result' => 'success',
'redirect' => $url,
);
}
$this->session_handler->destroy_session_data();
}
wc_add_notice(
$this->order_processor->last_error(),
'error'
);
return null;
}
/**
* Captures an authorized payment for an WooCommerce order.
*

View file

@ -0,0 +1,82 @@
<?php
/**
* The process_payment functionality for the both gateways.
*
* @package WooCommerce\PayPalCommerce\WcGateway\Gateway
*/
declare( strict_types=1 );
namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
/**
* Trait ProcessPaymentTrait
*/
trait ProcessPaymentTrait {
/**
* Process a payment for an WooCommerce order.
*
* @param int $order_id The WooCommerce order id.
*
* @return array|null
*/
public function process_payment( $order_id ) {
global $woocommerce;
$wc_order = wc_get_order( $order_id );
if ( ! is_a( $wc_order, \WC_Order::class ) ) {
return null;
}
/**
* If the WC_Order is payed through the approved webhook.
*/
//phpcs:disable WordPress.Security.NonceVerification.Recommended
if ( isset( $_REQUEST['ppcp-resume-order'] ) && $wc_order->has_status( 'processing' ) ) {
$this->session_handler->destroy_session_data();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
//phpcs:enable WordPress.Security.NonceVerification.Recommended
try {
if ( $this->order_processor->process( $wc_order, $woocommerce ) ) {
$this->session_handler->destroy_session_data();
return array(
'result' => 'success',
'redirect' => $this->get_return_url( $wc_order ),
);
}
} catch ( PayPalApiException $error ) {
if ( $error->has_detail( 'INSTRUMENT_DECLINED' ) ) {
$this->session_handler->increment_insufficient_funding_tries();
$host = $this->config->has( 'sandbox_on' ) && $this->config->get( 'sandbox_on' ) ?
'https://www.sandbox.paypal.com/' : 'https://www.paypal.com/';
$url = $host . 'checkoutnow?token=' . $this->session_handler->order()->id();
if ( $this->session_handler->insufficient_funding_tries() >= 3 ) {
$this->session_handler->destroy_session_data();
wc_add_notice(
__( 'Please use a different payment method.', 'paypal-payments-for-woocommerce' ),
'error'
);
return null;
}
return array(
'result' => 'success',
'redirect' => $url,
);
}
$this->session_handler->destroy_session_data();
}
wc_add_notice(
$this->order_processor->last_error(),
'error'
);
return null;
}
}