Skip to content

Commit

Permalink
Create WC payment method from card payment token
Browse files Browse the repository at this point in the history
  • Loading branch information
Emili Castells Guasch authored and Emili Castells Guasch committed Dec 12, 2023
1 parent 8a4bcc5 commit 10e79cb
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ const init = () => {
},
body: JSON.stringify({
nonce: ppcp_add_payment_method.ajax.create_setup_token.nonce,
payment_method: PaymentMethods.CARDS
})
})

Expand All @@ -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
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
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
Expand Down Expand Up @@ -98,16 +100,34 @@ public function handle_request(): bool {
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 );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
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
Expand Down Expand Up @@ -67,13 +68,8 @@ public static function nonce(): string {
*/
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(
Expand All @@ -85,6 +81,14 @@ public function handle_request(): bool {
)
);

$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 );
Expand Down

0 comments on commit 10e79cb

Please sign in to comment.