mirror of
https://github.com/woocommerce/woocommerce-paypal-payments.git
synced 2025-09-06 18:16:38 +08:00
Create PayPal subscription plan from WC subscription product (WIP)
This commit is contained in:
parent
3c1ad831c1
commit
760a6d06f7
3 changed files with 65 additions and 3 deletions
|
@ -67,9 +67,9 @@ class CatalogProducts {
|
||||||
* @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(): stdClass {
|
public function create(string $name): stdClass {
|
||||||
$data = array(
|
$data = array(
|
||||||
'name' => '',
|
'name' => $name,
|
||||||
);
|
);
|
||||||
|
|
||||||
$bearer = $this->bearer->bearer();
|
$bearer = $this->bearer->bearer();
|
||||||
|
|
|
@ -70,9 +70,17 @@ 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( string $product_id ): stdClass {
|
public function create_plan(
|
||||||
|
string $product_id,
|
||||||
|
array $billing_cycles,
|
||||||
|
array $payment_preferences
|
||||||
|
): stdClass {
|
||||||
|
|
||||||
$data = array(
|
$data = array(
|
||||||
'product_id' => $product_id,
|
'product_id' => $product_id,
|
||||||
|
'name' => 'Testing Plan',
|
||||||
|
'billing_cycles' => array($billing_cycles),
|
||||||
|
'payment_preferences' => $payment_preferences
|
||||||
);
|
);
|
||||||
|
|
||||||
$bearer = $this->bearer->bearer();
|
$bearer = $this->bearer->bearer();
|
||||||
|
|
|
@ -9,6 +9,8 @@ declare(strict_types=1);
|
||||||
|
|
||||||
namespace WooCommerce\PayPalCommerce\Subscription;
|
namespace WooCommerce\PayPalCommerce\Subscription;
|
||||||
|
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\CatalogProducts;
|
||||||
|
use WooCommerce\PayPalCommerce\ApiClient\Endpoint\Subscriptions;
|
||||||
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;
|
||||||
use Psr\Log\LoggerInterface;
|
use Psr\Log\LoggerInterface;
|
||||||
|
@ -135,6 +137,58 @@ class SubscriptionModule implements ModuleInterface {
|
||||||
return $data;
|
return $data;
|
||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
add_action('woocommerce_update_product', function($product_id) use($c) {
|
||||||
|
$product = wc_get_product( $product_id );
|
||||||
|
if($product->get_type() === 'subscription') {
|
||||||
|
if(!$product->meta_exists('ppcp_subscription_product_id')) {
|
||||||
|
$products_endpoint = $c->get('api.endpoint.catalog-products');
|
||||||
|
assert($products_endpoint instanceof CatalogProducts);
|
||||||
|
|
||||||
|
$subscription_product = $products_endpoint->create($product->get_title());
|
||||||
|
$product->update_meta_data('ppcp_subscription_product_id', $subscription_product->id);
|
||||||
|
$product->save();
|
||||||
|
|
||||||
|
$subscriptions_endpoint = $c->get('api.endpoint.subscriptions');
|
||||||
|
assert($subscriptions_endpoint instanceof Subscriptions);
|
||||||
|
|
||||||
|
$billing_cycles = array(
|
||||||
|
'frequency' => array(
|
||||||
|
'interval_unit' => $product->get_meta('_subscription_period'),
|
||||||
|
'interval_count' => $product->get_meta('_subscription_period_interval'),
|
||||||
|
),
|
||||||
|
'tenure_type' => 'REGULAR',
|
||||||
|
'sequence' => 1,
|
||||||
|
'total_cycles' => $product->get_meta('_subscription_length'),
|
||||||
|
'pricing_scheme' => array(
|
||||||
|
'fixed_price' => array(
|
||||||
|
'value' => $product->get_meta('_subscription_price'),
|
||||||
|
'currency_code' => 'USD',
|
||||||
|
),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
|
||||||
|
$payment_preferences = array(
|
||||||
|
'auto_bill_outstanding' => true,
|
||||||
|
'setup_fee' => array(
|
||||||
|
'value' => $product->get_meta('_subscription_sign_up_fee'),
|
||||||
|
'currency_code' => 'USD',
|
||||||
|
),
|
||||||
|
'setup_fee_failure_action' => 'CONTINUE',
|
||||||
|
'payment_failure_threshold' => 3
|
||||||
|
);
|
||||||
|
|
||||||
|
$subscription_plan = $subscriptions_endpoint->create_plan(
|
||||||
|
$subscription_product->id,
|
||||||
|
$billing_cycles,
|
||||||
|
$payment_preferences
|
||||||
|
);
|
||||||
|
|
||||||
|
$product->update_meta_data('ppcp_subscription_plan', $subscription_plan->id);
|
||||||
|
$product->save();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue