Skip to content

Commit

Permalink
Remove reliance on fixed Price IDs.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
dracos committed Oct 10, 2024
1 parent c3b8277 commit e7b5ab3
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 24 deletions.
11 changes: 5 additions & 6 deletions classes/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -104,15 +104,15 @@ 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') {
$new_price /= 2;
}
}
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;
Expand Down Expand Up @@ -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");
}
Expand Down
2 changes: 1 addition & 1 deletion classes/TestStripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
],
Expand Down
10 changes: 10 additions & 0 deletions conf/general-example
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down
2 changes: 1 addition & 1 deletion tests/AcceptApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 4 additions & 4 deletions www/docs/api/hook.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand Down Expand Up @@ -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) {
Expand Down
10 changes: 2 additions & 8 deletions www/docs/js/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
14 changes: 10 additions & 4 deletions www/includes/easyparliament/templates/html/api/update.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,17 @@ function rdio($name, $value, $text, $id, $required = false, $checked = false) {
<ul id="id_price">
<?php
$price = $stripe ? $stripe->price->id : get_http_var('price');
rdio('price', 'twfy-1k', '£20/mth – 1,000 calls per month', 'id_price_0', 1, $price);
rdio('price', 'twfy-5k', '£50/mth – 5,000 calls per month', 'id_price_1', 1, $price);
rdio('price', 'twfy-10k', '£100/mth – 10,000 calls per month', 'id_price_2', 1, $price);
rdio('price', 'twfy-0k', '£300/mth – Unlimited calls', 'id_price_3', 1, $price);
rdio('price', PRICING_TIER_1_ID, '£' . PRICING_TIER_1_AMOUNT . '/mth – 1,000 calls per month', 'id_price_0', 1, $price);
rdio('price', PRICING_TIER_2_ID, '£' . PRICING_TIER_2_AMOUNT . '/mth – 5,000 calls per month', 'id_price_1', 1, $price);
rdio('price', PRICING_TIER_3_ID, '£' . PRICING_TIER_3_AMOUNT . '/mth – 10,000 calls per month', 'id_price_2', 1, $price);
rdio('price', PRICING_TIER_4_ID, '£' . PRICING_TIER_4_AMOUNT . '/mth – Unlimited calls', 'id_price_3', 1, $price);
?>
<span id="js-price-information"
data-<?= PRICING_TIER_1_ID ?>="<?= PRICING_TIER_1_AMOUNT ?>"
data-<?= PRICING_TIER_2_ID ?>="<?= PRICING_TIER_2_AMOUNT ?>"
data-<?= PRICING_TIER_3_ID ?>="<?= PRICING_TIER_3_AMOUNT ?>"
data-<?= PRICING_TIER_4_ID ?>="<?= PRICING_TIER_4_AMOUNT ?>"
></span>
</ul>
</div>

Expand Down

0 comments on commit e7b5ab3

Please sign in to comment.