This repository was archived by the owner on Nov 30, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 375
/
Copy pathActivatePlan.php
150 lines (132 loc) · 4.61 KB
/
ActivatePlan.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Osiset\ShopifyApp\Actions;
use Illuminate\Support\Carbon;
use Osiset\ShopifyApp\Contracts\Commands\Charge as IChargeCommand;
use Osiset\ShopifyApp\Contracts\Commands\Shop as IShopCommand;
use Osiset\ShopifyApp\Contracts\Objects\Values\PlanId;
use Osiset\ShopifyApp\Contracts\Queries\Plan as IPlanQuery;
use Osiset\ShopifyApp\Contracts\Queries\Shop as IShopQuery;
use Osiset\ShopifyApp\Messaging\Events\PlanActivatedEvent;
use Osiset\ShopifyApp\Objects\Enums\ChargeStatus;
use Osiset\ShopifyApp\Objects\Enums\ChargeType;
use Osiset\ShopifyApp\Objects\Enums\PlanType;
use Osiset\ShopifyApp\Objects\Transfers\Charge as ChargeTransfer;
use Osiset\ShopifyApp\Objects\Values\ChargeId;
use Osiset\ShopifyApp\Objects\Values\ChargeReference;
use Osiset\ShopifyApp\Objects\Values\ShopId;
use Osiset\ShopifyApp\Services\ChargeHelper;
/**
* Activates a plan for a shop.
*/
class ActivatePlan
{
/**
* The charge helper.
*
* @var ChargeHelper
*/
protected $chargeHelper;
/**
* Action which cancels the current plan.
*
* @var callable
*/
protected $cancelCurrentPlan;
/**
* Querier for shops.
*
* @var IShopQuery
*/
protected $shopQuery;
/**
* Command for charges.
*
* @var IChargeCommand
*/
protected $chargeCommand;
/**
* Command for shops.
*
* @var IShopCommand
*/
protected $shopCommand;
/**
* Querier for plans.
*
* @var IPlanQuery
*/
protected $planQuery;
/**
* Setup.
*
* @param callable $cancelCurrentPlanAction Action which cancels the current plan.
* @param ChargeHelper $chargeHelper The charge helper.
* @param IShopQuery $shopQuery The querier for shops.
* @param IPlanQuery $planQuery The querier for plans.
* @param IChargeCommand $chargeCommand The commands for charges.
* @param IShopCommand $shopCommand The commands for shops.
*
* @return void
*/
public function __construct(
callable $cancelCurrentPlanAction,
ChargeHelper $chargeHelper,
IShopQuery $shopQuery,
IPlanQuery $planQuery,
IChargeCommand $chargeCommand,
IShopCommand $shopCommand
) {
$this->cancelCurrentPlan = $cancelCurrentPlanAction;
$this->chargeHelper = $chargeHelper;
$this->shopQuery = $shopQuery;
$this->planQuery = $planQuery;
$this->chargeCommand = $chargeCommand;
$this->shopCommand = $shopCommand;
}
/**
* Execution.
* TODO: Rethrow an API exception.
*
* @param ShopId $shopId The shop ID.
* @param PlanId $planId The plan to use.
* @param ChargeReference $chargeRef The charge ID from Shopify.
*
* @return ChargeId
*/
public function __invoke(ShopId $shopId, PlanId $planId, ChargeReference $chargeRef): ChargeId
{
// Get the shop
$shop = $this->shopQuery->getById($shopId);
// Get the plan
$plan = $this->planQuery->getById($planId);
$chargeType = ChargeType::fromNative($plan->getType()->toNative());
// Activate the plan on Shopify
$response = $shop->apiHelper()->activateCharge($chargeType, $chargeRef);
// Cancel the shop's current plan
call_user_func($this->cancelCurrentPlan, $shopId);
// Cancel the existing charge if it exists (happens if someone refreshes during)
$this->chargeCommand->delete($chargeRef, $shopId);
// Create the charge transfer
$transfer = new ChargeTransfer();
$transfer->shopId = $shopId;
$transfer->planId = $planId;
$transfer->chargeReference = $chargeRef;
$transfer->chargeType = $chargeType;
$transfer->chargeStatus = ChargeStatus::fromNative(strtoupper($response['status']));
if ($plan->isType(PlanType::RECURRING())) {
$transfer->activatedOn = new Carbon($response['activated_on']);
$transfer->billingOn = new Carbon($response['billing_on']);
$transfer->trialEndsOn = new Carbon($response['trial_ends_on']);
} else {
$transfer->activatedOn = Carbon::today();
$transfer->billingOn = null;
$transfer->trialEndsOn = null;
}
$transfer->planDetails = $this->chargeHelper->details($plan, $shop);
// Create the charge
$charge = $this->chargeCommand->make($transfer);
$this->shopCommand->setToPlan($shopId, $planId);
event(new PlanActivatedEvent($shop, $plan, $charge));
return $charge;
}
}