Skip to content

Commit

Permalink
Need to specify schedule ID, not object.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Jan 30, 2025
1 parent b6942bd commit 79d48af
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 11 deletions.
16 changes: 16 additions & 0 deletions classes/Stripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,20 @@ public function createSubscription($args) {
public function getInvoices($args) {
return \Stripe\Invoice::all($args);
}

public function createSchedule($id) {
return \Stripe\SubscriptionSchedule::create(['from_subscription' => $id]);
}

public function updateSchedule($id, $phases) {
return \Stripe\SubscriptionSchedule::update($id, ['phases' => $phases]);
}

public function releaseSchedule($id) {
\Stripe\SubscriptionSchedule::release($id);
}

public function updateSubscription($id, $args) {
\Stripe\Subscription::update($id, $args);
}
}
17 changes: 12 additions & 5 deletions classes/Subscription.php
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ private function update_subscription($form_data) {

if ($old_price >= $new_price) {
if ($this->stripe->schedule) {
\Stripe\SubscriptionSchedule::release($this->stripe->schedule);
$this->api->releaseSchedule($this->stripe->schedule->id);
}
$schedule = \Stripe\SubscriptionSchedule::create(['from_subscription' => $this->stripe->id]);
$schedule = $this->api->createSchedule($this->stripe->id);
$phases = [
[
'items' => [['price' => $schedule->phases[0]->items[0]->price]],
Expand All @@ -149,7 +149,7 @@ private function update_subscription($form_data) {
if ($form_data['coupon']) {
$phases[1]['coupon'] = $form_data['coupon'];
}
\Stripe\SubscriptionSchedule::update($schedule->id, ['phases' => $phases]);
$this->api->updateSchedule($schedule->id, $phases);
}

if ($old_price < $new_price) {
Expand All @@ -166,9 +166,9 @@ private function update_subscription($form_data) {
$args['coupon'] = '';
}
if ($this->stripe->schedule) {
\Stripe\SubscriptionSchedule::release($this->stripe->schedule);
$this->api->releaseSchedule($this->stripe->schedule->id);
}
\Stripe\Subscription::update($this->stripe->id, $args);
$this->api->updateSubscription($this->stripe->id, $args);
}
}

Expand Down Expand Up @@ -233,6 +233,13 @@ private function add_subscription($form_data) {
]);
}

public function cancel_subscription() {
if ($this->stripe->schedule) {
$this->api->releaseSchedule($this->stripe->schedule->id);
}
$this->api->updateSubscription($this->stripe->id, ['cancel_at_period_end' => true]);
}

public function invoices() {
$invoices = $this->api->getInvoices([
'subscription' => $this->stripe->id,
Expand Down
59 changes: 58 additions & 1 deletion classes/TestStripe.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,34 @@ public function getSubscription($args) {
],
],
], null);
} elseif ($args['id'] == 'sub_456') {
return \Stripe\Util\Util::convertToStripeObject([
'id' => 'sub_456',
'discount' => null,
'schedule' => [
'id' => 'sub_sched',
'phases' => [
],
],
'plan' => [
'amount' => '4167',
'id' => 'twfy-5k',
'nickname' => 'Many calls per month',
'interval' => 'month',
],
'cancel_at_period_end' => false,
'created' => time(),
'current_period_end' => time(),
'latest_invoice' => [],
'customer' => [
'id' => 'cus_456',
'balance' => 0,
'default_source' => [],
'invoice_settings' => [
'default_payment_method' => [],
],
],
], null);
}
return \Stripe\Util\Util::convertToStripeObject([], null);
}
Expand All @@ -51,8 +79,37 @@ public function updateCustomer($id, $args) {
}

public function createSubscription($args) {
if ($args['plan'] == 'twfy-5k') {
$id = 'sub_456';
} else {
$id = 'sub_123';
}
return \Stripe\Util\Util::convertToStripeObject([
'id' => $id,
], null);
}

public function createSchedule($id) {
return \Stripe\Util\Util::convertToStripeObject([
'id' => 'sub_123',
'id' => 'schedule_1',
'phases' => [
[
'start_date' => time(),
'end_date' => time(),
'discounts' => null,
'items' => [
[
'price' => '5000',
],
],
],
],
], null);
}

public function updateSchedule($id, $phases) {}

public function releaseSchedule($id) {}

public function updateSubscription($id, $args) {}
}
24 changes: 24 additions & 0 deletions tests/AcceptApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,28 @@ public function testApiKeySignup() {
$this->assertStringContainsString('It costs you £0/month.', $page);
$this->assertStringContainsString('100% discount applied.', $page);
}

public function testApiKeyDowngrade() {
$page = $this->post_page('update-plan', [
'stripeToken' => 'TOKEN',
'plan' => 'twfy-5k',
'charitable_tick' => 'on',
'charitable' => 'c',
'charity_number' => '123456',
'tandcs_tick' => 'on',
]);
$this->assertEquals('Location: /api/key?updated=1', $page);
$page = $this->get_page('key', ['updated' => 1]);
$this->assertStringContainsString('Your current plan is <strong>Many calls per month</strong>.', $page);
$this->assertStringContainsString('It costs you £50/month.', $page);

$page = $this->post_page('update-plan', [
'plan' => 'twfy-1k',
'charitable_tick' => 'on',
'charitable' => 'c',
'charity_number' => '123456',
'tandcs_tick' => 'on',
]);
$page = $this->get_page('key', ['updated' => 1]);
}
}
6 changes: 1 addition & 5 deletions www/docs/api/cancel-plan.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,7 @@
print 'CSRF validation failure!';
exit;
}

if ($subscription->stripe->schedule) {
\Stripe\SubscriptionSchedule::release($subscription->stripe->schedule);
}
\Stripe\Subscription::update($subscription->stripe->id, ['cancel_at_period_end' => true]);
$subscription->cancel_subscription();
redirect('/api/key?cancelled=1');
}

Expand Down

0 comments on commit 79d48af

Please sign in to comment.