mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
call partners endpoint when rendering the dcc settings to check whether the pp account is able to process dcc.
In order to do so, it was also necessary to store the merchant id during onboarding. includes a bugfix where the cache invalidation did not work because the cache object was constructed with the wrong prefix key.
This commit is contained in:
parent
22c7e3975f
commit
c09e314baa
14 changed files with 638 additions and 29 deletions
|
@ -9,13 +9,13 @@ declare(strict_types=1);
|
|||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient;
|
||||
|
||||
use Dhii\Data\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\IdentityToken;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\LoginSeller;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\OrderEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnerReferrals;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PartnersEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentsEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\PaymentTokenEndpoint;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\WebhookEndpoint;
|
||||
|
@ -32,6 +32,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentsFactory;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentSourceFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentTokenFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PurchaseUnitFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\SellerStatusFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ShippingFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\WebhookFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Helper\Cache;
|
||||
|
@ -85,6 +86,19 @@ return array(
|
|||
$logger
|
||||
);
|
||||
},
|
||||
'api.endpoint.partners' => static function ( $container ) : PartnersEndpoint {
|
||||
return new PartnersEndpoint(
|
||||
$container->get( 'api.host' ),
|
||||
$container->get( 'api.bearer' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' ),
|
||||
$container->get( 'api.factory.sellerstatus' ),
|
||||
$container->get( 'api.partner_merchant_id' ),
|
||||
$container->get( 'api.merchant_id' )
|
||||
);
|
||||
},
|
||||
'api.factory.sellerstatus' => static function ( $container ) : SellerStatusFactory {
|
||||
return new SellerStatusFactory();
|
||||
},
|
||||
'api.endpoint.payment-token' => static function ( $container ) : PaymentTokenEndpoint {
|
||||
return new PaymentTokenEndpoint(
|
||||
$container->get( 'api.host' ),
|
||||
|
|
149
modules/ppcp-api-client/src/Endpoint/class-partnersendpoint.php
Normal file
149
modules/ppcp-api-client/src/Endpoint/class-partnersendpoint.php
Normal file
|
@ -0,0 +1,149 @@
|
|||
<?php
|
||||
/**
|
||||
* The Partners 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\SellerStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\SellerStatusFactory;
|
||||
|
||||
/**
|
||||
* Class PartnersEndpoint
|
||||
*/
|
||||
class PartnersEndpoint {
|
||||
|
||||
use RequestTrait;
|
||||
|
||||
/**
|
||||
* The Host URL.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $host;
|
||||
|
||||
/**
|
||||
* The bearer.
|
||||
*
|
||||
* @var Bearer
|
||||
*/
|
||||
private $bearer;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
*
|
||||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
|
||||
/**
|
||||
* The seller status factory.
|
||||
*
|
||||
* @var SellerStatusFactory
|
||||
*/
|
||||
private $seller_status_factory;
|
||||
|
||||
/**
|
||||
* The partner ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $partner_id;
|
||||
|
||||
/**
|
||||
* The merchant ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $merchant_id;
|
||||
|
||||
/**
|
||||
* PartnersEndpoint constructor.
|
||||
*
|
||||
* @param string $host The host.
|
||||
* @param Bearer $bearer The bearer.
|
||||
* @param LoggerInterface $logger The logger.
|
||||
* @param SellerStatusFactory $seller_status_factory The seller status factory.
|
||||
* @param string $partner_id The partner ID.
|
||||
* @param string $merchant_id The merchant ID.
|
||||
*/
|
||||
public function __construct(
|
||||
string $host,
|
||||
Bearer $bearer,
|
||||
LoggerInterface $logger,
|
||||
SellerStatusFactory $seller_status_factory,
|
||||
string $partner_id,
|
||||
string $merchant_id
|
||||
) {
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->logger = $logger;
|
||||
$this->seller_status_factory = $seller_status_factory;
|
||||
$this->partner_id = $partner_id;
|
||||
$this->merchant_id = $merchant_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current seller status.
|
||||
*
|
||||
* @return SellerStatus
|
||||
* @throws RuntimeException When request could not be fullfilled.
|
||||
*/
|
||||
public function seller_status() : SellerStatus {
|
||||
$url = trailingslashit( $this->host ) . 'v1/customer/partners/' . $this->partner_id . '/merchant-integrations/' . $this->merchant_id;
|
||||
$bearer = $this->bearer->bearer();
|
||||
$args = array(
|
||||
'method' => 'GET',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
);
|
||||
$response = $this->request( $url, $args );
|
||||
if ( is_wp_error( $response ) ) {
|
||||
|
||||
$error = new RuntimeException(
|
||||
__(
|
||||
'Could not fetch sellers status.',
|
||||
'paypal-payments-for-woocommerce'
|
||||
)
|
||||
);
|
||||
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
$error->getMessage(),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
}
|
||||
|
||||
$json = json_decode( wp_remote_retrieve_body( $response ) );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 200 !== $status_code ) {
|
||||
$error = new PayPalApiException( $json, $status_code );
|
||||
$this->logger->log(
|
||||
'warning',
|
||||
$error->getMessage(),
|
||||
array(
|
||||
'args' => $args,
|
||||
'response' => $response,
|
||||
)
|
||||
);
|
||||
throw $error;
|
||||
}
|
||||
|
||||
$status = $this->seller_status_factory->from_paypal_reponse( $json );
|
||||
return $status;
|
||||
}
|
||||
}
|
65
modules/ppcp-api-client/src/Entity/class-sellerstatus.php
Normal file
65
modules/ppcp-api-client/src/Entity/class-sellerstatus.php
Normal file
|
@ -0,0 +1,65 @@
|
|||
<?php
|
||||
/**
|
||||
* The seller status entity.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
/**
|
||||
* Class SellerStatus
|
||||
*/
|
||||
class SellerStatus {
|
||||
|
||||
/**
|
||||
* The products.
|
||||
*
|
||||
* @var SellerStatusProduct[]
|
||||
*/
|
||||
private $products;
|
||||
|
||||
/**
|
||||
* SellerStatus constructor.
|
||||
*
|
||||
* @param SellerStatusProduct[] $products The products.
|
||||
*/
|
||||
public function __construct( array $products ) {
|
||||
foreach ( $products as $key => $product ) {
|
||||
if ( is_a( $product, SellerStatusProduct::class ) ) {
|
||||
continue;
|
||||
}
|
||||
unset( $products[ $key ] );
|
||||
}
|
||||
$this->products = $products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the products.
|
||||
*
|
||||
* @return SellerStatusProduct[]
|
||||
*/
|
||||
public function products() : array {
|
||||
return $this->products;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the enitity as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array() : array {
|
||||
$products = array_map(
|
||||
function( SellerStatusProduct $product ) : array {
|
||||
return $product->to_array();
|
||||
},
|
||||
$this->products()
|
||||
);
|
||||
|
||||
return array(
|
||||
'products' => $products,
|
||||
);
|
||||
}
|
||||
}
|
108
modules/ppcp-api-client/src/Entity/class-sellerstatusproduct.php
Normal file
108
modules/ppcp-api-client/src/Entity/class-sellerstatusproduct.php
Normal file
|
@ -0,0 +1,108 @@
|
|||
<?php
|
||||
/**
|
||||
* The products of a seller status.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
/**
|
||||
* Class SellerStatusProduct
|
||||
*/
|
||||
class SellerStatusProduct {
|
||||
|
||||
const VETTING_STATUS_APPROVED = 'APPROVED';
|
||||
const VETTING_STATUS_PENDING = 'PENDING';
|
||||
const VETTING_STATUS_DECLINED = 'DECLINED';
|
||||
const VETTING_STATUS_SUBSCRIBED = 'SUBSCRIBED';
|
||||
const VETTING_STATUS_IN_REVIEW = 'IN_REVIEW';
|
||||
const VETTING_STATUS_DENIED = 'DENIED';
|
||||
/**
|
||||
* The name of the product.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* The vetting status of the product.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $vetting_status;
|
||||
|
||||
/**
|
||||
* The capabilities of the product.
|
||||
*
|
||||
* @var string[]
|
||||
*/
|
||||
private $capabilities;
|
||||
|
||||
/**
|
||||
* SellerStatusProduct constructor.
|
||||
*
|
||||
* @param string $name The name of the product.
|
||||
* @param string $vetting_status The vetting status of the product.
|
||||
* @param string[] $capabilities The capabilities of the product.
|
||||
*/
|
||||
public function __construct(
|
||||
string $name,
|
||||
string $vetting_status,
|
||||
array $capabilities
|
||||
) {
|
||||
foreach ( $capabilities as $key => $capability ) {
|
||||
if ( is_string( $capability ) ) {
|
||||
continue;
|
||||
}
|
||||
unset( $capabilities[ $key ] );
|
||||
}
|
||||
$this->name = $name;
|
||||
$this->vetting_status = $vetting_status;
|
||||
$this->capabilities = $capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of the product.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function name() : string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the vetting status for this product.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function vetting_status() : string {
|
||||
return $this->vetting_status;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the capabilities of this product.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function capabilities() : array {
|
||||
return $this->capabilities;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the entity as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array() : array {
|
||||
return array(
|
||||
'name' => $this->name(),
|
||||
'vetting_status' => $this->vetting_status(),
|
||||
'capabilities' => $this->capabilities(),
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
<?php
|
||||
/**
|
||||
* Factory for the SellerStatus object.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare( strict_types=1 );
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatus;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\SellerStatusProduct;
|
||||
|
||||
/**
|
||||
* Class SellerStatusFactory
|
||||
*/
|
||||
class SellerStatusFactory {
|
||||
|
||||
/**
|
||||
* Creates a SellerStatus Object out of a PayPal response.
|
||||
*
|
||||
* @param \stdClass $json The response object.
|
||||
*
|
||||
* @return SellerStatus
|
||||
*/
|
||||
public function from_paypal_reponse( \stdClass $json ) : SellerStatus {
|
||||
$products = array_map(
|
||||
function( $json ) : SellerStatusProduct {
|
||||
$product = new SellerStatusProduct(
|
||||
isset( $json->name ) ? (string) $json->name : '',
|
||||
isset( $json->vetting_status ) ? (string) $json->vetting_status : '',
|
||||
isset( $json->capabilities ) ? (array) $json->capabilities : array()
|
||||
);
|
||||
return $product;
|
||||
},
|
||||
isset( $json->products ) ? (array) $json->products : array()
|
||||
);
|
||||
|
||||
return new SellerStatus( $products );
|
||||
}
|
||||
}
|
|
@ -49,7 +49,8 @@ class Cache {
|
|||
* @return bool
|
||||
*/
|
||||
public function has( string $key ): bool {
|
||||
return $this->get( $key ) !== false;
|
||||
$value = $this->get( $key );
|
||||
return false !== $value;
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue