mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Add update subscription (WIP)
This commit is contained in:
parent
8ceb7e0664
commit
b5e1f1530a
8 changed files with 171 additions and 44 deletions
|
@ -734,7 +734,7 @@ class WC_Subscriptions_Product
|
||||||
/**
|
/**
|
||||||
* Hooked to the @see 'wp_scheduled_delete' WP-Cron scheduled task to rename the '_wp_trash_meta_time' meta value
|
* Hooked to the @see 'wp_scheduled_delete' WP-Cron scheduled task to rename the '_wp_trash_meta_time' meta value
|
||||||
* as '_wc_trash_meta_time'. This is the flag used by WordPress to determine which posts should be automatically
|
* as '_wc_trash_meta_time'. This is the flag used by WordPress to determine which posts should be automatically
|
||||||
* purged from the trash. We want to make sure Subscriptions products are not automatically purged (but still want
|
* purged from the trash. We want to make sure BillingPlans products are not automatically purged (but still want
|
||||||
* to keep a record of when the product was trashed).
|
* to keep a record of when the product was trashed).
|
||||||
*
|
*
|
||||||
* @since 1.4.9
|
* @since 1.4.9
|
||||||
|
@ -1629,7 +1629,7 @@ function wcs_get_order_items_product_id($item_id)
|
||||||
/**
|
/**
|
||||||
* Get the variation ID for variation items or the product ID for non-variation items.
|
* Get the variation ID for variation items or the product ID for non-variation items.
|
||||||
*
|
*
|
||||||
* When acting on cart items or order items, Subscriptions often needs to use an item's canonical product ID. For
|
* When acting on cart items or order items, BillingPlans often needs to use an item's canonical product ID. For
|
||||||
* items representing a variation, that means the 'variation_id' value, if the item is not a variation, that means
|
* items representing a variation, that means the 'variation_id' value, if the item is not a variation, that means
|
||||||
* the 'product_id value. This function helps save keystrokes on the idiom to check if an item is to a variation or not.
|
* the 'product_id value. This function helps save keystrokes on the idiom to check if an item is to a variation or not.
|
||||||
*
|
*
|
||||||
|
|
|
@ -10,7 +10,7 @@ declare(strict_types=1);
|
||||||
namespace WooCommerce\PayPalCommerce\ApiClient;
|
namespace WooCommerce\PayPalCommerce\ApiClient;
|
||||||
|
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
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\Vendor\Psr\Container\ContainerInterface;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\Bearer;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
|
use WooCommerce\PayPalCommerce\ApiClient\Authentication\PayPalBearer;
|
||||||
|
@ -215,8 +215,8 @@ return array(
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
$container->get( 'woocommerce.logger.woocommerce' )
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
'api.endpoint.subscriptions' => static function( ContainerInterface $container ): Subscriptions {
|
'api.endpoint.billing-plans' => static function( ContainerInterface $container ): BillingPlans {
|
||||||
return new Subscriptions(
|
return new BillingPlans(
|
||||||
$container->get( 'api.host' ),
|
$container->get( 'api.host' ),
|
||||||
$container->get( 'api.bearer' ),
|
$container->get( 'api.bearer' ),
|
||||||
$container->get( 'woocommerce.logger.woocommerce' )
|
$container->get( 'woocommerce.logger.woocommerce' )
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
<?php
|
<?php
|
||||||
/**
|
/**
|
||||||
* The Subscriptions endpoint.
|
* The Billing Plans endpoint.
|
||||||
*
|
*
|
||||||
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
|
* @package WooCommerce\PayPalCommerce\ApiClient\Endpoint
|
||||||
*/
|
*/
|
||||||
|
@ -16,9 +16,9 @@ use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\RuntimeException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class Subscriptions
|
* Class BillingPlans
|
||||||
*/
|
*/
|
||||||
class Subscriptions {
|
class BillingPlans {
|
||||||
|
|
||||||
use RequestTrait;
|
use RequestTrait;
|
||||||
|
|
||||||
|
@ -44,7 +44,7 @@ class Subscriptions {
|
||||||
private $logger;
|
private $logger;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Subscriptions constructor.
|
* BillingPlans constructor.
|
||||||
*
|
*
|
||||||
* @param string $host The host.
|
* @param string $host The host.
|
||||||
* @param Bearer $bearer The bearer.
|
* @param Bearer $bearer The bearer.
|
||||||
|
@ -70,7 +70,7 @@ class Subscriptions {
|
||||||
* @throws RuntimeException If the request fails.
|
* @throws RuntimeException If the request fails.
|
||||||
* @throws PayPalApiException If the request fails.
|
* @throws PayPalApiException If the request fails.
|
||||||
*/
|
*/
|
||||||
public function create_plan(
|
public function create(
|
||||||
string $product_id,
|
string $product_id,
|
||||||
array $billing_cycles,
|
array $billing_cycles,
|
||||||
array $payment_preferences
|
array $payment_preferences
|
||||||
|
@ -90,6 +90,7 @@ class Subscriptions {
|
||||||
'headers' => array(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
|
'Prefer' => 'return=representation'
|
||||||
),
|
),
|
||||||
'body' => wp_json_encode( $data ),
|
'body' => wp_json_encode( $data ),
|
||||||
);
|
);
|
||||||
|
@ -111,4 +112,60 @@ class Subscriptions {
|
||||||
|
|
||||||
return $json;
|
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(
|
'headers' => array(
|
||||||
'Authorization' => 'Bearer ' . $bearer->token(),
|
'Authorization' => 'Bearer ' . $bearer->token(),
|
||||||
'Content-Type' => 'application/json',
|
'Content-Type' => 'application/json',
|
||||||
|
'Prefer' => 'return=representation'
|
||||||
),
|
),
|
||||||
'body' => wp_json_encode( $data ),
|
'body' => wp_json_encode( $data ),
|
||||||
);
|
);
|
||||||
|
|
|
@ -38,7 +38,7 @@ class SubscriptionsHandler {
|
||||||
/**
|
/**
|
||||||
* Constructor.
|
* Constructor.
|
||||||
*
|
*
|
||||||
* @param RenewalHandler $ppcp_renewal_handler PayPal Payments Subscriptions renewal handler.
|
* @param RenewalHandler $ppcp_renewal_handler PayPal Payments BillingPlans renewal handler.
|
||||||
* @param MockGateway $gateway Mock gateway instance.
|
* @param MockGateway $gateway Mock gateway instance.
|
||||||
*/
|
*/
|
||||||
public function __construct( RenewalHandler $ppcp_renewal_handler, MockGateway $gateway ) {
|
public function __construct( RenewalHandler $ppcp_renewal_handler, MockGateway $gateway ) {
|
||||||
|
@ -69,7 +69,7 @@ class SubscriptionsHandler {
|
||||||
/**
|
/**
|
||||||
* Adds a mock gateway to disguise as PPEC when needed. Hooked onto `woocommerce_payment_gateways`.
|
* Adds a mock gateway to disguise as PPEC when needed. Hooked onto `woocommerce_payment_gateways`.
|
||||||
* The mock gateway fixes display issues where subscriptions paid via PPEC appear as "via Manual Renewal" and also
|
* The mock gateway fixes display issues where subscriptions paid via PPEC appear as "via Manual Renewal" and also
|
||||||
* prevents Subscriptions from automatically changing the payment method to "manual" when a subscription is edited.
|
* prevents BillingPlans from automatically changing the payment method to "manual" when a subscription is edited.
|
||||||
*
|
*
|
||||||
* @param array $gateways List of gateways.
|
* @param array $gateways List of gateways.
|
||||||
* @return array
|
* @return array
|
||||||
|
@ -144,14 +144,14 @@ class SubscriptionsHandler {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// My Account > Subscriptions.
|
// My Account > BillingPlans.
|
||||||
if ( is_wc_endpoint_url( 'subscriptions' ) ) {
|
if ( is_wc_endpoint_url( 'subscriptions' ) ) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Checks that require Subscriptions.
|
// Checks that require BillingPlans.
|
||||||
if ( class_exists( \WC_Subscriptions::class ) ) {
|
if ( class_exists( \WC_Subscriptions::class ) ) {
|
||||||
// My Account > Subscriptions > (Subscription).
|
// My Account > BillingPlans > (Subscription).
|
||||||
if ( wcs_is_view_subscription_page() ) {
|
if ( wcs_is_view_subscription_page() ) {
|
||||||
$subscription = wcs_get_subscription( absint( get_query_var( 'view-subscription' ) ) );
|
$subscription = wcs_get_subscription( absint( get_query_var( 'view-subscription' ) ) );
|
||||||
|
|
||||||
|
@ -183,7 +183,7 @@ class SubscriptionsHandler {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Are we on the WC > Subscriptions screen?
|
// Are we on the WC > BillingPlans screen?
|
||||||
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
// phpcs:ignore WordPress.Security.NonceVerification.Missing
|
||||||
$post_type = wc_clean( wp_unslash( $_GET['post_type'] ?? $_POST['post_type'] ?? '' ) );
|
$post_type = wc_clean( wp_unslash( $_GET['post_type'] ?? $_POST['post_type'] ?? '' ) );
|
||||||
if ( $post_type === 'shop_subscription' ) {
|
if ( $post_type === 'shop_subscription' ) {
|
||||||
|
|
|
@ -11,7 +11,7 @@ namespace WooCommerce\PayPalCommerce\Subscription;
|
||||||
|
|
||||||
use WC_Product_Subscription;
|
use WC_Product_Subscription;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Subscriptions;
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\BillingPlans;
|
||||||
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
use WooCommerce\PayPalCommerce\ApiClient\Exception\PayPalApiException;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Container\ServiceProvider;
|
||||||
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
use WooCommerce\PayPalCommerce\Vendor\Dhii\Modular\Module\ModuleInterface;
|
||||||
|
@ -161,13 +161,47 @@ class SubscriptionModule implements ModuleInterface {
|
||||||
|
|
||||||
$product = wc_get_product( $product_id );
|
$product = wc_get_product( $product_id );
|
||||||
if ( $product->get_type() === 'subscription' ) {
|
if ( $product->get_type() === 'subscription' ) {
|
||||||
if ( ! $product->meta_exists( 'ppcp_subscription_product_id' ) ) {
|
$billing_plans_endpoint = $c->get( 'api.endpoint.billing-plans' );
|
||||||
|
assert( $billing_plans_endpoint instanceof BillingPlans );
|
||||||
|
|
||||||
|
if($product->meta_exists( 'ppcp_subscription_product' ) && $product->meta_exists( 'ppcp_subscription_plan' )) {
|
||||||
|
if($product->get_meta( '_subscription_price' ) === $product->get_meta('ppcp_subscription_plan')->billing_cycles[0]->pricing_scheme->fixed_price->value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$billing_cycles = array(
|
||||||
|
'pricing_scheme' => array(
|
||||||
|
'fixed_price' => array(
|
||||||
|
'value' => $product->get_meta( '_subscription_price' ),
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
try {
|
||||||
|
$billing_plans_endpoint->update_pricing(
|
||||||
|
$product->get_meta('ppcp_subscription_plan')->id,
|
||||||
|
$billing_cycles
|
||||||
|
);
|
||||||
|
} catch ( RuntimeException $exception ) {
|
||||||
|
$error = $exception->getMessage();
|
||||||
|
if ( is_a( $exception, PayPalApiException::class ) ) {
|
||||||
|
$error = $exception->get_details( $error );
|
||||||
|
}
|
||||||
|
|
||||||
|
$logger = $c->get( 'woocommerce.logger.woocommerce' );
|
||||||
|
$logger->error( 'Could not update subscription product on PayPal. ' . $error );
|
||||||
|
}
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if ( ! $product->meta_exists( 'ppcp_subscription_product' ) ) {
|
||||||
$products_endpoint = $c->get( 'api.endpoint.catalog-products' );
|
$products_endpoint = $c->get( 'api.endpoint.catalog-products' );
|
||||||
assert( $products_endpoint instanceof CatalogProducts );
|
assert( $products_endpoint instanceof CatalogProducts );
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$subscription_product = $products_endpoint->create( $product->get_title() );
|
$subscription_product = $products_endpoint->create( $product->get_title() );
|
||||||
$product->update_meta_data( 'ppcp_subscription_product_id', $subscription_product->id );
|
$product->update_meta_data( 'ppcp_subscription_product', $subscription_product );
|
||||||
$product->save();
|
$product->save();
|
||||||
} catch ( RuntimeException $exception ) {
|
} catch ( RuntimeException $exception ) {
|
||||||
$error = $exception->getMessage();
|
$error = $exception->getMessage();
|
||||||
|
@ -180,10 +214,7 @@ class SubscriptionModule implements ModuleInterface {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( $product->get_meta( 'ppcp_subscription_product_id' ) && ! $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
if ( $product->get_meta( 'ppcp_subscription_product' ) && ! $product->meta_exists( 'ppcp_subscription_plan' ) ) {
|
||||||
$subscriptions_endpoint = $c->get( 'api.endpoint.subscriptions' );
|
|
||||||
assert( $subscriptions_endpoint instanceof Subscriptions );
|
|
||||||
|
|
||||||
$billing_cycles = array(
|
$billing_cycles = array(
|
||||||
'frequency' => array(
|
'frequency' => array(
|
||||||
'interval_unit' => $product->get_meta( '_subscription_period' ),
|
'interval_unit' => $product->get_meta( '_subscription_period' ),
|
||||||
|
@ -211,13 +242,13 @@ class SubscriptionModule implements ModuleInterface {
|
||||||
);
|
);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
$subscription_plan = $subscriptions_endpoint->create_plan(
|
$subscription_plan = $billing_plans_endpoint->create(
|
||||||
$product->get_meta( 'ppcp_subscription_product_id' ),
|
$product->get_meta( 'ppcp_subscription_product' )->id,
|
||||||
$billing_cycles,
|
$billing_cycles,
|
||||||
$payment_preferences
|
$payment_preferences
|
||||||
);
|
);
|
||||||
|
|
||||||
$product->update_meta_data( 'ppcp_subscription_plan', $subscription_plan->id );
|
$product->update_meta_data( 'ppcp_subscription_plan', $subscription_plan );
|
||||||
$product->save();
|
$product->save();
|
||||||
} catch ( RuntimeException $exception ) {
|
} catch ( RuntimeException $exception ) {
|
||||||
$error = $exception->getMessage();
|
$error = $exception->getMessage();
|
||||||
|
@ -236,20 +267,23 @@ class SubscriptionModule implements ModuleInterface {
|
||||||
|
|
||||||
add_action(
|
add_action(
|
||||||
'add_meta_boxes',
|
'add_meta_boxes',
|
||||||
function( string $post_type ) {
|
function( string $post_type ) use($c) {
|
||||||
if ( $post_type === 'product' ) {
|
if ( $post_type === 'product' ) {
|
||||||
$post_id = wc_clean( wp_unslash( $_GET['post'] ?? '' ) );
|
$post_id = wc_clean( wp_unslash( $_GET['post'] ?? '' ) );
|
||||||
$product = wc_get_product( $post_id );
|
$product = wc_get_product( $post_id );
|
||||||
if ( is_a( $product, WC_Product_Subscription::class ) ) {
|
if (is_a($product, WC_Product_Subscription::class)) {
|
||||||
$product_id = $product->get_meta( 'ppcp_subscription_product_id' );
|
$settings = $c->get('wcgateway.settings');
|
||||||
$plan_id = $product->get_meta( 'ppcp_subscription_plan' );
|
assert($settings instanceof Settings);
|
||||||
if ( $product_id && $plan_id ) {
|
if ($settings->get('subscriptions_mode') && $settings->get('subscriptions_mode') === 'subscriptions_api') {
|
||||||
add_meta_box(
|
$subscription_product = $product->get_meta('ppcp_subscription_product');
|
||||||
'ppcp_subscription',
|
$subscription_plan = $product->get_meta('ppcp_subscription_plan');
|
||||||
__( 'PayPal Subscription', 'woocommerce-paypal-payments' ),
|
add_meta_box('ppcp_subscription', __('PayPal Subscription', 'woocommerce-paypal-payments'),
|
||||||
function() use ( $product_id, $plan_id ) {
|
function () use ($subscription_product, $subscription_plan) {
|
||||||
echo '<p>Product ID: ' . esc_attr( $product_id ) . '</p>';
|
echo '<label><input type="checkbox" name="ppcp_connect_subscriptions_api" checked="checked">Connect to BillingPlans API</label>';
|
||||||
echo '<p>Plan ID: ' . esc_attr( $plan_id ) . '</p>';
|
if ($subscription_product && $subscription_plan) {
|
||||||
|
echo '<p>Product ID: ' . esc_attr($subscription_product->id) . '</p>';
|
||||||
|
echo '<p>Plan ID: ' . esc_attr($subscription_plan->id) . '</p>';
|
||||||
|
}
|
||||||
},
|
},
|
||||||
$post_type,
|
$post_type,
|
||||||
'side',
|
'side',
|
||||||
|
|
|
@ -409,17 +409,17 @@ return function ( ContainerInterface $container, array $fields ): array {
|
||||||
'input_class' => $container->get( 'wcgateway.settings.should-disable-fraudnet-checkbox' ) ? array( 'ppcp-disabled-checkbox' ) : array(),
|
'input_class' => $container->get( 'wcgateway.settings.should-disable-fraudnet-checkbox' ) ? array( 'ppcp-disabled-checkbox' ) : array(),
|
||||||
),
|
),
|
||||||
'subscriptions_configuration_heading' => array(
|
'subscriptions_configuration_heading' => array(
|
||||||
'heading' => __( 'Subscriptions', 'woocommerce-paypal-payments' ),
|
'heading' => __( 'BillingPlans', 'woocommerce-paypal-payments' ),
|
||||||
'type' => 'ppcp-heading',
|
'type' => 'ppcp-heading',
|
||||||
'screens' => array(
|
'screens' => array(
|
||||||
State::STATE_ONBOARDED,
|
State::STATE_ONBOARDED,
|
||||||
),
|
),
|
||||||
'requirements' => array(),
|
'requirements' => array(),
|
||||||
'gateway' => Settings::CONNECTION_TAB_ID,
|
'gateway' => Settings::CONNECTION_TAB_ID,
|
||||||
'description' => __( 'Configure WooCommerce Subscriptions integration with PayPal.', 'woocommerce-paypal-payments' ),
|
'description' => __( 'Configure WooCommerce BillingPlans integration with PayPal.', 'woocommerce-paypal-payments' ),
|
||||||
),
|
),
|
||||||
'subscriptions_mode' => array(
|
'subscriptions_mode' => array(
|
||||||
'title' => __( 'Subscriptions Mode', 'woocommerce-paypal-payments' ),
|
'title' => __( 'BillingPlans Mode', 'woocommerce-paypal-payments' ),
|
||||||
'type' => 'select',
|
'type' => 'select',
|
||||||
'class' => array(),
|
'class' => array(),
|
||||||
'input_class' => array( 'wc-enhanced-select' ),
|
'input_class' => array( 'wc-enhanced-select' ),
|
||||||
|
@ -428,7 +428,7 @@ return function ( ContainerInterface $container, array $fields ): array {
|
||||||
'default' => 'vaulting_api',
|
'default' => 'vaulting_api',
|
||||||
'options' => array(
|
'options' => array(
|
||||||
'vaulting_api' => __( 'PayPal Vaulting', 'woocommerce-paypal-payments' ),
|
'vaulting_api' => __( 'PayPal Vaulting', 'woocommerce-paypal-payments' ),
|
||||||
'subscriptions_api' => __( 'PayPal Subscriptions', 'woocommerce-paypal-payments' ),
|
'subscriptions_api' => __( 'PayPal BillingPlans', 'woocommerce-paypal-payments' ),
|
||||||
),
|
),
|
||||||
'screens' => array(
|
'screens' => array(
|
||||||
State::STATE_ONBOARDED,
|
State::STATE_ONBOARDED,
|
||||||
|
|
|
@ -15,11 +15,14 @@ async function loginAsAdmin(page) {
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
test.describe('Merchant', () => {
|
test.describe.serial('Merchant', () => {
|
||||||
|
const title = (Math.random() + 1).toString(36).substring(7);
|
||||||
|
let product_id = '';
|
||||||
|
let plan_id = '';
|
||||||
|
|
||||||
test('Create new subscription product', async ({page, request}) => {
|
test('Create new subscription product', async ({page, request}) => {
|
||||||
await loginAsAdmin(page);
|
await loginAsAdmin(page);
|
||||||
|
|
||||||
const title = (Math.random() + 1).toString(36).substring(7);
|
|
||||||
await page.goto('/wp-admin/post-new.php?post_type=product');
|
await page.goto('/wp-admin/post-new.php?post_type=product');
|
||||||
await page.fill('#title', title);
|
await page.fill('#title', title);
|
||||||
await page.selectOption('select#product-type', 'subscription');
|
await page.selectOption('select#product-type', 'subscription');
|
||||||
|
@ -47,7 +50,9 @@ test.describe('Merchant', () => {
|
||||||
});
|
});
|
||||||
await expect(product.id).toBeTruthy;
|
await expect(product.id).toBeTruthy;
|
||||||
|
|
||||||
const plans = await request.get(`https://api.sandbox.paypal.com/v1/billing/plans?product_id=${product.id}&page_size=10&page=1&total_required=true`, {
|
product_id = product.id;
|
||||||
|
|
||||||
|
const plans = await request.get(`https://api.sandbox.paypal.com/v1/billing/plans?product_id=${product_id}&page_size=10&page=1&total_required=true`, {
|
||||||
headers: {
|
headers: {
|
||||||
'Authorization': AUTHORIZATION,
|
'Authorization': AUTHORIZATION,
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
|
@ -60,5 +65,35 @@ test.describe('Merchant', () => {
|
||||||
return p.product_id === product.id;
|
return p.product_id === product.id;
|
||||||
});
|
});
|
||||||
await expect(plan.id).toBeTruthy;
|
await expect(plan.id).toBeTruthy;
|
||||||
|
|
||||||
|
plan_id = plan.id;
|
||||||
|
});
|
||||||
|
|
||||||
|
test('Update subscription product', async ({page, request}) => {
|
||||||
|
await loginAsAdmin(page);
|
||||||
|
|
||||||
|
await page.goto('/wp-admin/edit.php?post_type=product');
|
||||||
|
await page.getByRole('link', { name: title, exact: true }).click();
|
||||||
|
|
||||||
|
await page.fill('#_subscription_price', '20');
|
||||||
|
|
||||||
|
await Promise.all([
|
||||||
|
page.waitForNavigation(),
|
||||||
|
page.locator('#publish').click(),
|
||||||
|
]);
|
||||||
|
|
||||||
|
const message = await page.locator('.notice-success');
|
||||||
|
await expect(message).toContainText('Product updated.');
|
||||||
|
|
||||||
|
const plan = await request.get(`https://api.sandbox.paypal.com/v1/billing/plans/${plan_id}`, {
|
||||||
|
headers: {
|
||||||
|
'Authorization': AUTHORIZATION,
|
||||||
|
'Content-Type': 'application/json'
|
||||||
|
}
|
||||||
|
});
|
||||||
|
expect(plan.ok()).toBeTruthy();
|
||||||
|
|
||||||
|
const plan_content = await plan.json();
|
||||||
|
await expect(plan_content.billing_cycles[0].pricing_scheme.fixed_price.value).toBe('20.0')
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue