Change subscription payment method (WIP)

This commit is contained in:
Emili Castells Guasch 2024-01-03 15:44:24 +01:00
parent 9ad2272907
commit f75846952d
4 changed files with 110 additions and 36 deletions

View file

@ -12,6 +12,7 @@ namespace WooCommerce\PayPalCommerce\WcGateway\Gateway;
use Exception;
use Psr\Log\LoggerInterface;
use WC_Order;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
@ -377,11 +378,11 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
}
/**
* If customer has chosen a saved credit card payment.
* If customer has chosen a saved credit card payment from checkout page.
*/
// phpcs:ignore WordPress.Security.NonceVerification.Missing
$saved_credit_card = wc_clean( wp_unslash( $_POST['saved_credit_card'] ?? '' ) );
if ( $saved_credit_card ) {
if ( $saved_credit_card && is_checkout() ) {
try {
$wc_order = $this->vaulted_credit_card_handler->handle_payment(
$saved_credit_card,
@ -395,6 +396,33 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
}
}
/**
* If customer is changing subscription payment.
*/
if (
// phpcs:ignore WordPress.Security.NonceVerification.Missing
isset( $_POST['woocommerce_change_payment'] )
&& $this->subscription_helper->has_subscription( $wc_order->get_id() )
&& $this->subscription_helper->is_subscription_change_payment()
&& $saved_credit_card
) {
$payment_token = WC_Payment_Tokens::get($saved_credit_card);
if($payment_token) {
$wc_order->add_payment_token($payment_token);
$wc_order->save();
return $this->handle_payment_success( $wc_order );
}
wc_add_notice( __( 'Could not change payment.', 'woocommerce-paypal-payments' ), 'error' );
return array(
'result' => 'failure',
'redirect' => wc_get_checkout_url(),
'errorMessage' => __( 'Could not change payment.', 'woocommerce-paypal-payments' ),
);
}
/**
* If the WC_Order is paid through the approved webhook.
*/