mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Create WC payment method from card payment token
This commit is contained in:
parent
8a4bcc55a1
commit
10e79cbcdc
3 changed files with 40 additions and 14 deletions
|
@ -88,6 +88,7 @@ const init = () => {
|
|||
},
|
||||
body: JSON.stringify({
|
||||
nonce: ppcp_add_payment_method.ajax.create_setup_token.nonce,
|
||||
payment_method: PaymentMethods.CARDS
|
||||
})
|
||||
})
|
||||
|
||||
|
@ -106,6 +107,7 @@ const init = () => {
|
|||
body: JSON.stringify({
|
||||
nonce: ppcp_add_payment_method.ajax.create_payment_token.nonce,
|
||||
vault_setup_token: vaultSetupToken,
|
||||
payment_method: PaymentMethods.CARDS
|
||||
})
|
||||
})
|
||||
|
||||
|
|
|
@ -15,6 +15,8 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
|||
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
|
||||
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
|
||||
use WooCommerce\PayPalCommerce\SavePaymentMethods\WooCommercePaymentTokens;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
|
||||
/**
|
||||
* Class CreatePaymentToken
|
||||
|
@ -98,16 +100,34 @@ class CreatePaymentToken implements EndpointInterface {
|
|||
if ( is_user_logged_in() && isset( $result->customer->id ) ) {
|
||||
update_user_meta( get_current_user_id(), '_ppcp_target_customer_id', $result->customer->id );
|
||||
|
||||
$email = '';
|
||||
if ( isset( $result->payment_source->paypal->email_address ) ) {
|
||||
$email = $result->payment_source->paypal->email_address;
|
||||
$payment_method = $data['payment_method'] ?? '';
|
||||
if($payment_method === PayPalGateway::ID) {
|
||||
$email = '';
|
||||
if ( isset( $result->payment_source->paypal->email_address ) ) {
|
||||
$email = $result->payment_source->paypal->email_address;
|
||||
}
|
||||
|
||||
$this->wc_payment_tokens->create_payment_token_paypal(
|
||||
get_current_user_id(),
|
||||
$result->id,
|
||||
$email
|
||||
);
|
||||
}
|
||||
|
||||
$this->wc_payment_tokens->create_payment_token_paypal(
|
||||
get_current_user_id(),
|
||||
$result->id,
|
||||
$email
|
||||
);
|
||||
if ($payment_method === CreditCardGateway::ID) {
|
||||
$token = new \WC_Payment_Token_CC();
|
||||
$token->set_token($result->id);
|
||||
$token->set_user_id(get_current_user_id());
|
||||
$token->set_gateway_id(CreditCardGateway::ID);
|
||||
|
||||
$token->set_last4($result->payment_source->card->last_digits ?? '');
|
||||
$expiry = explode('-', $result->payment_source->card->expiry ?? '');
|
||||
$token->set_expiry_year($expiry[0] ?? '');
|
||||
$token->set_expiry_month($expiry[1] ?? '');
|
||||
$token->set_card_type($result->payment_source->card->brand ?? '');
|
||||
|
||||
$token->save();
|
||||
}
|
||||
}
|
||||
|
||||
wp_send_json_success( $result );
|
||||
|
|
|
@ -14,6 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentMethodTokensEndpoint;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
|
||||
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
|
||||
/**
|
||||
* Class CreateSetupToken
|
||||
|
@ -67,13 +68,8 @@ class CreateSetupToken implements EndpointInterface {
|
|||
*/
|
||||
public function handle_request(): bool {
|
||||
try {
|
||||
$this->request_data->read_request( $this->nonce() );
|
||||
$data = $this->request_data->read_request( $this->nonce() );
|
||||
|
||||
/**
|
||||
* Suppress ArgumentTypeCoercion
|
||||
*
|
||||
* @psalm-suppress ArgumentTypeCoercion
|
||||
*/
|
||||
$payment_source = new PaymentSource(
|
||||
'paypal',
|
||||
(object) array(
|
||||
|
@ -85,6 +81,14 @@ class CreateSetupToken implements EndpointInterface {
|
|||
)
|
||||
);
|
||||
|
||||
$payment_method = $data['payment_method'] ?? '';
|
||||
if($payment_method === CreditCardGateway::ID) {
|
||||
$payment_source = new PaymentSource(
|
||||
'card',
|
||||
(object) array()
|
||||
);
|
||||
}
|
||||
|
||||
$result = $this->payment_method_tokens_endpoint->setup_tokens( $payment_source );
|
||||
|
||||
wp_send_json_success( $result );
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue