mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add save card payment for changing subscription payment method (WIP)
This commit is contained in:
parent
f081fc2392
commit
0262c28580
7 changed files with 149 additions and 19 deletions
|
@ -128,6 +128,33 @@ document.addEventListener(
|
||||||
|
|
||||||
const result = await response.json();
|
const result = await response.json();
|
||||||
if(result.success === true) {
|
if(result.success === true) {
|
||||||
|
if(ppcp_add_payment_method.is_subscription_change_payment_page) {
|
||||||
|
const subscriptionId = ppcp_add_payment_method.subscription_id_to_change_payment;
|
||||||
|
if(subscriptionId && result.data) {
|
||||||
|
const req = await fetch(ppcp_add_payment_method.ajax.subscription_change_payment_method.endpoint, {
|
||||||
|
method: "POST",
|
||||||
|
credentials: 'same-origin',
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'application/json',
|
||||||
|
},
|
||||||
|
body: JSON.stringify({
|
||||||
|
nonce: ppcp_add_payment_method.ajax.subscription_change_payment_method.nonce,
|
||||||
|
subscription_id: subscriptionId,
|
||||||
|
payment_method: getCurrentPaymentMethod(),
|
||||||
|
wc_payment_token_id: result.data
|
||||||
|
})
|
||||||
|
});
|
||||||
|
|
||||||
|
const res = await req.json();
|
||||||
|
if (res.success === true) {
|
||||||
|
window.location.href = `${ppcp_add_payment_method.view_subscriptions_page}/${subscriptionId}`;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
window.location.href = ppcp_add_payment_method.payment_methods_page;
|
window.location.href = ppcp_add_payment_method.payment_methods_page;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -80,7 +80,8 @@ class CreatePaymentToken implements EndpointInterface {
|
||||||
*/
|
*/
|
||||||
public function handle_request(): bool {
|
public function handle_request(): bool {
|
||||||
try {
|
try {
|
||||||
$data = $this->request_data->read_request( $this->nonce() );
|
$data = $this->request_data->read_request( $this->nonce() );
|
||||||
|
$wc_token_id = 0;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Suppress ArgumentTypeCoercion
|
* Suppress ArgumentTypeCoercion
|
||||||
|
@ -107,7 +108,7 @@ class CreatePaymentToken implements EndpointInterface {
|
||||||
$email = $result->payment_source->paypal->email_address;
|
$email = $result->payment_source->paypal->email_address;
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->wc_payment_tokens->create_payment_token_paypal(
|
$wc_token_id = $this->wc_payment_tokens->create_payment_token_paypal(
|
||||||
$current_user_id,
|
$current_user_id,
|
||||||
$result->id,
|
$result->id,
|
||||||
$email
|
$email
|
||||||
|
@ -115,11 +116,11 @@ class CreatePaymentToken implements EndpointInterface {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( isset( $result->payment_source->card ) ) {
|
if ( isset( $result->payment_source->card ) ) {
|
||||||
$this->wc_payment_tokens->create_payment_token_card( $current_user_id, $result );
|
$wc_token_id = $this->wc_payment_tokens->create_payment_token_card( $current_user_id, $result );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_send_json_success( $result );
|
wp_send_json_success( $wc_token_id );
|
||||||
return true;
|
return true;
|
||||||
} catch ( Exception $exception ) {
|
} catch ( Exception $exception ) {
|
||||||
wp_send_json_error();
|
wp_send_json_error();
|
||||||
|
|
|
@ -28,6 +28,7 @@ use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint\SubscriptionChangePaymentMethod;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class SavePaymentMethodsModule
|
* Class SavePaymentMethodsModule
|
||||||
|
@ -211,13 +212,16 @@ class SavePaymentMethodsModule implements ModuleInterface {
|
||||||
'ppcp-add-payment-method',
|
'ppcp-add-payment-method',
|
||||||
'ppcp_add_payment_method',
|
'ppcp_add_payment_method',
|
||||||
array(
|
array(
|
||||||
'client_id' => $c->get( 'button.client_id' ),
|
'client_id' => $c->get( 'button.client_id' ),
|
||||||
'merchant_id' => $c->get( 'api.merchant_id' ),
|
'merchant_id' => $c->get( 'api.merchant_id' ),
|
||||||
'id_token' => $id_token,
|
'id_token' => $id_token,
|
||||||
'payment_methods_page' => wc_get_account_endpoint_url( 'payment-methods' ),
|
'payment_methods_page' => wc_get_account_endpoint_url( 'payment-methods' ),
|
||||||
'error_message' => __( 'Could not save payment method.', 'woocommerce-paypal-payments' ),
|
'view_subscriptions_page' => wc_get_account_endpoint_url( 'view-subscription' ),
|
||||||
'verification_method' => $verification_method,
|
'is_subscription_change_payment_page' => $this->is_subscription_change_payment_method_page(),
|
||||||
'ajax' => array(
|
'subscription_id_to_change_payment' => $this->is_subscription_change_payment_method_page() ? (int) $_GET['change_payment_method'] : 0,
|
||||||
|
'error_message' => __( 'Could not save payment method.', 'woocommerce-paypal-payments' ),
|
||||||
|
'verification_method' => $verification_method,
|
||||||
|
'ajax' => array(
|
||||||
'create_setup_token' => array(
|
'create_setup_token' => array(
|
||||||
'endpoint' => \WC_AJAX::get_endpoint( CreateSetupToken::ENDPOINT ),
|
'endpoint' => \WC_AJAX::get_endpoint( CreateSetupToken::ENDPOINT ),
|
||||||
'nonce' => wp_create_nonce( CreateSetupToken::nonce() ),
|
'nonce' => wp_create_nonce( CreateSetupToken::nonce() ),
|
||||||
|
@ -226,6 +230,10 @@ class SavePaymentMethodsModule implements ModuleInterface {
|
||||||
'endpoint' => \WC_AJAX::get_endpoint( CreatePaymentToken::ENDPOINT ),
|
'endpoint' => \WC_AJAX::get_endpoint( CreatePaymentToken::ENDPOINT ),
|
||||||
'nonce' => wp_create_nonce( CreatePaymentToken::nonce() ),
|
'nonce' => wp_create_nonce( CreatePaymentToken::nonce() ),
|
||||||
),
|
),
|
||||||
|
'subscription_change_payment_method' => array(
|
||||||
|
'endpoint' => \WC_AJAX::get_endpoint( SubscriptionChangePaymentMethod::ENDPOINT ),
|
||||||
|
'nonce' => wp_create_nonce( SubscriptionChangePaymentMethod::nonce() ),
|
||||||
|
),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
|
@ -70,17 +70,17 @@ class WooCommercePaymentTokens {
|
||||||
* @param string $token The PayPal payment token.
|
* @param string $token The PayPal payment token.
|
||||||
* @param string $email The PayPal customer email.
|
* @param string $email The PayPal customer email.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function create_payment_token_paypal(
|
public function create_payment_token_paypal(
|
||||||
int $customer_id,
|
int $customer_id,
|
||||||
string $token,
|
string $token,
|
||||||
string $email
|
string $email
|
||||||
): void {
|
): int {
|
||||||
|
|
||||||
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, PayPalGateway::ID );
|
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, PayPalGateway::ID );
|
||||||
if ( $this->payment_token_helper->token_exist( $wc_tokens, $token ) ) {
|
if ( $this->payment_token_helper->token_exist( $wc_tokens, $token ) ) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
|
$payment_token_paypal = $this->payment_token_factory->create( 'paypal' );
|
||||||
|
@ -101,6 +101,8 @@ class WooCommercePaymentTokens {
|
||||||
"Could not create WC payment token PayPal for customer {$customer_id}. " . $exception->getMessage()
|
"Could not create WC payment token PayPal for customer {$customer_id}. " . $exception->getMessage()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return $payment_token_paypal->get_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -109,12 +111,12 @@ class WooCommercePaymentTokens {
|
||||||
* @param int $customer_id The WC customer ID.
|
* @param int $customer_id The WC customer ID.
|
||||||
* @param stdClass $payment_token The Credit Card payment token.
|
* @param stdClass $payment_token The Credit Card payment token.
|
||||||
*
|
*
|
||||||
* @return void
|
* @return int
|
||||||
*/
|
*/
|
||||||
public function create_payment_token_card( int $customer_id, stdClass $payment_token ): void {
|
public function create_payment_token_card( int $customer_id, stdClass $payment_token ): int {
|
||||||
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, CreditCardGateway::ID );
|
$wc_tokens = WC_Payment_Tokens::get_customer_tokens( $customer_id, CreditCardGateway::ID );
|
||||||
if ( $this->payment_token_helper->token_exist( $wc_tokens, $payment_token->id ) ) {
|
if ( $this->payment_token_helper->token_exist( $wc_tokens, $payment_token->id ) ) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$token = new WC_Payment_Token_CC();
|
$token = new WC_Payment_Token_CC();
|
||||||
|
@ -139,6 +141,8 @@ class WooCommercePaymentTokens {
|
||||||
"Could not create WC payment token card for customer {$customer_id}. " . $exception->getMessage()
|
"Could not create WC payment token card for customer {$customer_id}. " . $exception->getMessage()
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
$token->save();
|
$token->save();
|
||||||
|
return $token->get_id();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,10 +9,9 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace WooCommerce\PayPalCommerce\WcSubscriptions;
|
namespace WooCommerce\PayPalCommerce\WcSubscriptions;
|
||||||
|
|
||||||
use WooCommerce\PayPalCommerce\PayPalSubscriptions\DeactivatePlanEndpoint;
|
|
||||||
use WooCommerce\PayPalCommerce\PayPalSubscriptions\SubscriptionsApiHandler;
|
|
||||||
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
use WooCommerce\PayPalCommerce\Vaulting\PaymentTokenRepository;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||||
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint\SubscriptionChangePaymentMethod;
|
||||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||||
|
|
||||||
return array(
|
return array(
|
||||||
|
@ -45,4 +44,9 @@ return array(
|
||||||
$endpoint = $container->get( 'api.endpoint.payment-token' );
|
$endpoint = $container->get( 'api.endpoint.payment-token' );
|
||||||
return new PaymentTokenRepository( $factory, $endpoint );
|
return new PaymentTokenRepository( $factory, $endpoint );
|
||||||
},
|
},
|
||||||
|
'wc-subscriptions.endpoint.subscription-change-payment-method' => static function( ContainerInterface $container ): SubscriptionChangePaymentMethod {
|
||||||
|
return new SubscriptionChangePaymentMethod(
|
||||||
|
$container->get( 'button.request-data' )
|
||||||
|
);
|
||||||
|
},
|
||||||
);
|
);
|
||||||
|
|
|
@ -0,0 +1,75 @@
|
||||||
|
<?php
|
||||||
|
/**
|
||||||
|
* The Create Payment Token endpoint.
|
||||||
|
*
|
||||||
|
* @package WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint
|
||||||
|
*/
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint;
|
||||||
|
|
||||||
|
use Exception;
|
||||||
|
use WC_Payment_Tokens;
|
||||||
|
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
|
||||||
|
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Class SubscriptionChangePaymentMethod
|
||||||
|
*/
|
||||||
|
class SubscriptionChangePaymentMethod implements EndpointInterface {
|
||||||
|
|
||||||
|
const ENDPOINT = 'ppc-subscription-change-payment-method';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The request data.
|
||||||
|
*
|
||||||
|
* @var RequestData
|
||||||
|
*/
|
||||||
|
private $request_data;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* SubscriptionChangePaymentMethod constructor.
|
||||||
|
*
|
||||||
|
* @param RequestData $request_data $request_data The request data.
|
||||||
|
*/
|
||||||
|
public function __construct( RequestData $request_data ) {
|
||||||
|
$this->request_data = $request_data;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Returns the nonce.
|
||||||
|
*
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public static function nonce(): string {
|
||||||
|
return self::ENDPOINT;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Handles the request.
|
||||||
|
*
|
||||||
|
* @return bool
|
||||||
|
* @throws Exception On Error.
|
||||||
|
*/
|
||||||
|
public function handle_request(): bool {
|
||||||
|
try {
|
||||||
|
$data = $this->request_data->read_request( $this->nonce() );
|
||||||
|
|
||||||
|
$subscription = wcs_get_subscription( $data['subscription_id'] );
|
||||||
|
$subscription->set_payment_method( $data['payment_method'] );
|
||||||
|
|
||||||
|
$wc_payment_token = WC_Payment_Tokens::get( $data['wc_payment_token_id'] );
|
||||||
|
if ( $wc_payment_token ) {
|
||||||
|
$subscription->add_payment_token( $wc_payment_token );
|
||||||
|
$subscription->save();
|
||||||
|
}
|
||||||
|
|
||||||
|
wp_send_json_success();
|
||||||
|
return true;
|
||||||
|
} catch ( Exception $exception ) {
|
||||||
|
wp_send_json_error();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -23,6 +23,7 @@ use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\TransactionIdHandlingTrait;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Endpoint\SubscriptionChangePaymentMethod;
|
||||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -169,6 +170,16 @@ class WcSubscriptionsModule implements ModuleInterface {
|
||||||
return $methods;
|
return $methods;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
add_action(
|
||||||
|
'wc_ajax_' . SubscriptionChangePaymentMethod::ENDPOINT,
|
||||||
|
static function () use ( $c ) {
|
||||||
|
$endpoint = $c->get( 'wc-subscriptions.endpoint.subscription-change-payment-method' );
|
||||||
|
assert( $endpoint instanceof SubscriptionChangePaymentMethod );
|
||||||
|
|
||||||
|
$endpoint->handle_request();
|
||||||
|
}
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue