mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Move saved card payment logic into vaulting (WIP)
This commit is contained in:
parent
46352ba16e
commit
5d8b783abb
7 changed files with 352 additions and 181 deletions
|
@ -57,4 +57,17 @@ return array(
|
|||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
},
|
||||
'vaulting.credit-card-handler' => function(ContainerInterface $container): VaultedCreditCardHandler {
|
||||
return new VaultedCreditCardHandler(
|
||||
$container->get('subscription.helper'),
|
||||
$container->get('vaulting.repository.payment-token'),
|
||||
$container->get( 'api.factory.purchase-unit' ),
|
||||
$container->get( 'api.factory.payer' ),
|
||||
$container->get( 'api.factory.shipping-preference' ),
|
||||
$container->get( 'api.endpoint.order' ),
|
||||
$container->get( 'onboarding.environment' ),
|
||||
$container->get( 'wcgateway.processor.authorized-payments' ),
|
||||
$container->get( 'wcgateway.settings' )
|
||||
);
|
||||
},
|
||||
);
|
||||
|
|
202
modules/ppcp-vaulting/src/VaultedCreditCardHandler.php
Normal file
202
modules/ppcp-vaulting/src/VaultedCreditCardHandler.php
Normal file
|
@ -0,0 +1,202 @@
|
|||
<?php
|
||||
/**
|
||||
* Handles payment through saved credit card.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\Vaulting
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\Vaulting;
|
||||
|
||||
use Psr\Container\ContainerInterface;
|
||||
use WC_Customer;
|
||||
use WC_Order;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PayerFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingPreferenceFactory;
|
||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\OrderMetaTrait;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
|
||||
|
||||
class VaultedCreditCardHandler
|
||||
{
|
||||
use OrderMetaTrait, TransactionIdHandlingTrait, PaymentsStatusHandlingTrait, FreeTrialHandlerTrait;
|
||||
|
||||
/**
|
||||
* The subscription helper.
|
||||
*
|
||||
* @var SubscriptionHelper
|
||||
*/
|
||||
protected $subscription_helper;
|
||||
|
||||
/**
|
||||
* The payment token repository.
|
||||
*
|
||||
* @var PaymentTokenRepository
|
||||
*/
|
||||
private $payment_token_repository;
|
||||
|
||||
/**
|
||||
* The purchase unit factory.
|
||||
*
|
||||
* @var PurchaseUnitFactory
|
||||
*/
|
||||
private $purchase_unit_factory;
|
||||
|
||||
/**
|
||||
* The payer factory.
|
||||
*
|
||||
* @var PayerFactory
|
||||
*/
|
||||
private $payer_factory;
|
||||
|
||||
/**
|
||||
* The shipping_preference factory.
|
||||
*
|
||||
* @var ShippingPreferenceFactory
|
||||
*/
|
||||
private $shipping_preference_factory;
|
||||
|
||||
/**
|
||||
* The order endpoint.
|
||||
*
|
||||
* @var OrderEndpoint
|
||||
*/
|
||||
private $order_endpoint;
|
||||
|
||||
/**
|
||||
* The environment.
|
||||
*
|
||||
* @var Environment
|
||||
*/
|
||||
protected $environment;
|
||||
|
||||
/**
|
||||
* The processor for authorized payments.
|
||||
*
|
||||
* @var AuthorizedPaymentsProcessor
|
||||
*/
|
||||
protected $authorized_payments_processor;
|
||||
|
||||
/**
|
||||
* The settings.
|
||||
*
|
||||
* @var ContainerInterface
|
||||
*/
|
||||
protected $config;
|
||||
|
||||
public function __construct(
|
||||
SubscriptionHelper $subscription_helper,
|
||||
PaymentTokenRepository $payment_token_repository,
|
||||
PurchaseUnitFactory $purchase_unit_factory,
|
||||
PayerFactory $payer_factory,
|
||||
ShippingPreferenceFactory $shipping_preference_factory,
|
||||
OrderEndpoint $order_endpoint,
|
||||
Environment $environment,
|
||||
AuthorizedPaymentsProcessor $authorized_payments_processor,
|
||||
ContainerInterface $config
|
||||
)
|
||||
{
|
||||
$this->subscription_helper = $subscription_helper;
|
||||
$this->payment_token_repository = $payment_token_repository;
|
||||
$this->purchase_unit_factory = $purchase_unit_factory;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->shipping_preference_factory = $shipping_preference_factory;
|
||||
$this->order_endpoint = $order_endpoint;
|
||||
$this->environment = $environment;
|
||||
$this->authorized_payments_processor = $authorized_payments_processor;
|
||||
$this->config = $config;
|
||||
}
|
||||
|
||||
public function handle_payment(
|
||||
string $saved_credit_card,
|
||||
WC_Order $wc_order
|
||||
): WC_Order {
|
||||
|
||||
$change_payment = filter_input( INPUT_POST, 'woocommerce_change_payment', FILTER_SANITIZE_STRING );
|
||||
if($change_payment) {
|
||||
if ( $this->subscription_helper->has_subscription( $wc_order->get_id() ) && $this->subscription_helper->is_subscription_change_payment() ) {
|
||||
if ( $saved_credit_card ) {
|
||||
update_post_meta( $wc_order->get_id(), 'payment_token_id', $saved_credit_card );
|
||||
|
||||
return $wc_order;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
$user_id = (int) $wc_order->get_customer_id();
|
||||
$customer = new WC_Customer( $user_id );
|
||||
$tokens = $this->payment_token_repository->all_for_user_id( (int) $customer->get_id() );
|
||||
$selected_token = null;
|
||||
foreach ( $tokens as $token ) {
|
||||
if ( $token->id() === $saved_credit_card ) {
|
||||
$selected_token = $token;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if ( ! $selected_token ) {
|
||||
throw new RuntimeException('Saved card token not found.');
|
||||
}
|
||||
|
||||
$purchase_unit = $this->purchase_unit_factory->from_wc_order( $wc_order );
|
||||
$payer = $this->payer_factory->from_customer( $customer );
|
||||
$shipping_preference = $this->shipping_preference_factory->from_state(
|
||||
$purchase_unit,
|
||||
''
|
||||
);
|
||||
|
||||
try {
|
||||
$order = $this->order_endpoint->create(
|
||||
array( $purchase_unit ),
|
||||
$shipping_preference,
|
||||
$payer,
|
||||
$selected_token
|
||||
);
|
||||
|
||||
$this->add_paypal_meta( $wc_order, $order, $this->environment );
|
||||
|
||||
if ( ! $order->status()->is( OrderStatus::COMPLETED ) ) {
|
||||
throw new RuntimeException("Unexpected status for order {$order->id()} using a saved card: {$order->status()->name()}.");
|
||||
}
|
||||
|
||||
if ( ! in_array(
|
||||
$order->intent(),
|
||||
array( 'CAPTURE', 'AUTHORIZE' ),
|
||||
true
|
||||
) ) {
|
||||
throw new RuntimeException("Could neither capture nor authorize order {$order->id()} using a saved card. Status: {$order->status()->name()}. Intent: {$order->intent()}.");
|
||||
}
|
||||
|
||||
if ( $order->intent() === 'AUTHORIZE' ) {
|
||||
$order = $this->order_endpoint->authorize( $order );
|
||||
$wc_order->update_meta_data( AuthorizedPaymentsProcessor::CAPTURED_META_KEY, 'false' );
|
||||
}
|
||||
|
||||
$transaction_id = $this->get_paypal_order_transaction_id( $order );
|
||||
if ( $transaction_id ) {
|
||||
$this->update_transaction_id( $transaction_id, $wc_order );
|
||||
}
|
||||
|
||||
$this->handle_new_order_status( $order, $wc_order );
|
||||
|
||||
if ( $this->is_free_trial_order( $wc_order ) ) {
|
||||
$this->authorized_payments_processor->void_authorizations( $order );
|
||||
$wc_order->payment_complete();
|
||||
} elseif ( $this->config->has( 'intent' ) && strtoupper( (string) $this->config->get( 'intent' ) ) === 'CAPTURE' ) {
|
||||
$this->authorized_payments_processor->capture_authorized_payment( $wc_order );
|
||||
}
|
||||
|
||||
return $wc_order;
|
||||
} catch ( RuntimeException $error ) {
|
||||
throw new RuntimeException($error->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue