mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-07 19:54:15 +08:00
Add update product handler
This commit is contained in:
parent
01ab5d6003
commit
50d12479b8
4 changed files with 105 additions and 7 deletions
|
@ -115,4 +115,60 @@ class CatalogProducts {
|
|||
|
||||
return $this->product_factory->from_paypal_response($json);
|
||||
}
|
||||
|
||||
public function update(string $id, array $data) {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v1/catalogs/products/' . $id;
|
||||
$args = array(
|
||||
'method' => 'PATCH',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation'
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
|
||||
throw new RuntimeException( 'Not able to update product.' );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 204 !== $status_code ) {
|
||||
throw new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function product(string $id): Product {
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v1/catalogs/products/' . $id;
|
||||
$args = array(
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation'
|
||||
),
|
||||
);
|
||||
|
||||
$response = $this->request( $url, $args );
|
||||
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
|
||||
throw new RuntimeException( 'Not able to get product.' );
|
||||
}
|
||||
|
||||
$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 $this->product_factory->from_paypal_response($json);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -46,6 +46,7 @@ return array(
|
|||
'subscription.api-handler' => static function(ContainerInterface $container): SubscriptionsApiHandler {
|
||||
return new SubscriptionsApiHandler(
|
||||
$container->get('api.endpoint.catalog-products'),
|
||||
$container->get('api.factory.product'),
|
||||
$container->get('api.endpoint.billing-plans'),
|
||||
$container->get('api.factory.billing-cycle'),
|
||||
$container->get('api.factory.payment-preferences'),
|
||||
|
|
|
@ -164,7 +164,7 @@ class SubscriptionModule implements ModuleInterface {
|
|||
assert($subscriptions_api_handler instanceof SubscriptionsApiHandler);
|
||||
|
||||
if ( $product->meta_exists( 'ppcp_subscription_product' ) && $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
$subscriptions_api_handler->update_product();
|
||||
$subscriptions_api_handler->update_product($product);
|
||||
$subscriptions_api_handler->update_plan();
|
||||
return;
|
||||
}
|
||||
|
@ -173,7 +173,7 @@ class SubscriptionModule implements ModuleInterface {
|
|||
$subscriptions_api_handler->create_product($product);
|
||||
}
|
||||
|
||||
if ( $product->get_meta( 'ppcp_subscription_product' ) && ! $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
if ( $product->meta_exists( 'ppcp_subscription_product' ) && ! $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||
$subscriptions_api_handler->create_plan($product);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -10,6 +10,7 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\BillingCycleFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\PaymentPreferencesFactory;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Factory\ProductFactory;
|
||||
|
||||
class SubscriptionsApiHandler {
|
||||
|
||||
|
@ -18,6 +19,11 @@ class SubscriptionsApiHandler {
|
|||
*/
|
||||
private $products_endpoint;
|
||||
|
||||
/**
|
||||
* @var ProductFactory
|
||||
*/
|
||||
private $product_factory;
|
||||
|
||||
/**
|
||||
* @var BillingPlans
|
||||
*/
|
||||
|
@ -38,15 +44,16 @@ class SubscriptionsApiHandler {
|
|||
*/
|
||||
private $logger;
|
||||
|
||||
|
||||
public function __construct(
|
||||
CatalogProducts $products_endpoint,
|
||||
ProductFactory $product_factory,
|
||||
BillingPlans $billing_plans_endpoint,
|
||||
BillingCycleFactory $billing_cycle_factory,
|
||||
PaymentPreferencesFactory $payment_preferences_factory,
|
||||
LoggerInterface $logger
|
||||
) {
|
||||
$this->products_endpoint = $products_endpoint;
|
||||
$this->product_factory = $product_factory;
|
||||
$this->billing_plans_endpoint = $billing_plans_endpoint;
|
||||
$this->billing_cycle_factory = $billing_cycle_factory;
|
||||
$this->payment_preferences_factory = $payment_preferences_factory;
|
||||
|
@ -70,7 +77,7 @@ class SubscriptionsApiHandler {
|
|||
$error = $exception->get_details( $error );
|
||||
}
|
||||
|
||||
$this->logger->error( 'Could not create subscription product on PayPal. ' . $error );
|
||||
$this->logger->error( 'Could not create catalog product on PayPal. ' . $error );
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,8 +101,42 @@ class SubscriptionsApiHandler {
|
|||
}
|
||||
}
|
||||
|
||||
public function update_product() {
|
||||
public function update_product(WC_Product $product) {
|
||||
try {
|
||||
$catalog_product_id = $product->get_meta( 'ppcp_subscription_product' )['id'] ?? '';
|
||||
if($catalog_product_id) {
|
||||
$catalog_product = $this->products_endpoint->product($catalog_product_id);
|
||||
$catalog_product_name = $catalog_product->name() ?? '';
|
||||
$catalog_product_description = $catalog_product->description() ?? '';
|
||||
if($catalog_product_name !== $product->get_title() || $catalog_product_description !== $product->get_description()) {
|
||||
$data = array();
|
||||
if($catalog_product_name !== $product->get_title()) {
|
||||
$data[] = (object) array(
|
||||
'op' => 'replace',
|
||||
'path' => '/name',
|
||||
'value' => $catalog_product_name,
|
||||
);
|
||||
}
|
||||
if($catalog_product_description !== $product->get_description()) {
|
||||
$data[] = (object) array(
|
||||
'op' => 'replace',
|
||||
'path' => '/description',
|
||||
'value' => $catalog_product_description,
|
||||
);
|
||||
}
|
||||
|
||||
$this->products_endpoint->update($catalog_product_id, $data);
|
||||
}
|
||||
}
|
||||
|
||||
} catch (RuntimeException $exception) {
|
||||
$error = $exception->getMessage();
|
||||
if ( is_a( $exception, PayPalApiException::class ) ) {
|
||||
$error = $exception->get_details( $error );
|
||||
}
|
||||
|
||||
$this->logger->error( 'Could not update catalog product on PayPal. ' . $error );
|
||||
}
|
||||
}
|
||||
|
||||
public function update_plan() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue