mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add wc invoice id when creating PayPal order
This commit is contained in:
parent
d2f39be90b
commit
2527f45a1e
5 changed files with 40 additions and 108 deletions
|
@ -127,19 +127,7 @@ class CardFieldsRenderer {
|
||||||
|
|
||||||
const paymentToken = document.querySelector('input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked')?.value
|
const paymentToken = document.querySelector('input[name="wc-ppcp-credit-card-gateway-payment-token"]:checked')?.value
|
||||||
if(paymentToken && paymentToken !== 'new') {
|
if(paymentToken && paymentToken !== 'new') {
|
||||||
fetch(this.defaultConfig.ajax.capture_card_payment.endpoint, {
|
document.querySelector('#place_order').click();
|
||||||
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -821,7 +821,6 @@ return array(
|
||||||
},
|
},
|
||||||
'save-payment-methods.endpoint.capture-card-payment' => static function( ContainerInterface $container ): CaptureCardPayment {
|
'save-payment-methods.endpoint.capture-card-payment' => static function( ContainerInterface $container ): CaptureCardPayment {
|
||||||
return new CaptureCardPayment(
|
return new CaptureCardPayment(
|
||||||
$container->get( 'button.request-data' ),
|
|
||||||
$container->get( 'api.host' ),
|
$container->get( 'api.host' ),
|
||||||
$container->get( 'api.bearer' ),
|
$container->get( 'api.bearer' ),
|
||||||
$container->get( 'api.factory.order' ),
|
$container->get( 'api.factory.order' ),
|
||||||
|
|
|
@ -12,15 +12,12 @@ namespace WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
use RuntimeException;
|
use RuntimeException;
|
||||||
use stdClass;
|
use stdClass;
|
||||||
use WC_Payment_Tokens;
|
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
use WooCommerce\PayPalCommerce\ApiClient\Factory\OrderFactory;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
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\Session\SessionHandler;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
use WooCommerce\PayPalCommerce\WcGateway\Settings\Settings;
|
||||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\RealTimeAccountUpdaterHelper;
|
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\RealTimeAccountUpdaterHelper;
|
||||||
|
@ -29,19 +26,10 @@ use WP_Error;
|
||||||
/**
|
/**
|
||||||
* Class CaptureCardPayment
|
* Class CaptureCardPayment
|
||||||
*/
|
*/
|
||||||
class CaptureCardPayment implements EndpointInterface {
|
class CaptureCardPayment {
|
||||||
|
|
||||||
use RequestTrait;
|
use RequestTrait;
|
||||||
|
|
||||||
const ENDPOINT = 'ppc-capture-card-payment';
|
|
||||||
|
|
||||||
/**
|
|
||||||
* The request data.
|
|
||||||
*
|
|
||||||
* @var RequestData
|
|
||||||
*/
|
|
||||||
private $request_data;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The host.
|
* The host.
|
||||||
*
|
*
|
||||||
|
@ -108,7 +96,6 @@ class CaptureCardPayment implements EndpointInterface {
|
||||||
/**
|
/**
|
||||||
* CaptureCardPayment constructor.
|
* CaptureCardPayment constructor.
|
||||||
*
|
*
|
||||||
* @param RequestData $request_data The request data.
|
|
||||||
* @param string $host The host.
|
* @param string $host The host.
|
||||||
* @param Bearer $bearer The bearer.
|
* @param Bearer $bearer The bearer.
|
||||||
* @param OrderFactory $order_factory The order factory.
|
* @param OrderFactory $order_factory The order factory.
|
||||||
|
@ -120,7 +107,6 @@ class CaptureCardPayment implements EndpointInterface {
|
||||||
* @param LoggerInterface $logger The logger.
|
* @param LoggerInterface $logger The logger.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
RequestData $request_data,
|
|
||||||
string $host,
|
string $host,
|
||||||
Bearer $bearer,
|
Bearer $bearer,
|
||||||
OrderFactory $order_factory,
|
OrderFactory $order_factory,
|
||||||
|
@ -131,7 +117,6 @@ class CaptureCardPayment implements EndpointInterface {
|
||||||
Settings $settings,
|
Settings $settings,
|
||||||
LoggerInterface $logger
|
LoggerInterface $logger
|
||||||
) {
|
) {
|
||||||
$this->request_data = $request_data;
|
|
||||||
$this->host = $host;
|
$this->host = $host;
|
||||||
$this->bearer = $bearer;
|
$this->bearer = $bearer;
|
||||||
$this->order_factory = $order_factory;
|
$this->order_factory = $order_factory;
|
||||||
|
@ -139,66 +124,8 @@ class CaptureCardPayment implements EndpointInterface {
|
||||||
$this->order_endpoint = $order_endpoint;
|
$this->order_endpoint = $order_endpoint;
|
||||||
$this->session_handler = $session_handler;
|
$this->session_handler = $session_handler;
|
||||||
$this->real_time_account_updater_helper = $real_time_account_updater_helper;
|
$this->real_time_account_updater_helper = $real_time_account_updater_helper;
|
||||||
$this->logger = $logger;
|
|
||||||
$this->settings = $settings;
|
$this->settings = $settings;
|
||||||
}
|
$this->logger = $logger;
|
||||||
|
|
||||||
/**
|
|
||||||
* 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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -208,7 +135,7 @@ class CaptureCardPayment implements EndpointInterface {
|
||||||
* @return stdClass
|
* @return stdClass
|
||||||
* @throws RuntimeException When request fails.
|
* @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';
|
$intent = $this->settings->has( 'intent' ) && strtoupper( (string) $this->settings->get( 'intent' ) ) === 'AUTHORIZE' ? 'AUTHORIZE' : 'CAPTURE';
|
||||||
$items = array( $this->purchase_unit_factory->from_wc_cart() );
|
$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();
|
$bearer = $this->bearer->bearer();
|
||||||
|
|
|
@ -131,6 +131,8 @@ return array(
|
||||||
$vaulted_credit_card_handler,
|
$vaulted_credit_card_handler,
|
||||||
$container->get( 'onboarding.environment' ),
|
$container->get( 'onboarding.environment' ),
|
||||||
$container->get( 'api.endpoint.order' ),
|
$container->get( 'api.endpoint.order' ),
|
||||||
|
$container->get('save-payment-methods.endpoint.capture-card-payment'),
|
||||||
|
$container->get( 'api.prefix' ),
|
||||||
$logger
|
$logger
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
|
|
|
@ -19,6 +19,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
use WooCommerce\PayPalCommerce\Onboarding\Environment;
|
||||||
use WooCommerce\PayPalCommerce\Onboarding\State;
|
use WooCommerce\PayPalCommerce\Onboarding\State;
|
||||||
|
use WooCommerce\PayPalCommerce\SavePaymentMethods\Endpoint\CaptureCardPayment;
|
||||||
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
use WooCommerce\PayPalCommerce\Session\SessionHandler;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\AuthorizedPaymentsProcessor;
|
||||||
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
|
use WooCommerce\PayPalCommerce\WcGateway\Processor\PaymentsStatusHandlingTrait;
|
||||||
|
@ -139,6 +140,20 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
*/
|
*/
|
||||||
private $order_endpoint;
|
private $order_endpoint;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Capture card payment.
|
||||||
|
*
|
||||||
|
* @var CaptureCardPayment
|
||||||
|
*/
|
||||||
|
private $capture_card_payment;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The prefix.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
private $prefix;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The logger.
|
* 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 VaultedCreditCardHandler $vaulted_credit_card_handler The vaulted credit card handler.
|
||||||
* @param Environment $environment The environment.
|
* @param Environment $environment The environment.
|
||||||
* @param OrderEndpoint $order_endpoint The order endpoint.
|
* @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.
|
* @param LoggerInterface $logger The logger.
|
||||||
*/
|
*/
|
||||||
public function __construct(
|
public function __construct(
|
||||||
|
@ -178,6 +195,8 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
VaultedCreditCardHandler $vaulted_credit_card_handler,
|
VaultedCreditCardHandler $vaulted_credit_card_handler,
|
||||||
Environment $environment,
|
Environment $environment,
|
||||||
OrderEndpoint $order_endpoint,
|
OrderEndpoint $order_endpoint,
|
||||||
|
CaptureCardPayment $capture_card_payment,
|
||||||
|
string $prefix,
|
||||||
LoggerInterface $logger
|
LoggerInterface $logger
|
||||||
) {
|
) {
|
||||||
$this->id = self::ID;
|
$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->vaulted_credit_card_handler = $vaulted_credit_card_handler;
|
||||||
$this->environment = $environment;
|
$this->environment = $environment;
|
||||||
$this->order_endpoint = $order_endpoint;
|
$this->order_endpoint = $order_endpoint;
|
||||||
|
$this->capture_card_payment = $capture_card_payment;
|
||||||
|
$this->prefix = $prefix;
|
||||||
$this->logger = $logger;
|
$this->logger = $logger;
|
||||||
|
|
||||||
if ( $state->current_state() === State::STATE_ONBOARDED ) {
|
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' );
|
$card_payment_token_id = wc_clean( wp_unslash( $_POST['wc-ppcp-credit-card-gateway-payment-token'] ?? '' ) );
|
||||||
if ( $saved_payment_card ) {
|
if($card_payment_token_id) {
|
||||||
if ( $saved_payment_card['payment_source'] === 'card' && $saved_payment_card['status'] === 'COMPLETED' ) {
|
$tokens = WC_Payment_Tokens::get_customer_tokens( get_current_user_id() );
|
||||||
$wc_order->update_meta_data( PayPalGateway::ORDER_ID_META_KEY, $saved_payment_card['order_id'] );
|
foreach ( $tokens as $token ) {
|
||||||
$wc_order->update_meta_data(
|
if ( $token->get_id() === (int) $card_payment_token_id ) {
|
||||||
PayPalGateway::ORDER_PAYMENT_MODE_META_KEY,
|
$custom_id = $wc_order->get_order_number();
|
||||||
$this->environment->current_environment_is( Environment::SANDBOX ) ? 'sandbox' : 'live'
|
$invoice_id = $this->prefix . $wc_order->get_order_number();
|
||||||
);
|
$create_order = $this->capture_card_payment->create_order($token->get_token(), $custom_id, $invoice_id);
|
||||||
$wc_order->save_meta_data();
|
|
||||||
|
|
||||||
$order_id = $saved_payment_card['order_id'] ?? '';
|
$order = $this->order_endpoint->order( $create_order->id );
|
||||||
if ( $order_id ) {
|
|
||||||
$order = $this->order_endpoint->order( $order_id );
|
|
||||||
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
|
$wc_order->update_meta_data( PayPalGateway::INTENT_META_KEY, $order->intent() );
|
||||||
|
|
||||||
if ( $order->intent() === 'AUTHORIZE' ) {
|
if ( $order->intent() === 'AUTHORIZE' ) {
|
||||||
|
@ -421,14 +439,10 @@ class CreditCardGateway extends \WC_Payment_Gateway_CC {
|
||||||
}
|
}
|
||||||
|
|
||||||
$this->handle_new_order_status( $order, $wc_order );
|
$this->handle_new_order_status( $order, $wc_order );
|
||||||
|
|
||||||
|
return $this->handle_payment_success( $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 );
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue