@@ -43,64 +43,50 @@ public function process($planId = null)
4343 $ shop = ShopifyApp::shop ();
4444 $ chargeId = request ('charge_id ' );
4545
46- // Setup the plan and get the charge
46+ // Setup the plan and activate
4747 $ plan = $ this ->getPlan ($ planId );
4848 $ billingPlan = new BillingPlan ($ shop , $ plan );
4949 $ billingPlan ->setChargeId ($ chargeId );
50- $ status = $ billingPlan ->getCharge ()-> status ;
50+ $ response = $ billingPlan ->activate () ;
5151
52- // Grab the plan detailed used
53- $ planDetails = $ billingPlan ->getChargeParams ();
54- unset($ planDetails ['return_url ' ]);
55-
56- // Create a charge (regardless of the status)
52+ // Create a charge
5753 $ charge = Charge::firstOrNew ([
5854 'type ' => $ plan ->type ,
59- 'shop_id ' => $ shop ->id ,
6055 'plan_id ' => $ plan ->id ,
56+ 'shop_id ' => $ shop ->id ,
6157 'charge_id ' => $ chargeId ,
58+ 'status ' => $ response ->status ,
6259 ]);
6360
64- // Check the customer's answer to the billing
65- if ($ status === 'accepted ' ) {
66- // Activate and add details to our charge
67- $ response = $ billingPlan ->activate ();
68- $ charge ->status = $ response ->status ;
69-
70- if ($ plan ->type === Charge::CHARGE_RECURRING ) {
71- // Recurring
72- $ charge ->billing_on = $ response ->billing_on ;
73- $ charge ->trial_ends_on = $ response ->trial_ends_on ;
74- $ charge ->activated_on = $ response ->activated_on ;
75- } else {
76- // One time
77- $ charge ->activated_on = Carbon::today ()->format ('Y-m-d ' );
78- }
79-
80- // Set old charge as cancelled, if one
81- $ lastCharge = $ this ->getLastCharge ($ shop );
82- if ($ lastCharge ) {
83- $ lastCharge ->status = 'cancelled ' ;
84- $ lastCharge ->save ();
85- }
61+ if ($ plan ->type === Charge::CHARGE_RECURRING ) {
62+ // Recurring
63+ $ charge ->billing_on = $ response ->billing_on ;
64+ $ charge ->trial_ends_on = $ response ->trial_ends_on ;
65+ $ charge ->activated_on = $ response ->activated_on ;
8666 } else {
87- // Customer declined the charge
88- $ charge ->status = 'declined ' ;
89- $ charge ->cancelled_on = Carbon::today ()->format ('Y-m-d ' );
67+ // One time
68+ $ charge ->activated_on = Carbon::today ()->format ('Y-m-d ' );
9069 }
9170
9271 // Merge in the plan details since the fields match the database columns
72+ $ planDetails = $ billingPlan ->getChargeParams ();
73+ unset($ planDetails ['return_url ' ]);
9374 foreach ($ planDetails as $ key => $ value ) {
9475 $ charge ->{$ key } = $ value ;
9576 }
77+
78+ // Finally, save the charge
9679 $ charge ->save ();
9780
98- if ($ status === 'declined ' ) {
99- // Show the error... don't allow access
100- return view ('shopify-app::billing.error ' , ['message ' => 'It seems you have declined the billing charge for this application ' ]);
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 ();
10187 }
10288
103- // All good, update the shop's plan and take them off freeium (if applicable)
89+ // All good, update the shop's plan and take them off freemium (if applicable)
10490 $ shop ->freemium = false ;
10591 $ shop ->plan_id = $ plan ->id ;
10692 $ shop ->save ();
0 commit comments