Update plan

This commit is contained in:
Emili Castells Guasch 2023-03-28 12:38:43 +02:00
parent 53ab79e576
commit 72be73c5dc
4 changed files with 79 additions and 32 deletions

View file

@ -11,6 +11,7 @@ namespace WooCommerce\PayPalCommerce\ApiClient\Endpoint;
use Psr\Log\LoggerInterface;
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
use WooCommerce\PayPalCommerce\ApiClient\Entity\BillingCycle;
use WooCommerce\PayPalCommerce\ApiClient\Entity\Plan;
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
@ -130,38 +131,46 @@ class BillingPlans {
return $this->plan_factory->from_paypal_response($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 {
public function plan(string $id): Plan {
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/billing/plans/' . $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->plan_factory->from_paypal_response($json);
}
public function update_pricing(string $id, BillingCycle $billing_cycle): 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"
),
),
"pricing_scheme" => $billing_cycle->pricing_scheme(),
),
),
);
$bearer = $this->bearer->bearer();
$url = trailingslashit( $this->host ) . 'v1/billing/plans/' . $billing_plan_id . '/update-pricing-schemes';
$url = trailingslashit( $this->host ) . 'v1/billing/plans/' . $id . '/update-pricing-schemes';
$args = array(
'method' => 'POST',
'headers' => array(
@ -173,7 +182,7 @@ class BillingPlans {
$response = $this->request( $url, $args );
if ( is_wp_error( $response ) || ! is_array( $response ) ) {
throw new RuntimeException( 'Not able to create plan.' );
throw new RuntimeException( 'Could not update pricing.' );
}
$json = json_decode( $response['body'] );