mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 14:57:26 +08:00
Merge branch 'trunk' into fix/PCP-696-improve-error-message
This commit is contained in:
commit
e762a17bdc
253 changed files with 21563 additions and 4172 deletions
File diff suppressed because it is too large
Load diff
105
modules/ppcp-api-client/src/Authentication/UserIdToken.php
Normal file
105
modules/ppcp-api-client/src/Authentication/UserIdToken.php
Normal file
|
@ -0,0 +1,105 @@
|
|||
<?php
|
||||
/**
|
||||
* Generates user ID token for payer.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Authentication
|
||||
*/
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Authentication;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\RequestTrait;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WP_Error;
|
||||
|
||||
/**
|
||||
* Class UserIdToken
|
||||
*/
|
||||
class UserIdToken {
|
||||
|
||||
use RequestTrait;
|
||||
|
||||
/**
|
||||
* The host.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $host;
|
||||
|
||||
/**
|
||||
* The bearer.
|
||||
*
|
||||
* @var Bearer
|
||||
*/
|
||||
private $bearer;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* UserIdToken constructor.
|
||||
*
|
||||
* @param string $host The host.
|
||||
* @param Bearer $bearer The bearer.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
*/
|
||||
public function __construct(
|
||||
string $host,
|
||||
Bearer $bearer,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `id_token` which uniquely identifies the payer.
|
||||
*
|
||||
* @param string $target_customer_id Vaulted customer id.
|
||||
*
|
||||
* @return string
|
||||
*
|
||||
* @throws PayPalApiException If the request fails.
|
||||
* @throws RuntimeException If something unexpected happens.
|
||||
*/
|
||||
public function id_token( string $target_customer_id = '' ): string {
|
||||
$bearer = $this->bearer->bearer();
|
||||
|
||||
$url = trailingslashit( $this->host ) . 'v1/oauth2/token?grant_type=client_credentials&response_type=id_token';
|
||||
if ( $target_customer_id ) {
|
||||
$url = add_query_arg(
|
||||
array(
|
||||
'target_customer_id' => $target_customer_id,
|
||||
),
|
||||
$url
|
||||
);
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/x-www-form-urlencoded',
|
||||
),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
if ( $response instanceof WP_Error ) {
|
||||
throw new RuntimeException( $response->get_error_message() );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 200 !== $status_code ) {
|
||||
throw new PayPalApiException( $json, $status_code );
|
||||
}
|
||||
|
||||
return $json->id_token;
|
||||
}
|
||||
}
|
|
@ -18,6 +18,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PatchCollection;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Payer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
|
@ -27,7 +28,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PatchCollectionFactory;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Helper\ErrorResponse;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\Subscription\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\Helper\SubscriptionHelper;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\FraudNet\FraudNet;
|
||||
use WP_Error;
|
||||
|
||||
|
@ -174,12 +175,15 @@ class OrderEndpoint {
|
|||
/**
|
||||
* Creates an order.
|
||||
*
|
||||
* @param PurchaseUnit[] $items The purchase unit items for the order.
|
||||
* @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values.
|
||||
* @param Payer|null $payer The payer off the order.
|
||||
* @param PaymentToken|null $payment_token The payment token.
|
||||
* @param string $paypal_request_id The paypal request id.
|
||||
* @param string $user_action The user action.
|
||||
* @param PurchaseUnit[] $items The purchase unit items for the order.
|
||||
* @param string $shipping_preference One of ApplicationContext::SHIPPING_PREFERENCE_ values.
|
||||
* @param Payer|null $payer The payer off the order.
|
||||
* @param PaymentToken|null $payment_token The payment token.
|
||||
* @param string $paypal_request_id The PayPal request id.
|
||||
* @param string $user_action The user action.
|
||||
* @param string $payment_method WC payment method.
|
||||
* @param array $request_data Request data.
|
||||
* @param PaymentSource|null $payment_source The payment source.
|
||||
*
|
||||
* @return Order
|
||||
* @throws RuntimeException If the request fails.
|
||||
|
@ -190,7 +194,10 @@ class OrderEndpoint {
|
|||
Payer $payer = null,
|
||||
PaymentToken $payment_token = null,
|
||||
string $paypal_request_id = '',
|
||||
string $user_action = ApplicationContext::USER_ACTION_CONTINUE
|
||||
string $user_action = ApplicationContext::USER_ACTION_CONTINUE,
|
||||
string $payment_method = '',
|
||||
array $request_data = array(),
|
||||
PaymentSource $payment_source = null
|
||||
): Order {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$data = array(
|
||||
|
@ -217,11 +224,16 @@ class OrderEndpoint {
|
|||
if ( $payment_token ) {
|
||||
$data['payment_source']['token'] = $payment_token->to_array();
|
||||
}
|
||||
if ( $payment_source ) {
|
||||
$data['payment_source'] = array(
|
||||
$payment_source->name() => $payment_source->properties(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* The filter can be used to modify the order creation request body data.
|
||||
*/
|
||||
$data = apply_filters( 'ppcp_create_order_request_body_data', $data );
|
||||
$data = apply_filters( 'ppcp_create_order_request_body_data', $data, $payment_method, $request_data );
|
||||
$url = trailingslashit( $this->host ) . 'v2/checkout/orders';
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
|
@ -260,26 +272,25 @@ class OrderEndpoint {
|
|||
);
|
||||
throw $error;
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 201 !== $status_code ) {
|
||||
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
|
||||
$error = new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
|
||||
$this->logger->warning(
|
||||
sprintf(
|
||||
'Failed to create order. PayPal API response: %1$s',
|
||||
$error->getMessage()
|
||||
),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
|
||||
throw $error;
|
||||
}
|
||||
|
||||
$order = $this->order_factory->from_paypal_response( $json );
|
||||
|
||||
do_action( 'woocommerce_paypal_payments_paypal_order_created', $order );
|
||||
|
|
|
@ -0,0 +1,155 @@
|
|||
<?php
|
||||
/**
|
||||
* The Payment Method Tokens endpoint.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use stdClass;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class PaymentMethodTokensEndpoint
|
||||
*/
|
||||
class PaymentMethodTokensEndpoint {
|
||||
|
||||
use RequestTrait;
|
||||
|
||||
/**
|
||||
* The host.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $host;
|
||||
|
||||
/**
|
||||
* The bearer.
|
||||
*
|
||||
* @var Bearer
|
||||
*/
|
||||
private $bearer;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* PaymentMethodTokensEndpoint constructor.
|
||||
*
|
||||
* @param string $host The host.
|
||||
* @param Bearer $bearer The bearer.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
*/
|
||||
public function __construct( string $host, Bearer $bearer, LoggerInterface $logger ) {
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a setup token.
|
||||
*
|
||||
* @param PaymentSource $payment_source The payment source.
|
||||
*
|
||||
* @return stdClass
|
||||
*
|
||||
* @throws RuntimeException When something when wrong with the request.
|
||||
* @throws PayPalApiException When something when wrong setting up the token.
|
||||
*/
|
||||
public function setup_tokens( PaymentSource $payment_source ): stdClass {
|
||||
$data = array(
|
||||
'payment_source' => array(
|
||||
$payment_source->name() => $payment_source->properties(),
|
||||
),
|
||||
);
|
||||
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v3/vault/setup-tokens';
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
|
||||
throw new RuntimeException( 'Not able to create setup token.' );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
|
||||
throw new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a payment token for the given payment source.
|
||||
*
|
||||
* @param PaymentSource $payment_source The payment source.
|
||||
*
|
||||
* @return stdClass
|
||||
*
|
||||
* @throws RuntimeException When something when wrong with the request.
|
||||
* @throws PayPalApiException When something when wrong setting up the token.
|
||||
*/
|
||||
public function create_payment_token( PaymentSource $payment_source ): stdClass {
|
||||
$data = array(
|
||||
'payment_source' => array(
|
||||
$payment_source->name() => $payment_source->properties(),
|
||||
),
|
||||
);
|
||||
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens';
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'PayPal-Request-Id' => uniqid( 'ppcp-', true ),
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
|
||||
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
|
||||
throw new RuntimeException( 'Not able to create setup token.' );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( ! in_array( $status_code, array( 200, 201 ), true ) ) {
|
||||
throw new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
}
|
||||
|
||||
return $json;
|
||||
}
|
||||
}
|
145
modules/ppcp-api-client/src/Endpoint/PaymentTokensEndpoint.php
Normal file
145
modules/ppcp-api-client/src/Endpoint/PaymentTokensEndpoint.php
Normal file
|
@ -0,0 +1,145 @@
|
|||
<?php
|
||||
/**
|
||||
* Payment tokens version 3 endpoint.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
|
||||
|
||||
use Psr\Log\LoggerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WP_Error;
|
||||
|
||||
/**
|
||||
* Class PaymentTokensEndpoint
|
||||
*/
|
||||
class PaymentTokensEndpoint {
|
||||
|
||||
use RequestTrait;
|
||||
|
||||
/**
|
||||
* The bearer.
|
||||
*
|
||||
* @var Bearer
|
||||
*/
|
||||
private $bearer;
|
||||
|
||||
/**
|
||||
* The host.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $host;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* PaymentTokensEndpoint constructor.
|
||||
*
|
||||
* @param string $host The bearer.
|
||||
* @param Bearer $bearer The bearer.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
*/
|
||||
public function __construct(
|
||||
string $host,
|
||||
Bearer $bearer,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a payment token with the given id.
|
||||
*
|
||||
* @param string $id Payment token id.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws RuntimeException When something went wrong with the request.
|
||||
* @throws PayPalApiException When something went wrong deleting the payment token.
|
||||
*/
|
||||
public function delete( string $id ): void {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens/' . $id;
|
||||
$args = array(
|
||||
'method' => 'DELETE',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
if ( $response instanceof WP_Error ) {
|
||||
throw new RuntimeException( $response->get_error_message() );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 204 !== $status_code ) {
|
||||
throw new PayPalApiException( $json, $status_code );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns all payment tokens for the given customer.
|
||||
*
|
||||
* @param string $customer_id PayPal customer id.
|
||||
* @return array
|
||||
*
|
||||
* @throws RuntimeException When something went wrong with the request.
|
||||
* @throws PayPalApiException When something went wrong getting the payment tokens.
|
||||
*/
|
||||
public function payment_tokens_for_customer( string $customer_id ): array {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v3/vault/payment-tokens?customer_id=' . $customer_id;
|
||||
$args = array(
|
||||
'method' => 'GET',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
if ( $response instanceof WP_Error ) {
|
||||
throw new RuntimeException( $response->get_error_message() );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 200 !== $status_code ) {
|
||||
throw new PayPalApiException( $json, $status_code );
|
||||
}
|
||||
|
||||
$tokens = array();
|
||||
$payment_tokens = $json->payment_tokens ?? array();
|
||||
foreach ( $payment_tokens as $payment_token ) {
|
||||
$name = array_key_first( (array) $payment_token->payment_source ) ?? '';
|
||||
if ( $name ) {
|
||||
$tokens[] = array(
|
||||
'id' => $payment_token->id,
|
||||
'payment_source' => new PaymentSource(
|
||||
$name,
|
||||
$payment_token->payment_source->$name
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $tokens;
|
||||
}
|
||||
}
|
|
@ -193,6 +193,53 @@ class PaymentsEndpoint {
|
|||
return $this->capture_factory->from_paypal_response( $json );
|
||||
}
|
||||
|
||||
/**
|
||||
* Reauthorizes an order.
|
||||
*
|
||||
* @param string $authorization_id The id.
|
||||
* @param Money|null $amount The amount to capture. If not specified, the whole authorized amount is captured.
|
||||
*
|
||||
* @return string
|
||||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
*/
|
||||
public function reauthorize( string $authorization_id, ?Money $amount = null ) : string {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v2/payments/authorizations/' . $authorization_id . '/reauthorize';
|
||||
|
||||
$data = array();
|
||||
if ( $amount ) {
|
||||
$data['amount'] = $amount->to_array();
|
||||
}
|
||||
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation',
|
||||
),
|
||||
'body' => wp_json_encode( $data, JSON_FORCE_OBJECT ),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
$json = json_decode( $response['body'] );
|
||||
|
||||
if ( is_wp_error( $response ) ) {
|
||||
throw new RuntimeException( 'Could not reauthorize authorized payment.' );
|
||||
}
|
||||
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 201 !== $status_code || ! is_object( $json ) ) {
|
||||
throw new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
}
|
||||
|
||||
return $json->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Refunds a payment.
|
||||
*
|
||||
|
|
|
@ -28,19 +28,29 @@ class Authorization {
|
|||
*/
|
||||
private $authorization_status;
|
||||
|
||||
/**
|
||||
* The fraud processor response (AVS, CVV ...).
|
||||
*
|
||||
* @var FraudProcessorResponse|null
|
||||
*/
|
||||
protected $fraud_processor_response;
|
||||
|
||||
/**
|
||||
* Authorization constructor.
|
||||
*
|
||||
* @param string $id The id.
|
||||
* @param AuthorizationStatus $authorization_status The status.
|
||||
* @param string $id The id.
|
||||
* @param AuthorizationStatus $authorization_status The status.
|
||||
* @param FraudProcessorResponse|null $fraud_processor_response The fraud processor response (AVS, CVV ...).
|
||||
*/
|
||||
public function __construct(
|
||||
string $id,
|
||||
AuthorizationStatus $authorization_status
|
||||
AuthorizationStatus $authorization_status,
|
||||
?FraudProcessorResponse $fraud_processor_response
|
||||
) {
|
||||
|
||||
$this->id = $id;
|
||||
$this->authorization_status = $authorization_status;
|
||||
$this->id = $id;
|
||||
$this->authorization_status = $authorization_status;
|
||||
$this->fraud_processor_response = $fraud_processor_response;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -71,15 +81,30 @@ class Authorization {
|
|||
$this->authorization_status->is( AuthorizationStatus::PENDING );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the fraud processor response (AVS, CVV ...).
|
||||
*
|
||||
* @return FraudProcessorResponse|null
|
||||
*/
|
||||
public function fraud_processor_response() : ?FraudProcessorResponse {
|
||||
return $this->fraud_processor_response;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array(): array {
|
||||
return array(
|
||||
$data = array(
|
||||
'id' => $this->id,
|
||||
'status' => $this->authorization_status->name(),
|
||||
);
|
||||
|
||||
if ( $this->fraud_processor_response ) {
|
||||
$data['fraud_processor_response'] = $this->fraud_processor_response->to_array();
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -14,7 +14,6 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
|||
*/
|
||||
class CardAuthenticationResult {
|
||||
|
||||
|
||||
const LIABILITY_SHIFT_POSSIBLE = 'POSSIBLE';
|
||||
const LIABILITY_SHIFT_NO = 'NO';
|
||||
const LIABILITY_SHIFT_UNKNOWN = 'UNKNOWN';
|
||||
|
|
|
@ -107,14 +107,6 @@ class Order {
|
|||
|
||||
$this->id = $id;
|
||||
$this->application_context = $application_context;
|
||||
$this->purchase_units = array_values(
|
||||
array_filter(
|
||||
$purchase_units,
|
||||
static function ( $unit ): bool {
|
||||
return is_a( $unit, PurchaseUnit::class );
|
||||
}
|
||||
)
|
||||
);
|
||||
$this->payer = $payer;
|
||||
$this->order_status = $order_status;
|
||||
$this->intent = ( 'CAPTURE' === $intent ) ? 'CAPTURE' : 'AUTHORIZE';
|
||||
|
@ -236,9 +228,6 @@ class Order {
|
|||
if ( $this->application_context() ) {
|
||||
$order['application_context'] = $this->application_context()->to_array();
|
||||
}
|
||||
if ( $this->payment_source() ) {
|
||||
$order['payment_source'] = $this->payment_source()->to_array();
|
||||
}
|
||||
|
||||
return $order;
|
||||
}
|
||||
|
|
|
@ -15,14 +15,15 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
|||
* Class OrderStatus
|
||||
*/
|
||||
class OrderStatus {
|
||||
const INTERNAL = 'INTERNAL';
|
||||
const CREATED = 'CREATED';
|
||||
const SAVED = 'SAVED';
|
||||
const APPROVED = 'APPROVED';
|
||||
const VOIDED = 'VOIDED';
|
||||
const COMPLETED = 'COMPLETED';
|
||||
const PENDING_APPROVAL = 'PENDING_APPROVAL';
|
||||
const VALID_STATUS = array(
|
||||
const INTERNAL = 'INTERNAL';
|
||||
const CREATED = 'CREATED';
|
||||
const SAVED = 'SAVED';
|
||||
const APPROVED = 'APPROVED';
|
||||
const VOIDED = 'VOIDED';
|
||||
const COMPLETED = 'COMPLETED';
|
||||
const PENDING_APPROVAL = 'PENDING_APPROVAL';
|
||||
const PAYER_ACTION_REQUIRED = 'PAYER_ACTION_REQUIRED';
|
||||
const VALID_STATUS = array(
|
||||
self::INTERNAL,
|
||||
self::CREATED,
|
||||
self::SAVED,
|
||||
|
@ -30,6 +31,7 @@ class OrderStatus {
|
|||
self::VOIDED,
|
||||
self::COMPLETED,
|
||||
self::PENDING_APPROVAL,
|
||||
self::PAYER_ACTION_REQUIRED,
|
||||
);
|
||||
|
||||
/**
|
||||
|
|
|
@ -9,74 +9,53 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
use stdClass;
|
||||
|
||||
/**
|
||||
* Class PaymentSource
|
||||
*/
|
||||
class PaymentSource {
|
||||
|
||||
/**
|
||||
* The card.
|
||||
* Payment source name.
|
||||
*
|
||||
* @var PaymentSourceCard|null
|
||||
* @var string
|
||||
*/
|
||||
private $card;
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* The wallet.
|
||||
* Payment source properties.
|
||||
*
|
||||
* @var PaymentSourceWallet|null
|
||||
* @var object
|
||||
*/
|
||||
private $wallet;
|
||||
private $properties;
|
||||
|
||||
/**
|
||||
* PaymentSource constructor.
|
||||
*
|
||||
* @param PaymentSourceCard|null $card The card.
|
||||
* @param PaymentSourceWallet|null $wallet The wallet.
|
||||
* @param string $name Payment source name.
|
||||
* @param object $properties Payment source properties.
|
||||
*/
|
||||
public function __construct(
|
||||
PaymentSourceCard $card = null,
|
||||
PaymentSourceWallet $wallet = null
|
||||
) {
|
||||
|
||||
$this->card = $card;
|
||||
$this->wallet = $wallet;
|
||||
public function __construct( string $name, object $properties ) {
|
||||
$this->name = $name;
|
||||
$this->properties = $properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the card.
|
||||
* Payment source name.
|
||||
*
|
||||
* @return PaymentSourceCard|null
|
||||
* @return string
|
||||
*/
|
||||
public function card() {
|
||||
|
||||
return $this->card;
|
||||
public function name(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the wallet.
|
||||
* Payment source properties.
|
||||
*
|
||||
* @return PaymentSourceWallet|null
|
||||
* @return object
|
||||
*/
|
||||
public function wallet() {
|
||||
|
||||
return $this->wallet;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the array of the object.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array(): array {
|
||||
|
||||
$data = array();
|
||||
if ( $this->card() ) {
|
||||
$data['card'] = $this->card()->to_array();
|
||||
}
|
||||
if ( $this->wallet() ) {
|
||||
$data['wallet'] = $this->wallet()->to_array();
|
||||
}
|
||||
return $data;
|
||||
public function properties(): object {
|
||||
return $this->properties;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,123 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* The PaymentSourceCard object.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
/**
|
||||
* Class PaymentSourceCard
|
||||
*/
|
||||
class PaymentSourceCard {
|
||||
|
||||
/**
|
||||
* The last digits of the card.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $last_digits;
|
||||
|
||||
/**
|
||||
* The brand.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $brand;
|
||||
|
||||
/**
|
||||
* The type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $type;
|
||||
|
||||
/**
|
||||
* The authentication result.
|
||||
*
|
||||
* @var CardAuthenticationResult|null
|
||||
*/
|
||||
private $authentication_result;
|
||||
|
||||
/**
|
||||
* PaymentSourceCard constructor.
|
||||
*
|
||||
* @param string $last_digits The last digits of the card.
|
||||
* @param string $brand The brand of the card.
|
||||
* @param string $type The type of the card.
|
||||
* @param CardAuthenticationResult|null $authentication_result The authentication result.
|
||||
*/
|
||||
public function __construct(
|
||||
string $last_digits,
|
||||
string $brand,
|
||||
string $type,
|
||||
CardAuthenticationResult $authentication_result = null
|
||||
) {
|
||||
|
||||
$this->last_digits = $last_digits;
|
||||
$this->brand = $brand;
|
||||
$this->type = $type;
|
||||
$this->authentication_result = $authentication_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last digits.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function last_digits(): string {
|
||||
|
||||
return $this->last_digits;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the brand.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function brand(): string {
|
||||
|
||||
return $this->brand;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function type(): string {
|
||||
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the authentication result.
|
||||
*
|
||||
* @return CardAuthenticationResult|null
|
||||
*/
|
||||
public function authentication_result() {
|
||||
|
||||
return $this->authentication_result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array(): array {
|
||||
|
||||
$data = array(
|
||||
'last_digits' => $this->last_digits(),
|
||||
'brand' => $this->brand(),
|
||||
'type' => $this->type(),
|
||||
);
|
||||
if ( $this->authentication_result() ) {
|
||||
$data['authentication_result'] = $this->authentication_result()->to_array();
|
||||
}
|
||||
return $data;
|
||||
}
|
||||
}
|
|
@ -1,25 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* The PaymentSourcewallet.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
/**
|
||||
* Class PaymentSourceWallet
|
||||
*/
|
||||
class PaymentSourceWallet {
|
||||
|
||||
/**
|
||||
* Returns the object as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array(): array {
|
||||
return array();
|
||||
}
|
||||
}
|
|
@ -21,19 +21,37 @@ class SellerStatus {
|
|||
*/
|
||||
private $products;
|
||||
|
||||
/**
|
||||
* The capabilities.
|
||||
*
|
||||
* @var SellerStatusCapability[]
|
||||
*/
|
||||
private $capabilities;
|
||||
|
||||
/**
|
||||
* SellerStatus constructor.
|
||||
*
|
||||
* @param SellerStatusProduct[] $products The products.
|
||||
* @param SellerStatusProduct[] $products The products.
|
||||
* @param SellerStatusCapability[] $capabilities The capabilities.
|
||||
*
|
||||
* @psalm-suppress RedundantConditionGivenDocblockType
|
||||
*/
|
||||
public function __construct( array $products ) {
|
||||
public function __construct( array $products, array $capabilities ) {
|
||||
foreach ( $products as $key => $product ) {
|
||||
if ( is_a( $product, SellerStatusProduct::class ) ) {
|
||||
continue;
|
||||
}
|
||||
unset( $products[ $key ] );
|
||||
}
|
||||
$this->products = $products;
|
||||
foreach ( $capabilities as $key => $capability ) {
|
||||
if ( is_a( $capability, SellerStatusCapability::class ) ) {
|
||||
continue;
|
||||
}
|
||||
unset( $capabilities[ $key ] );
|
||||
}
|
||||
|
||||
$this->products = $products;
|
||||
$this->capabilities = $capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -45,6 +63,15 @@ class SellerStatus {
|
|||
return $this->products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the capabilities.
|
||||
*
|
||||
* @return SellerStatusCapability[]
|
||||
*/
|
||||
public function capabilities() : array {
|
||||
return $this->capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the enitity as array.
|
||||
*
|
||||
|
@ -58,8 +85,16 @@ class SellerStatus {
|
|||
$this->products()
|
||||
);
|
||||
|
||||
$capabilities = array_map(
|
||||
function( SellerStatusCapability $capability ) : array {
|
||||
return $capability->to_array();
|
||||
},
|
||||
$this->capabilities()
|
||||
);
|
||||
|
||||
return array(
|
||||
'products' => $products,
|
||||
'products' => $products,
|
||||
'capabilities' => $capabilities,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,77 @@
|
|||
<?php
|
||||
/**
|
||||
* The capabilities of a seller status.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
/**
|
||||
* Class SellerStatusCapability
|
||||
*/
|
||||
class SellerStatusCapability {
|
||||
|
||||
const STATUS_ACTIVE = 'ACTIVE';
|
||||
|
||||
/**
|
||||
* The name of the product.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* The status of the capability.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $status;
|
||||
|
||||
/**
|
||||
* SellerStatusCapability constructor.
|
||||
*
|
||||
* @param string $name The name of the product.
|
||||
* @param string $status The status of the capability.
|
||||
*/
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $status
|
||||
) {
|
||||
$this->name = $name;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the product.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function name() : string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the status for this capability.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function status() : string {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array() : array {
|
||||
return array(
|
||||
'name' => $this->name(),
|
||||
'status' => $this->status(),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
|
@ -14,7 +14,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Entity\AmountBreakdown;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Item;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Money;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\Subscription\FreeTrialHandlerTrait;
|
||||
use WooCommerce\PayPalCommerce\WcSubscriptions\FreeTrialHandlerTrait;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CardButtonGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\CreditCardGateway;
|
||||
use WooCommerce\PayPalCommerce\WcGateway\Gateway\PayPalGateway;
|
||||
|
|
|
@ -19,6 +19,22 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
|||
*/
|
||||
class AuthorizationFactory {
|
||||
|
||||
/**
|
||||
* The FraudProcessorResponseFactory factory.
|
||||
*
|
||||
* @var FraudProcessorResponseFactory
|
||||
*/
|
||||
protected $fraud_processor_response_factory;
|
||||
|
||||
/**
|
||||
* AuthorizationFactory constructor.
|
||||
*
|
||||
* @param FraudProcessorResponseFactory $fraud_processor_response_factory The FraudProcessorResponseFactory factory.
|
||||
*/
|
||||
public function __construct( FraudProcessorResponseFactory $fraud_processor_response_factory ) {
|
||||
$this->fraud_processor_response_factory = $fraud_processor_response_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an Authorization based off a PayPal response.
|
||||
*
|
||||
|
@ -42,12 +58,17 @@ class AuthorizationFactory {
|
|||
|
||||
$reason = $data->status_details->reason ?? null;
|
||||
|
||||
$fraud_processor_response = isset( $data->processor_response ) ?
|
||||
$this->fraud_processor_response_factory->from_paypal_response( $data->processor_response )
|
||||
: null;
|
||||
|
||||
return new Authorization(
|
||||
$data->id,
|
||||
new AuthorizationStatus(
|
||||
$data->status,
|
||||
$reason ? new AuthorizationStatusDetails( $reason ) : null
|
||||
)
|
||||
),
|
||||
$fraud_processor_response
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,33 @@
|
|||
<?php
|
||||
/**
|
||||
* The card authentication result factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult;
|
||||
|
||||
/**
|
||||
* Class CardAuthenticationResultFactory
|
||||
*/
|
||||
class CardAuthenticationResultFactory {
|
||||
|
||||
/**
|
||||
* Returns a card authentication result from the given response object.
|
||||
*
|
||||
* @param stdClass $authentication_result The authentication result object.
|
||||
* @return CardAuthenticationResult
|
||||
*/
|
||||
public function from_paypal_response( stdClass $authentication_result ): CardAuthenticationResult {
|
||||
return new CardAuthenticationResult(
|
||||
$authentication_result->liability_shift ?? '',
|
||||
$authentication_result->three_d_secure->enrollment_status ?? '',
|
||||
$authentication_result->three_d_secure->authentication_status ?? ''
|
||||
);
|
||||
}
|
||||
}
|
|
@ -25,8 +25,8 @@ class FraudProcessorResponseFactory {
|
|||
* @return FraudProcessorResponse
|
||||
*/
|
||||
public function from_paypal_response( stdClass $data ): FraudProcessorResponse {
|
||||
$avs_code = $data->avs_code ?: null;
|
||||
$cvv_code = $data->cvv_code ?: null;
|
||||
$avs_code = ( $data->avs_code ?? null ) ?: null;
|
||||
$cvv_code = ( $data->cvv_code ?? null ) ?: null;
|
||||
|
||||
return new FraudProcessorResponse( $avs_code, $cvv_code );
|
||||
}
|
||||
|
|
|
@ -61,12 +61,12 @@ class ItemFactory {
|
|||
|
||||
$price = (float) $item['line_subtotal'] / (float) $item['quantity'];
|
||||
return new Item(
|
||||
mb_substr( $product->get_name(), 0, 127 ),
|
||||
$this->prepare_item_string( $product->get_name() ),
|
||||
new Money( $price, $this->currency ),
|
||||
$quantity,
|
||||
$this->prepare_description( $product->get_description() ),
|
||||
$this->prepare_item_string( $product->get_description() ),
|
||||
null,
|
||||
$product->get_sku(),
|
||||
$this->prepare_sku( $product->get_sku() ),
|
||||
( $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
|
||||
$product->get_permalink(),
|
||||
$image[0] ?? '',
|
||||
|
@ -138,12 +138,12 @@ class ItemFactory {
|
|||
$image = $product instanceof WC_Product ? wp_get_attachment_image_src( (int) $product->get_image_id(), 'full' ) : '';
|
||||
|
||||
return new Item(
|
||||
mb_substr( $item->get_name(), 0, 127 ),
|
||||
$this->prepare_item_string( $item->get_name() ),
|
||||
new Money( $price_without_tax_rounded, $currency ),
|
||||
$quantity,
|
||||
$product instanceof WC_Product ? $this->prepare_description( $product->get_description() ) : '',
|
||||
$product instanceof WC_Product ? $this->prepare_item_string( $product->get_description() ) : '',
|
||||
null,
|
||||
$product instanceof WC_Product ? $product->get_sku() : '',
|
||||
$product instanceof WC_Product ? $this->prepare_sku( $product->get_sku() ) : '',
|
||||
( $product instanceof WC_Product && $product->is_virtual() ) ? Item::DIGITAL_GOODS : Item::PHYSICAL_GOODS,
|
||||
$product instanceof WC_Product ? $product->get_permalink() : '',
|
||||
$image[0] ?? ''
|
||||
|
@ -160,7 +160,7 @@ class ItemFactory {
|
|||
*/
|
||||
private function from_wc_order_fee( \WC_Order_Item_Fee $item, \WC_Order $order ): Item {
|
||||
return new Item(
|
||||
$item->get_name(),
|
||||
$this->prepare_item_string( $item->get_name() ),
|
||||
new Money( (float) $item->get_amount(), $order->get_currency() ),
|
||||
$item->get_quantity(),
|
||||
'',
|
||||
|
|
|
@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
|||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Order;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\OrderStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PurchaseUnit;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Repository\ApplicationContextRepository;
|
||||
|
@ -48,13 +49,6 @@ class OrderFactory {
|
|||
*/
|
||||
private $application_context_factory;
|
||||
|
||||
/**
|
||||
* The PaymentSource factory.
|
||||
*
|
||||
* @var PaymentSourceFactory
|
||||
*/
|
||||
private $payment_source_factory;
|
||||
|
||||
/**
|
||||
* OrderFactory constructor.
|
||||
*
|
||||
|
@ -62,21 +56,18 @@ class OrderFactory {
|
|||
* @param PayerFactory $payer_factory The Payer factory.
|
||||
* @param ApplicationContextRepository $application_context_repository The Application Context repository.
|
||||
* @param ApplicationContextFactory $application_context_factory The Application Context factory.
|
||||
* @param PaymentSourceFactory $payment_source_factory The Payment Source factory.
|
||||
*/
|
||||
public function __construct(
|
||||
PurchaseUnitFactory $purchase_unit_factory,
|
||||
PayerFactory $payer_factory,
|
||||
ApplicationContextRepository $application_context_repository,
|
||||
ApplicationContextFactory $application_context_factory,
|
||||
PaymentSourceFactory $payment_source_factory
|
||||
ApplicationContextFactory $application_context_factory
|
||||
) {
|
||||
|
||||
$this->purchase_unit_factory = $purchase_unit_factory;
|
||||
$this->payer_factory = $payer_factory;
|
||||
$this->application_context_repository = $application_context_repository;
|
||||
$this->application_context_factory = $application_context_factory;
|
||||
$this->payment_source_factory = $payment_source_factory;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -152,9 +143,23 @@ class OrderFactory {
|
|||
$application_context = ( isset( $order_data->application_context ) ) ?
|
||||
$this->application_context_factory->from_paypal_response( $order_data->application_context )
|
||||
: null;
|
||||
$payment_source = ( isset( $order_data->payment_source ) ) ?
|
||||
$this->payment_source_factory->from_paypal_response( $order_data->payment_source ) :
|
||||
null;
|
||||
|
||||
$payment_source = null;
|
||||
if ( isset( $order_data->payment_source ) ) {
|
||||
$json_encoded_payment_source = wp_json_encode( $order_data->payment_source );
|
||||
if ( $json_encoded_payment_source ) {
|
||||
$payment_source_as_array = json_decode( $json_encoded_payment_source, true );
|
||||
if ( $payment_source_as_array ) {
|
||||
$name = array_key_first( $payment_source_as_array );
|
||||
if ( $name ) {
|
||||
$payment_source = new PaymentSource(
|
||||
$name,
|
||||
$order_data->payment_source->$name
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return new Order(
|
||||
$order_data->id,
|
||||
|
|
|
@ -1,53 +0,0 @@
|
|||
<?php
|
||||
/**
|
||||
* The PaymentSource factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\CardAuthenticationResult;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSource;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentSourceCard;
|
||||
|
||||
/**
|
||||
* Class PaymentSourceFactory
|
||||
*/
|
||||
class PaymentSourceFactory {
|
||||
|
||||
/**
|
||||
* Returns a PaymentSource for a PayPal Response.
|
||||
*
|
||||
* @param \stdClass $data The JSON object.
|
||||
*
|
||||
* @return PaymentSource
|
||||
*/
|
||||
public function from_paypal_response( \stdClass $data ): PaymentSource {
|
||||
|
||||
$card = null;
|
||||
$wallet = null;
|
||||
if ( isset( $data->card ) ) {
|
||||
$authentication_result = null;
|
||||
if ( isset( $data->card->authentication_result ) ) {
|
||||
$authentication_result = new CardAuthenticationResult(
|
||||
isset( $data->card->authentication_result->liability_shift ) ?
|
||||
(string) $data->card->authentication_result->liability_shift : '',
|
||||
isset( $data->card->authentication_result->three_d_secure->enrollment_status ) ?
|
||||
(string) $data->card->authentication_result->three_d_secure->enrollment_status : '',
|
||||
isset( $data->card->authentication_result->three_d_secure->authentication_status ) ?
|
||||
(string) $data->card->authentication_result->three_d_secure->authentication_status : ''
|
||||
);
|
||||
}
|
||||
$card = new PaymentSourceCard(
|
||||
isset( $data->card->last_digits ) ? (string) $data->card->last_digits : '',
|
||||
isset( $data->card->brand ) ? (string) $data->card->brand : '',
|
||||
isset( $data->card->type ) ? (string) $data->card->type : '',
|
||||
$authentication_result
|
||||
);
|
||||
}
|
||||
return new PaymentSource( $card, $wallet );
|
||||
}
|
||||
}
|
|
@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
|||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusCapability;
|
||||
|
||||
/**
|
||||
* Class SellerStatusFactory
|
||||
|
@ -37,6 +38,17 @@ class SellerStatusFactory {
|
|||
isset( $json->products ) ? (array) $json->products : array()
|
||||
);
|
||||
|
||||
return new SellerStatus( $products );
|
||||
$capabilities = array_map(
|
||||
function( $json ) : SellerStatusCapability {
|
||||
$capability = new SellerStatusCapability(
|
||||
isset( $json->name ) ? (string) $json->name : '',
|
||||
isset( $json->status ) ? (string) $json->status : ''
|
||||
);
|
||||
return $capability;
|
||||
},
|
||||
isset( $json->capabilities ) ? (array) $json->capabilities : array()
|
||||
);
|
||||
|
||||
return new SellerStatus( $products, $capabilities );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -12,13 +12,23 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Helper;
|
|||
trait ItemTrait {
|
||||
|
||||
/**
|
||||
* Cleanups the description and prepares it for sending to PayPal.
|
||||
* Cleans up item strings (title and description for example) and prepares them for sending to PayPal.
|
||||
*
|
||||
* @param string $description Item description.
|
||||
* @param string $string Item string.
|
||||
* @return string
|
||||
*/
|
||||
protected function prepare_description( string $description ): string {
|
||||
$description = strip_shortcodes( wp_strip_all_tags( $description ) );
|
||||
return substr( $description, 0, 127 ) ?: '';
|
||||
protected function prepare_item_string( string $string ): string {
|
||||
$string = strip_shortcodes( wp_strip_all_tags( $string ) );
|
||||
return substr( $string, 0, 127 ) ?: '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Prepares the sku for sending to PayPal.
|
||||
*
|
||||
* @param string $sku Item sku.
|
||||
* @return string
|
||||
*/
|
||||
protected function prepare_sku( string $sku ): string {
|
||||
return substr( wp_strip_all_tags( $sku ), 0, 127 ) ?: '';
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,7 +54,7 @@ class ApplicationContextRepository {
|
|||
$payment_preference = $this->settings->has( 'payee_preferred' ) && $this->settings->get( 'payee_preferred' ) ?
|
||||
ApplicationContext::PAYMENT_METHOD_IMMEDIATE_PAYMENT_REQUIRED : ApplicationContext::PAYMENT_METHOD_UNRESTRICTED;
|
||||
$context = new ApplicationContext(
|
||||
network_home_url( \WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ),
|
||||
home_url( \WC_AJAX::get_endpoint( ReturnUrlEndpoint::ENDPOINT ) ),
|
||||
(string) wc_get_checkout_url(),
|
||||
(string) $brand_name,
|
||||
$locale,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue