Add wc invoice id when creating PayPal order

This commit is contained in:
Emili Castells Guasch 2024-03-08 17:38:43 +01:00
parent d2f39be90b
commit 2527f45a1e
5 changed files with 40 additions and 108 deletions

View file

@ -127,19 +127,7 @@ class CardFieldsRenderer {
const paymentToken = document.querySelector('input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked')?.value
if(paymentToken && paymentToken !== 'new') {
fetch(this.defaultConfig.ajax.capture_card_payment.endpoint, {
method: 'POST',
credentials: 'same-origin',
body: JSON.stringify({
nonce: this.defaultConfig.ajax.capture_card_payment.nonce,
payment_token: paymentToken
})
}).then((res) => {
return res.json();
}).then((data) => {
document.querySelector('#place_order').click();
});
return;
}

View file

@ -821,7 +821,6 @@ return array(
},
'save-payment-methods.endpoint.capture-card-payment' => static function( ContainerInterface $container ): CaptureCardPayment {
return new CaptureCardPayment(
$container->get( 'button.request-data' ),
$container->get( 'api.host' ),
$container->get( 'api.bearer' ),
$container->get( 'api.factory.order' ),

View file

@ -12,15 +12,12 @@ namespace WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint;
use Psr\Log\LoggerInterface;
use RuntimeException;
use stdClass;
use WC_Payment_Tokens;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
use WooCommerce\PayPalCommerce\Button\Endpoint\EndpointInterface;
use WooCommerce\PayPalCommerce\Button\Endpoint\RequestData;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\RealTimeAccountUpdaterHelper;
@ -29,19 +26,10 @@ use WP_Error;
/**
* Class CaptureCardPayment
*/
class CaptureCardPayment implements EndpointInterface {
class CaptureCardPayment {
use RequestTrait;
const ENDPOINT = 'ppc-capture-card-payment';
/**
* The request data.
*
* @var RequestData
*/
private $request_data;
/**
* The host.
*
@ -108,7 +96,6 @@ class CaptureCardPayment implements EndpointInterface {
/**
* CaptureCardPayment constructor.
*
* @param RequestData $request_data The request data.
* @param string $host The host.
* @param Bearer $bearer The bearer.
* @param OrderFactory $order_factory The order factory.
@ -120,7 +107,6 @@ class CaptureCardPayment implements EndpointInterface {
* @param LoggerInterface $logger The logger.
*/
public function __construct(
RequestData $request_data,
string $host,
Bearer $bearer,
OrderFactory $order_factory,
@ -131,7 +117,6 @@ class CaptureCardPayment implements EndpointInterface {
Settings $settings,
LoggerInterface $logger
) {
$this->request_data = $request_data;
$this->host = $host;
$this->bearer = $bearer;
$this->order_factory = $order_factory;
@ -139,66 +124,8 @@ class CaptureCardPayment implements EndpointInterface {
$this->order_endpoint = $order_endpoint;
$this->session_handler = $session_handler;
$this->real_time_account_updater_helper = $real_time_account_updater_helper;
$this->logger = $logger;
$this->settings = $settings;
}
/**
* Returns the nonce.
*
* @return string
*/
public static function nonce(): string {
return self::ENDPOINT;
}
/**
* Handles the request.
*
* @return bool
*/
public function handle_request(): bool {
$data = $this->request_data->read_request( $this->nonce() );
$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() );
foreach ( $tokens as $token ) {
if ( $token->get_id() === (int) $data['payment_token'] ) {
try {
$order = $this->create_order( $token->get_token() );
$id = $order->id ?? '';
$status = $order->status ?? '';
$payment_source = isset( $order->payment_source->card ) ? 'card' : '';
$expiry = $order->payment_source->card->expiry ?? '';
$last_digits = $order->payment_source->card->last_digits ?? '';
if ( $id && $status && $payment_source && $expiry && $last_digits ) {
$this->real_time_account_updater_helper->update_wc_card_token(
$expiry,
$last_digits,
$token
);
WC()->session->set(
'ppcp_saved_payment_card',
array(
'order_id' => $id,
'status' => $status,
'payment_source' => $payment_source,
)
);
wp_send_json_success();
return true;
}
} catch ( RuntimeException $exception ) {
wp_send_json_error();
return false;
}
}
}
wp_send_json_error();
return false;
$this->logger = $logger;
}
/**
@ -208,7 +135,7 @@ class CaptureCardPayment implements EndpointInterface {
* @return stdClass
* @throws RuntimeException When request fails.
*/
private function create_order( string $vault_id ): stdClass {
public function create_order( string $vault_id, string $custom_id, string $invoice_id ): stdClass {
$intent = $this->settings->has( 'intent' ) && strtoupper( (string) $this->settings->get( 'intent' ) ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE';
$items = array( $this->purchase_unit_factory->from_wc_cart() );
@ -230,6 +157,8 @@ class CaptureCardPayment implements EndpointInterface {
),
),
),
'custom_id' => $custom_id,
'invoice_id' => $invoice_id,
);
$bearer = $this->bearer->bearer();

View file

@ -131,6 +131,8 @@ return array(
$vaulted_credit_card_handler,
$container->get( 'onboarding.environment' ),
$container->get( 'api.endpoint.order' ),
$container->get('save-payment-methods.endpoint.capture-card-payment'),
$container->get( 'api.prefix' ),
$logger
);
},

View file

@ -19,6 +19,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
use WooCommerce\PayPalCommerce\Onboarding\Environment;
use WooCommerce\PayPalCommerce\Onboarding\State;
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CaptureCardPayment;
use WooCommerce\PayPalCommerce\Session\SessionHandler;
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
@ -139,6 +140,20 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
*/
private $order_endpoint;
/**
* Capture card payment.
*
* @var CaptureCardPayment
*/
private $capture_card_payment;
/**
* The prefix.
*
* @var string
*/
private $prefix;
/**
* The logger.
*
@ -162,6 +177,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
* @param VaultedCreditCardHandler $vaulted_credit_card_handler The vaulted credit card handler.
* @param Environment $environment The environment.
* @param OrderEndpoint $order_endpoint The order endpoint.
* @param CaptureCardPayment $capture_card_payment Capture card payment.
* @param string $prefix The prefix.
* @param LoggerInterface $logger The logger.
*/
public function __construct(
@ -178,6 +195,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
VaultedCreditCardHandler $vaulted_credit_card_handler,
Environment $environment,
OrderEndpoint $order_endpoint,
CaptureCardPayment $capture_card_payment,
string $prefix,
LoggerInterface $logger
) {
$this->id = self::ID;
@ -194,6 +213,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
$this->vaulted_credit_card_handler = $vaulted_credit_card_handler;
$this->environment = $environment;
$this->order_endpoint = $order_endpoint;
$this->capture_card_payment = $capture_card_payment;
$this->prefix = $prefix;
$this->logger = $logger;
if ( $state->current_state() === State::STATE_ONBOARDED ) {
@ -390,19 +411,16 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
);
}
$saved_payment_card = WC()->session->get( 'ppcp_saved_payment_card' );
if ( $saved_payment_card ) {
if ( $saved_payment_card['payment_source'] === 'card' && $saved_payment_card['status'] === 'COMPLETED' ) {
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $saved_payment_card['order_id'] );
$wc_order->update_meta_data(
PayPalGateway::ORDER_PAYMENT_MODE_META_KEY,
$this->environment->current_environment_is( Environment::SANDBOX ) ? 'sandbox' : 'live'
);
$wc_order->save_meta_data();
$card_payment_token_id = wc_clean( wp_unslash( $_POST['wc-ppcp-credit-card-gateway-payment-token'] ?? '' ) );
if($card_payment_token_id) {
$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() );
foreach ( $tokens as $token ) {
if ( $token->get_id() === (int) $card_payment_token_id ) {
$custom_id = $wc_order->get_order_number();
$invoice_id = $this->prefix . $wc_order->get_order_number();
$create_order = $this->capture_card_payment->create_order($token->get_token(), $custom_id, $invoice_id);
$order_id = $saved_payment_card['order_id'] ?? '';
if ( $order_id ) {
$order = $this->order_endpoint->order( $order_id );
$order = $this->order_endpoint->order( $create_order->id );
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
if ( $order->intent() === 'AUTHORIZE' ) {
@ -421,14 +439,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
}
$this->handle_new_order_status( $order, $wc_order );
}
WC()->session->set( 'ppcp_saved_payment_card', null );
return $this->handle_payment_success( $wc_order );
}
WC()->session->set( 'ppcp_saved_payment_card', null );
}
}
/**