From e7b5ab3af5a919b3c4536b3eca2c8f5c01ba8784 Mon Sep 17 00:00:00 2001 From: Matthew Somerville Date: Thu, 10 Oct 2024 14:34:31 +0100 Subject: [PATCH] Remove reliance on fixed Price IDs. Switch to using metadata on the price to get the number of calls for a price, rather than assuming it can be worked out from the price (plan) ID. --- classes/Subscription.php | 11 +++++------ classes/TestStripe.php | 2 +- conf/general-example | 10 ++++++++++ tests/AcceptApiTest.php | 2 +- www/docs/api/hook.php | 8 ++++---- www/docs/js/payment.js | 10 ++-------- .../easyparliament/templates/html/api/update.php | 14 ++++++++++---- 7 files changed, 33 insertions(+), 24 deletions(-) diff --git a/classes/Subscription.php b/classes/Subscription.php index dfac8bc680..cdfdcd361f 100644 --- a/classes/Subscription.php +++ b/classes/Subscription.php @@ -11,8 +11,8 @@ class Subscription { public $upcoming; public $has_payment_data = false; - private static $prices = ['twfy-1k', 'twfy-5k', 'twfy-10k', 'twfy-0k']; - private static $amounts = [2000, 5000, 10000, 30000]; + private static $prices = [PRICING_TIER_1_ID, PRICING_TIER_2_ID, PRICING_TIER_3_ID, PRICING_TIER_4_ID]; + private static $amounts = [PRICING_TIER_1_AMOUNT, PRICING_TIER_2_AMOUNT, PRICING_TIER_3_AMOUNT, PRICING_TIER_4_AMOUNT]; public function __construct($arg) { # User ID @@ -104,7 +104,7 @@ private function update_subscription($form_data) { foreach ($this::$prices as $i => $price) { if ($price == $form_data['price']) { - $new_price = $this::$amounts[$i]; + $new_price = $this::$amounts[$i] * 100; if ($form_data['coupon'] == 'charitable100') { $new_price = 0; } elseif ($form_data['coupon'] == 'charitable50') { @@ -112,7 +112,7 @@ private function update_subscription($form_data) { } } if ($price == $this->stripe->price->id) { - $old_price = $this::$amounts[$i]; + $old_price = $this::$amounts[$i] * 100; if ($this->stripe->discount && ($coupon = $this->stripe->discount->coupon)) { if ($coupon->percent_off == 100) { $old_price = 0; @@ -326,8 +326,7 @@ public function createOrUpdateFromForm() { } public function redis_update_max($price) { - preg_match('#^twfy-(\d+)k#', $price, $m); - $max = $m[1] * 1000; + $max = $price->metadata['calls']; $this->redis->set("$this->redis_prefix:max", $max); $this->redis->del("$this->redis_prefix:blocked"); } diff --git a/classes/TestStripe.php b/classes/TestStripe.php index 70f6d281a9..903af173c4 100644 --- a/classes/TestStripe.php +++ b/classes/TestStripe.php @@ -14,7 +14,7 @@ public function getSubscription($args) { 'items' => [ [ 'price' => [ 'unit_amount' => '2000', - 'id' => 'twfy-1k', + 'id' => 'price_12', 'nickname' => 'Some calls per month', 'interval' => 'month', ], diff --git a/conf/general-example b/conf/general-example index c5559d8f11..00442551b3 100644 --- a/conf/general-example +++ b/conf/general-example @@ -197,6 +197,16 @@ define('STRIPE_SECRET_KEY', ''); define('STRIPE_ENDPOINT_SECRET', ''); define('STRIPE_API_VERSION', ''); define('STRIPE_TAX_RATE', ''); + +define('PRICING_TIER_1_ID', 'price_12'); +define('PRICING_TIER_2_ID', 'price_34'); +define('PRICING_TIER_3_ID', 'price_56'); +define('PRICING_TIER_4_ID', 'price_78'); +define('PRICING_TIER_1_AMOUNT', '20'); +define('PRICING_TIER_2_AMOUNT', '50'); +define('PRICING_TIER_3_AMOUNT', '100'); +define('PRICING_TIER_4_AMOUNT', '300'); + define('REDIS_DB_HOST', 'localhost'); define('REDIS_DB_PORT', '6379'); define('REDIS_DB_NUMBER', '0'); diff --git a/tests/AcceptApiTest.php b/tests/AcceptApiTest.php index 35d932a429..154182cae0 100644 --- a/tests/AcceptApiTest.php +++ b/tests/AcceptApiTest.php @@ -144,7 +144,7 @@ public function testApiKeySignup() { $page = $this->post_page('key'); $this->assertStringContainsString('Subscribe to a plan', $page); $page = $this->post_page('update-plan', [ - 'price' => 'twfy-1k', + 'price' => 'price_12', 'charitable_tick' => 'on', 'charitable' => 'c', 'charity_number' => '123456', diff --git a/www/docs/api/hook.php b/www/docs/api/hook.php index 87291d13b6..44b5957b4e 100644 --- a/www/docs/api/hook.php +++ b/www/docs/api/hook.php @@ -30,7 +30,7 @@ } elseif ($event->type == 'customer.subscription.updated') { $sub = new \MySociety\TheyWorkForYou\Subscription($obj->id); if ($sub->stripe) { - $sub->redis_update_max($obj->items[0]->price->id); + $sub->redis_update_max($obj->items[0]->price); } } elseif ($event->type == 'invoice.payment_failed' && $obj->billing_reason == 'subscription_cycle' && stripe_twfy_sub($obj)) { $customer = \Stripe\Customer::retrieve($obj->customer); @@ -51,7 +51,7 @@ $sub = new \MySociety\TheyWorkForYou\Subscription($obj->subscription); } if ($sub->stripe) { - $sub->redis_update_max($sub->stripe->items[0]->price->id); + $sub->redis_update_max($sub->stripe->items[0]->price); } try { # Update the invoice's PaymentIntent and Charge to say it came from TWFY (for CSV export) @@ -82,8 +82,8 @@ function stripe_twfy_sub($invoice) { if (!$invoice->subscription) { return false; } - $stripe_sub = \Stripe\Subscription::retrieve($invoice->subscription); - return substr($stripe_sub->items[0]->price->id, 0, 4) == 'twfy'; + $stripe_sub = \Stripe\Subscription::retrieve($invoice->subscription, expand=['items.data.price.product']); + return substr($stripe_sub->items[0]->price->product->name, 0, 14) == 'TheyWorkForYou'; } function stripe_reset_quota($subscription) { diff --git a/www/docs/js/payment.js b/www/docs/js/payment.js index 0deabf6b52..cfc8a4b463 100644 --- a/www/docs/js/payment.js +++ b/www/docs/js/payment.js @@ -2,20 +2,14 @@ function price_cost() { var price = document.querySelector('input[name=price]:checked'), + pricing = document.getElementById('js-price-information'), ctick = document.getElementById('id_charitable_tick'), charitable = document.querySelector('input[name=charitable]:checked'); price = price ? price.value : ''; ctick = ctick ? ctick.checked : ''; charitable = charitable ? charitable.value : ''; - var num = 20; - if (price === 'twfy-5k') { - num = 50; - } else if (price === 'twfy-10k') { - num = 100; - } else if (price === 'twfy-0k') { - num = 300; - } + var num = pricing.dataset[price] || 20; if (ctick) { if (charitable === 'c' || charitable === 'i') { if (num === 20) { diff --git a/www/includes/easyparliament/templates/html/api/update.php b/www/includes/easyparliament/templates/html/api/update.php index dd92899bf6..5d3045b5cd 100644 --- a/www/includes/easyparliament/templates/html/api/update.php +++ b/www/includes/easyparliament/templates/html/api/update.php @@ -53,11 +53,17 @@ function rdio($name, $value, $text, $id, $required = false, $checked = false) {