mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-05 08:59:14 +08:00
Add update subscription (WIP)
This commit is contained in:
parent
8ceb7e0664
commit
b5e1f1530a
8 changed files with 171 additions and 44 deletions
|
@ -10,7 +10,7 @@ declare(strict_types=1);
|
|||
namespace WooCommerce\PayPalCommerce\ApiClient;
|
||||
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Subscriptions;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingPlans;
|
||||
use WooCommerce\PayPalCommerce\Vendor\Psr\Container\ContainerInterface;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
|
||||
|
@ -215,8 +215,8 @@ return array(
|
|||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
);
|
||||
},
|
||||
'api.endpoint.subscriptions' => static function( ContainerInterface $container ): Subscriptions {
|
||||
return new Subscriptions(
|
||||
'api.endpoint.billing-plans' => static function( ContainerInterface $container ): BillingPlans {
|
||||
return new BillingPlans(
|
||||
$container->get( 'api.host' ),
|
||||
$container->get( 'api.bearer' ),
|
||||
$container->get( 'woocommerce.logger.woocommerce' )
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
<?php
|
||||
/**
|
||||
* The Subscriptions endpoint.
|
||||
* The Billing Plans endpoint.
|
||||
*
|
||||
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
|
||||
*/
|
||||
|
@ -16,9 +16,9 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
|||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||
|
||||
/**
|
||||
* Class Subscriptions
|
||||
* Class BillingPlans
|
||||
*/
|
||||
class Subscriptions {
|
||||
class BillingPlans {
|
||||
|
||||
use RequestTrait;
|
||||
|
||||
|
@ -44,7 +44,7 @@ class Subscriptions {
|
|||
private $logger;
|
||||
|
||||
/**
|
||||
* Subscriptions constructor.
|
||||
* BillingPlans constructor.
|
||||
*
|
||||
* @param string $host The host.
|
||||
* @param Bearer $bearer The bearer.
|
||||
|
@ -70,7 +70,7 @@ class Subscriptions {
|
|||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
*/
|
||||
public function create_plan(
|
||||
public function create(
|
||||
string $product_id,
|
||||
array $billing_cycles,
|
||||
array $payment_preferences
|
||||
|
@ -90,6 +90,7 @@ class Subscriptions {
|
|||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation'
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
@ -111,4 +112,60 @@ class Subscriptions {
|
|||
|
||||
return $json;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates a subscription plan.
|
||||
*
|
||||
* @param string $billing_plan_id Billing plan ID.
|
||||
* @param array $billing_cycles Billing cycles.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws RuntimeException If the request fails.
|
||||
* @throws PayPalApiException If the request fails.
|
||||
*/
|
||||
public function update_pricing(string $billing_plan_id, array $billing_cycles):void {
|
||||
$data = array(
|
||||
"pricing_schemes" => array(
|
||||
(object)array(
|
||||
"billing_cycle_sequence" => 1,
|
||||
"pricing_scheme" => array(
|
||||
"fixed_price" => array(
|
||||
"value" => $billing_cycles['pricing_scheme']['fixed_price']['value'],
|
||||
"currency_code" => "USD"
|
||||
),
|
||||
"roll_out_strategy" => array(
|
||||
"effective_time" => "2022-11-01T00:00:00Z",
|
||||
"process_change_from" => "NEXT_PAYMENT"
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$bearer = $this->bearer->bearer();
|
||||
$url = trailingslashit( $this->host ) . 'v1/billing/plans/' . $billing_plan_id . '/update-pricing-schemes';
|
||||
$args = array(
|
||||
'method' => 'POST',
|
||||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
),
|
||||
'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 plan.' );
|
||||
}
|
||||
|
||||
$json = json_decode( $response['body'] );
|
||||
$status_code = (int) wp_remote_retrieve_response_code( $response );
|
||||
if ( 204 !== $status_code ) {
|
||||
throw new PayPalApiException(
|
||||
$json,
|
||||
$status_code
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -79,6 +79,7 @@ class CatalogProducts {
|
|||
'headers' => array(
|
||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||
'Content-Type' => 'application/json',
|
||||
'Prefer' => 'return=representation'
|
||||
),
|
||||
'body' => wp_json_encode( $data ),
|
||||
);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue