Skip to content

Commit 18c8a9f

Browse files
committed
Cancel last charge first
1 parent b5ae262 commit 18c8a9f

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

src/ShopifyApp/Traits/BillingControllerTrait.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public function process($planId = null)
4949
$billingPlan->setChargeId($chargeId);
5050
$response = $billingPlan->activate();
5151

52+
// Set old charge as cancelled, if one
53+
$lastCharge = $this->getLastCharge($shop);
54+
if ($lastCharge) {
55+
$lastCharge->status = 'cancelled';
56+
$lastCharge->cancelled_on = Carbon::today()->format('Y-m-d');
57+
$lastCharge->save();
58+
}
59+
5260
// Create a charge
5361
$charge = Charge::firstOrNew([
5462
'type' => $plan->type,
@@ -78,14 +86,6 @@ public function process($planId = null)
7886
// Finally, save the charge
7987
$charge->save();
8088

81-
// Set old charge as cancelled, if one
82-
$lastCharge = $this->getLastCharge($shop);
83-
if ($lastCharge) {
84-
$lastCharge->status = 'cancelled';
85-
$lastCharge->cancelled_on = Carbon::today()->format('Y-m-d');
86-
$lastCharge->save();
87-
}
88-
8989
// All good, update the shop's plan and take them off freemium (if applicable)
9090
$shop->freemium = false;
9191
$shop->plan_id = $plan->id;

0 commit comments

Comments
 (0)