mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Refactor (WIP)
This commit is contained in:
parent
baee53e90f
commit
999231fd01
14 changed files with 759 additions and 102 deletions
|
@ -12,6 +12,10 @@ namespace WooCommerce\PayPalCommerce\ApiClient;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingSubscriptions;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingPlans;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\BillingCycleFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentPreferencesFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PlanFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ProductFactory;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
|
||||
|
@ -213,6 +217,7 @@ return array(
|
|||
return new CatalogProducts(
|
||||
$container->get( 'api.host' ),
|
||||
$container->get( 'api.bearer' ),
|
||||
$container->get('api.factory.product'),
|
||||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
},
|
||||
|
@ -220,6 +225,8 @@ return array(
|
|||
return new BillingPlans(
|
||||
$container->get( 'api.host' ),
|
||||
$container->get( 'api.bearer' ),
|
||||
$container->get('api.factory.billing-cycle'),
|
||||
$container->get('api.factory.plan'),
|
||||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
},
|
||||
|
@ -378,6 +385,21 @@ return array(
|
|||
'api.factory.fraud-processor-response' => static function ( ContainerInterface $container ): FraudProcessorResponseFactory {
|
||||
return new FraudProcessorResponseFactory();
|
||||
},
|
||||
'api.factory.product' => static function(ContainerInterface $container): ProductFactory {
|
||||
return new ProductFactory();
|
||||
},
|
||||
'api.factory.billing-cycle' => static function(ContainerInterface $container): BillingCycleFactory {
|
||||
return new BillingCycleFactory();
|
||||
},
|
||||
'api.factory.payment-preferences' => static function(ContainerInterface $container):PaymentPreferencesFactory {
|
||||
return new PaymentPreferencesFactory();
|
||||
},
|
||||
'api.factory.plan' => static function(ContainerInterface $container): PlanFactory {
|
||||
return new PlanFactory(
|
||||
$container->get('api.factory.billing-cycle'),
|
||||
$container->get('api.factory.payment-preferences')
|
||||
);
|
||||
},
|
||||
'api.helpers.dccapplies' => static function ( ContainerInterface $container ) : DccApplies {
|
||||
return new DccApplies(
|
||||
$container->get( 'api.dcc-supported-country-currency-matrix' ),
|
||||
|
|
|
@ -10,10 +10,12 @@ 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\Plan;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\BillingCycleFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PlanFactory;
|
||||
|
||||
/**
|
||||
* Class BillingPlans
|
||||
|
@ -36,6 +38,16 @@ class BillingPlans {
|
|||
*/
|
||||
private $bearer;
|
||||
|
||||
/**
|
||||
* @var BillingCycleFactory
|
||||
*/
|
||||
private $billing_cycle_factory;
|
||||
|
||||
/**
|
||||
* @var PlanFactory
|
||||
*/
|
||||
private $plan_factory;
|
||||
|
||||
/**
|
||||
* The logger.
|
||||
*
|
||||
|
@ -53,10 +65,14 @@ class BillingPlans {
|
|||
public function __construct(
|
||||
string $host,
|
||||
Bearer $bearer,
|
||||
BillingCycleFactory $billing_cycle_factory,
|
||||
PlanFactory $plan_factory,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->billing_cycle_factory = $billing_cycle_factory;
|
||||
$this->plan_factory = $plan_factory;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
|
@ -65,7 +81,7 @@ class BillingPlans {
|
|||
*
|
||||
* @param string $product_id The product id.
|
||||
*
|
||||
* @return stdClass
|
||||
* @return Plan
|
||||
*
|
||||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
|
@ -74,12 +90,12 @@ class BillingPlans {
|
|||
string $product_id,
|
||||
array $billing_cycles,
|
||||
array $payment_preferences
|
||||
): stdClass {
|
||||
): Plan {
|
||||
|
||||
$data = array(
|
||||
'product_id' => $product_id,
|
||||
'name' => 'Testing Plan',
|
||||
'billing_cycles' => array($billing_cycles),
|
||||
'billing_cycles' => $billing_cycles,
|
||||
'payment_preferences' => $payment_preferences
|
||||
);
|
||||
|
||||
|
@ -110,7 +126,7 @@ class BillingPlans {
|
|||
);
|
||||
}
|
||||
|
||||
return $json;
|
||||
return $this->plan_factory->from_paypal_response($json);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -10,10 +10,11 @@ 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\Product;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ProductFactory;
|
||||
|
||||
/**
|
||||
* Class CatalogProduct
|
||||
|
@ -41,6 +42,10 @@ class CatalogProducts {
|
|||
* @var LoggerInterface
|
||||
*/
|
||||
private $logger;
|
||||
/**
|
||||
* @var ProductFactory
|
||||
*/
|
||||
private $product_factory;
|
||||
|
||||
/**
|
||||
* CatalogProducts constructor.
|
||||
|
@ -52,26 +57,35 @@ class CatalogProducts {
|
|||
public function __construct(
|
||||
string $host,
|
||||
Bearer $bearer,
|
||||
ProductFactory $product_factory,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->host = $host;
|
||||
$this->bearer = $bearer;
|
||||
$this->product_factory = $product_factory;
|
||||
$this->logger = $logger;
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a product.
|
||||
*
|
||||
* @return stdClass
|
||||
* @param string $name Product name.
|
||||
* @param string $description Product description.
|
||||
*
|
||||
* @return Product
|
||||
*
|
||||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
*/
|
||||
public function create(string $name): stdClass {
|
||||
public function create(string $name, string $description): Product {
|
||||
$data = array(
|
||||
'name' => $name,
|
||||
);
|
||||
|
||||
if($description) {
|
||||
$data['description'] = $description;
|
||||
}
|
||||
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v1/catalogs/products';
|
||||
$args = array(
|
||||
|
@ -99,6 +113,6 @@ class CatalogProducts {
|
|||
);
|
||||
}
|
||||
|
||||
return $json;
|
||||
return $this->product_factory->from_paypal_response($json);
|
||||
}
|
||||
}
|
||||
|
|
97
modules/ppcp-api-client/src/Entity/BillingCycle.php
Normal file
97
modules/ppcp-api-client/src/Entity/BillingCycle.php
Normal file
|
@ -0,0 +1,97 @@
|
|||
<?php
|
||||
/**
|
||||
* The Billing Cycle object.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
class BillingCycle {
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $frequency;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $sequence;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $tenure_type;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $pricing_scheme;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $total_cycles;
|
||||
|
||||
public function __construct(
|
||||
array $frequency,
|
||||
int $sequence,
|
||||
string $tenure_type,
|
||||
array $pricing_scheme = array(),
|
||||
int $total_cycles = 1
|
||||
) {
|
||||
$this->frequency = $frequency;
|
||||
$this->sequence = $sequence;
|
||||
$this->tenure_type = $tenure_type;
|
||||
$this->pricing_scheme = $pricing_scheme;
|
||||
$this->total_cycles = $total_cycles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function frequency(): array {
|
||||
return $this->frequency;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function sequence(): int {
|
||||
return $this->sequence;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function tenure_type(): string {
|
||||
return $this->tenure_type;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function pricing_scheme(): array {
|
||||
return $this->pricing_scheme;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function total_cycles(): int {
|
||||
return $this->total_cycles;
|
||||
}
|
||||
|
||||
public function to_array() {
|
||||
return array(
|
||||
'frequency' => $this->frequency(),
|
||||
'sequence' => $this->sequence(),
|
||||
'tenure_type' => $this->tenure_type(),
|
||||
'pricing_scheme' => $this->pricing_scheme(),
|
||||
'total_cycles' => $this->total_cycles(),
|
||||
);
|
||||
}
|
||||
}
|
82
modules/ppcp-api-client/src/Entity/PaymentPreferences.php
Normal file
82
modules/ppcp-api-client/src/Entity/PaymentPreferences.php
Normal file
|
@ -0,0 +1,82 @@
|
|||
<?php
|
||||
/**
|
||||
* The Payment Preferences object.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
class PaymentPreferences {
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $setup_fee;
|
||||
|
||||
/**
|
||||
* @var bool
|
||||
*/
|
||||
private $auto_bill_outstanding;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $setup_fee_failure_action;
|
||||
|
||||
/**
|
||||
* @var int
|
||||
*/
|
||||
private $payment_failure_threshold;
|
||||
|
||||
public function __construct(
|
||||
array $setup_fee,
|
||||
bool $auto_bill_outstanding = true,
|
||||
string $setup_fee_failure_action = 'CONTINUE',
|
||||
int $payment_failure_threshold = 3
|
||||
) {
|
||||
|
||||
$this->setup_fee = $setup_fee;
|
||||
$this->auto_bill_outstanding = $auto_bill_outstanding;
|
||||
$this->setup_fee_failure_action = $setup_fee_failure_action;
|
||||
$this->payment_failure_threshold = $payment_failure_threshold;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function setup_fee(): array {
|
||||
return $this->setup_fee;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
*/
|
||||
public function auto_bill_outstanding(): bool {
|
||||
return $this->auto_bill_outstanding;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function setup_fee_failure_action(): string {
|
||||
return $this->setup_fee_failure_action;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return int
|
||||
*/
|
||||
public function payment_failure_threshold(): int {
|
||||
return $this->payment_failure_threshold;
|
||||
}
|
||||
|
||||
public function to_array():array {
|
||||
return array(
|
||||
'setup_fee' => $this->setup_fee(),
|
||||
'auto_bill_outstanding' => $this->auto_bill_outstanding(),
|
||||
'setup_fee_failure_action' => $this->setup_fee_failure_action(),
|
||||
'payment_failure_threshold' => $this->payment_failure_threshold(),
|
||||
);
|
||||
}
|
||||
}
|
112
modules/ppcp-api-client/src/Entity/Plan.php
Normal file
112
modules/ppcp-api-client/src/Entity/Plan.php
Normal file
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
/**
|
||||
* The Plan object.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
class Plan {
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $product_id;
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
private $billing_cycles;
|
||||
|
||||
/**
|
||||
* @var PaymentPreferences
|
||||
*/
|
||||
private $payment_preferences;
|
||||
|
||||
/**
|
||||
* @var string
|
||||
*/
|
||||
private $status;
|
||||
|
||||
public function __construct(
|
||||
string $id,
|
||||
string $name,
|
||||
string $product_id,
|
||||
array $billing_cycles,
|
||||
PaymentPreferences $payment_preferences,
|
||||
string $status = ''
|
||||
) {
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->product_id = $product_id;
|
||||
$this->billing_cycles = $billing_cycles;
|
||||
$this->payment_preferences = $payment_preferences;
|
||||
$this->status = $status;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function id(): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function name(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function product_id(): string {
|
||||
return $this->product_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array
|
||||
*/
|
||||
public function billing_cycles(): array {
|
||||
return $this->billing_cycles;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return PaymentPreferences
|
||||
*/
|
||||
public function payment_preferences(): PaymentPreferences {
|
||||
return $this->payment_preferences;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return string
|
||||
*/
|
||||
public function status(): string {
|
||||
return $this->status;
|
||||
}
|
||||
|
||||
public function to_array():array {
|
||||
return array(
|
||||
'id' => $this->id(),
|
||||
'name' => $this->name(),
|
||||
'product_id' => $this->product_id(),
|
||||
'billing_cycles' => $this->billing_cycles(),
|
||||
'payment_preferences' => $this->payment_preferences(),
|
||||
'status' => $this->status(),
|
||||
);
|
||||
}
|
||||
}
|
85
modules/ppcp-api-client/src/Entity/Product.php
Normal file
85
modules/ppcp-api-client/src/Entity/Product.php
Normal file
|
@ -0,0 +1,85 @@
|
|||
<?php
|
||||
/**
|
||||
* The Product object.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Entity
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Entity;
|
||||
|
||||
class Product {
|
||||
|
||||
/**
|
||||
* Product ID.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $id;
|
||||
|
||||
/**
|
||||
* Product name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $name;
|
||||
|
||||
/**
|
||||
* Product description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
private $description;
|
||||
|
||||
/**
|
||||
* @param string $id Product ID.
|
||||
* @param string $name Product name.
|
||||
* @param string $description Product description.
|
||||
*/
|
||||
public function __construct( string $id, string $name, string $description = '' ) {
|
||||
$this->id = $id;
|
||||
$this->name = $name;
|
||||
$this->description = $description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product ID.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function id(): string {
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function name(): string {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the product description.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function description(): string {
|
||||
return $this->description;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the object as array.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function to_array() {
|
||||
return array(
|
||||
'id' => $this->id(),
|
||||
'name' => $this->name(),
|
||||
'description' => $this->description(),
|
||||
);
|
||||
}
|
||||
}
|
53
modules/ppcp-api-client/src/Factory/BillingCycleFactory.php
Normal file
53
modules/ppcp-api-client/src/Factory/BillingCycleFactory.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
/**
|
||||
* The Billing Cycle factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WC_Product;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\BillingCycle;
|
||||
|
||||
class BillingCycleFactory {
|
||||
|
||||
public function from_wc_product(WC_Product $product): BillingCycle {
|
||||
return new BillingCycle(
|
||||
array(
|
||||
'interval_unit' => $product->get_meta('_subscription_period'),
|
||||
'interval_count' => $product->get_meta('_subscription_period_interval'),
|
||||
),
|
||||
1,
|
||||
'REGULAR',
|
||||
array(
|
||||
'fixed_price' => array(
|
||||
'value' => $product->get_meta('_subscription_price'),
|
||||
'currency_code' => 'USD',
|
||||
),
|
||||
),
|
||||
(int)$product->get_meta('_subscription_length')
|
||||
);
|
||||
}
|
||||
|
||||
public function from_paypal_response(stdClass $data): BillingCycle {
|
||||
return new BillingCycle(
|
||||
array(
|
||||
'interval_unit' => $data->frequency->interval_unit,
|
||||
'interval_count' => $data->frequency->interval_count,
|
||||
),
|
||||
$data->sequence,
|
||||
$data->tenure_type,
|
||||
array(
|
||||
'fixed_price' => array(
|
||||
'value' => $data->pricing_scheme->fixed_price->value,
|
||||
'currency_code' => $data->pricing_scheme->fixed_price->currency_code,
|
||||
),
|
||||
),
|
||||
$data->total_cycles
|
||||
);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
<?php
|
||||
/**
|
||||
* The Payment Preferences factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WC_Product;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\PaymentPreferences;
|
||||
|
||||
class PaymentPreferencesFactory {
|
||||
|
||||
public function from_wc_product(WC_Product $product):PaymentPreferences {
|
||||
return new PaymentPreferences(
|
||||
array(
|
||||
'value' => $product->get_meta( '_subscription_sign_up_fee' ) ?: '0',
|
||||
'currency_code' => 'USD',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
public function from_paypal_response(stdClass $data) {
|
||||
return new PaymentPreferences(
|
||||
array(
|
||||
'value' => $data->setup_fee->value,
|
||||
'currency_code' => $data->setup_fee->currency_code,
|
||||
),
|
||||
$data->auto_bill_outstanding,
|
||||
$data->setup_fee_failure_action,
|
||||
$data->payment_failure_threshold
|
||||
);
|
||||
}
|
||||
}
|
66
modules/ppcp-api-client/src/Factory/PlanFactory.php
Normal file
66
modules/ppcp-api-client/src/Factory/PlanFactory.php
Normal file
|
@ -0,0 +1,66 @@
|
|||
<?php
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Plan;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
class PlanFactory
|
||||
{
|
||||
/**
|
||||
* @var BillingCycleFactory
|
||||
*/
|
||||
private $billing_cycle_factory;
|
||||
/**
|
||||
* @var PaymentPreferencesFactory
|
||||
*/
|
||||
private $payment_preferences_factory;
|
||||
|
||||
public function __construct(
|
||||
BillingCycleFactory $billing_cycle_factory,
|
||||
PaymentPreferencesFactory $payment_preferences_factory
|
||||
){
|
||||
$this->billing_cycle_factory = $billing_cycle_factory;
|
||||
$this->payment_preferences_factory = $payment_preferences_factory;
|
||||
}
|
||||
|
||||
public function from_paypal_response(stdClass $data): Plan {
|
||||
if ( ! isset( $data->id ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No id for given plan', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
}
|
||||
if ( ! isset( $data->name ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No name for plan given', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
}
|
||||
if ( ! isset( $data->product_id ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No product id for given plan', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
}
|
||||
if ( ! isset( $data->billing_cycles ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No billing cycles for given plan', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
}
|
||||
|
||||
$billing_cycles = array();
|
||||
foreach ($data->billing_cycles as $billing_cycle) {
|
||||
$billing_cycles[] = $this->billing_cycle_factory->from_paypal_response($billing_cycle);
|
||||
}
|
||||
|
||||
$payment_preferences = $this->payment_preferences_factory->from_paypal_response($data->payment_preferences) ?? array();
|
||||
|
||||
return new Plan(
|
||||
$data->id,
|
||||
$data->name,
|
||||
$data->product_id,
|
||||
$billing_cycles,
|
||||
$payment_preferences,
|
||||
$data->status ?? ''
|
||||
);
|
||||
}
|
||||
}
|
44
modules/ppcp-api-client/src/Factory/ProductFactory.php
Normal file
44
modules/ppcp-api-client/src/Factory/ProductFactory.php
Normal file
|
@ -0,0 +1,44 @@
|
|||
<?php
|
||||
/**
|
||||
* The Product factory.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Factory
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace WooCommerce\PayPalCommerce\ApiClient\Factory;
|
||||
|
||||
use stdClass;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Entity\Product;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
class ProductFactory {
|
||||
|
||||
/**
|
||||
* Creates a Product based off a PayPal response.
|
||||
*
|
||||
* @param stdClass $data The JSON object.
|
||||
*
|
||||
* @return Product
|
||||
* @throws RuntimeException When JSON object is malformed.
|
||||
*/
|
||||
public function from_paypal_response(stdClass $data): Product {
|
||||
if ( ! isset( $data->id ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No id for product given', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
}
|
||||
if ( ! isset( $data->name ) ) {
|
||||
throw new RuntimeException(
|
||||
__( 'No name for product given', 'woocommerce-paypal-payments' )
|
||||
);
|
||||
}
|
||||
|
||||
return new Product(
|
||||
$data->id,
|
||||
$data->name,
|
||||
$data->description ?? ''
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue