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
|
@ -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' ),
|
||||
|
|
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue