1818use Osiset \ShopifyApp \Objects \Values \ShopId ;
1919use Osiset \ShopifyApp \Services \ChargeHelper ;
2020
21- /**
22- * Activates a plan for a shop.
23- */
2421class ActivatePlan
2522{
2623 /**
27- * The charge helper.
28- *
29- * @var ChargeHelper
30- */
31- protected $ chargeHelper ;
32-
33- /**
34- * Action which cancels the current plan.
35- *
36- * @var callable
37- */
38- protected $ cancelCurrentPlan ;
39-
40- /**
41- * Querier for shops.
42- *
43- * @var IShopQuery
44- */
45- protected $ shopQuery ;
46-
47- /**
48- * Command for charges.
49- *
50- * @var IChargeCommand
51- */
52- protected $ chargeCommand ;
53-
54- /**
55- * Command for shops.
56- *
57- * @var IShopCommand
58- */
59- protected $ shopCommand ;
60-
61- /**
62- * Querier for plans.
63- *
64- * @var IPlanQuery
65- */
66- protected $ planQuery ;
67-
68- /**
69- * Setup.
70- *
71- * @param callable $cancelCurrentPlanAction Action which cancels the current plan.
72- * @param ChargeHelper $chargeHelper The charge helper.
73- * @param IShopQuery $shopQuery The querier for shops.
74- * @param IPlanQuery $planQuery The querier for plans.
75- * @param IChargeCommand $chargeCommand The commands for charges.
76- * @param IShopCommand $shopCommand The commands for shops.
77- *
78- * @return void
24+ * @param callable $cancelCurrentPlan
7925 */
8026 public function __construct (
81- callable $ cancelCurrentPlanAction ,
82- ChargeHelper $ chargeHelper ,
83- IShopQuery $ shopQuery ,
84- IPlanQuery $ planQuery ,
85- IChargeCommand $ chargeCommand ,
86- IShopCommand $ shopCommand
27+ protected $ cancelCurrentPlan ,
28+ protected ChargeHelper $ chargeHelper ,
29+ protected IShopQuery $ shopQuery ,
30+ protected IPlanQuery $ planQuery ,
31+ protected IChargeCommand $ chargeCommand ,
32+ protected IShopCommand $ shopCommand
8733 ) {
88- $ this ->cancelCurrentPlan = $ cancelCurrentPlanAction ;
89- $ this ->chargeHelper = $ chargeHelper ;
90- $ this ->shopQuery = $ shopQuery ;
91- $ this ->planQuery = $ planQuery ;
92- $ this ->chargeCommand = $ chargeCommand ;
93- $ this ->shopCommand = $ shopCommand ;
9434 }
9535
9636 /**
97- * Execution.
9837 * TODO: Rethrow an API exception.
99- *
100- * @param ShopId $shopId The shop ID.
101- * @param PlanId $planId The plan to use.
102- * @param ChargeReference $chargeRef The charge ID from Shopify.
103- *
104- * @return ChargeId
10538 */
10639 public function __invoke (ShopId $ shopId , PlanId $ planId , ChargeReference $ chargeRef , string $ host ): ChargeId
10740 {
108- // Get the shop
10941 $ shop = $ this ->shopQuery ->getById ($ shopId );
110-
111- // Get the plan
11242 $ plan = $ this ->planQuery ->getById ($ planId );
11343 $ chargeType = ChargeType::fromNative ($ plan ->getType ()->toNative ());
11444
11545 // Activate the plan on Shopify
11646 $ response = $ shop ->apiHelper ()->activateCharge ($ chargeType , $ chargeRef );
117-
11847 // Cancel the shop's current plan
11948 call_user_func ($ this ->cancelCurrentPlan , $ shopId );
120-
12149 // Cancel the existing charge if it exists (happens if someone refreshes during)
12250 $ this ->chargeCommand ->delete ($ chargeRef , $ shopId );
12351
124- // Create the charge transfer
12552 $ transfer = new ChargeTransfer ();
12653 $ transfer ->shopId = $ shopId ;
12754 $ transfer ->planId = $ planId ;
12855 $ transfer ->chargeReference = $ chargeRef ;
12956 $ transfer ->chargeType = $ chargeType ;
13057 $ transfer ->chargeStatus = ChargeStatus::fromNative (strtoupper ($ response ['status ' ]));
58+ $ transfer ->planDetails = $ this ->chargeHelper ->details ($ plan , $ shop , $ host );
59+
13160 if ($ plan ->isType (PlanType::RECURRING ())) {
13261 $ transfer ->activatedOn = new Carbon ($ response ['activated_on ' ]);
13362 $ transfer ->billingOn = new Carbon ($ response ['billing_on ' ]);
@@ -137,9 +66,7 @@ public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $charge
13766 $ transfer ->billingOn = null ;
13867 $ transfer ->trialEndsOn = null ;
13968 }
140- $ transfer ->planDetails = $ this ->chargeHelper ->details ($ plan , $ shop , $ host );
14169
142- // Create the charge
14370 $ charge = $ this ->chargeCommand ->make ($ transfer );
14471 $ this ->shopCommand ->setToPlan ($ shopId , $ planId );
14572
0 commit comments