Skip to content

Commit 1a1bbb5

Browse files
committed
Merge branch 'chore/bump-php-min-8.1' of github.com:enmaboya/laravel-shopify; branch 'master' of github.com:Kyon147/laravel-shopify
2 parents 1f0a5b6 + 52dabf2 commit 1a1bbb5

28 files changed

+163
-796
lines changed

src/Actions/ActivatePlan.php

Lines changed: 9 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -18,116 +18,45 @@
1818
use Osiset\ShopifyApp\Objects\Values\ShopId;
1919
use Osiset\ShopifyApp\Services\ChargeHelper;
2020

21-
/**
22-
* Activates a plan for a shop.
23-
*/
2421
class 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

src/Actions/ActivateUsageCharge.php

Lines changed: 12 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -13,89 +13,45 @@
1313
use Osiset\ShopifyApp\Objects\Values\ShopId;
1414
use Osiset\ShopifyApp\Services\ChargeHelper;
1515

16-
/**
17-
* Activates a usage charge for a shop.
18-
*/
1916
class ActivateUsageCharge
2017
{
21-
/**
22-
* The helper for charges.
23-
*
24-
* @var ChargeHelper
25-
*/
26-
protected $chargeHelper;
27-
28-
/**
29-
* Command for charges.
30-
*
31-
* @var IChargeCommand
32-
*/
33-
protected $chargeCommand;
34-
35-
/**
36-
* Querier for shops.
37-
*
38-
* @var IShopQuery
39-
*/
40-
protected $shopQuery;
41-
42-
/**
43-
* Setup.
44-
*
45-
* @param ChargeHelper $chargeHelper The helper for charges.
46-
* @param IChargeCommand $chargeCommand The commands for charges.
47-
* @param IShopQuery $shopQuery The querier for shops.
48-
*
49-
* @return void
50-
*/
5118
public function __construct(
52-
ChargeHelper $chargeHelper,
53-
IChargeCommand $chargeCommand,
54-
IShopQuery $shopQuery
19+
protected ChargeHelper $chargeHelper,
20+
protected IChargeCommand $chargeCommand,
21+
protected IShopQuery $shopQuery
5522
) {
56-
$this->chargeHelper = $chargeHelper;
57-
$this->chargeCommand = $chargeCommand;
58-
$this->shopQuery = $shopQuery;
5923
}
6024

6125
/**
62-
* Execute.
6326
* TODO: Rethrow an API exception.
6427
*
65-
* @param ShopId $shopId The shop ID.
66-
* @param UsageChargeDetailsTransfer $ucd The usage charge details (without charge ID).
67-
*
6828
* @throws ChargeNotRecurringException
69-
*
70-
* @return ChargeId|bool
7129
*/
72-
public function __invoke(ShopId $shopId, UsageChargeDetailsTransfer $ucd)
30+
public function __invoke(ShopId $shopId, UsageChargeDetailsTransfer $ucd): ChargeId|bool
7331
{
74-
// Get the shop
7532
$shop = $this->shopQuery->getById($shopId);
76-
7733
// Ensure we have a recurring charge
7834
$currentCharge = $this->chargeHelper->chargeForPlan($shop->plan->getId(), $shop);
35+
7936
if (! $currentCharge->isType(ChargeType::RECURRING())) {
8037
throw new ChargeNotRecurringException('Can only create usage charges for recurring charge.');
8138
}
8239

8340
// Create the usage charge
8441
$ucd->chargeReference = $currentCharge->getReference();
8542
$response = $shop->apiHelper()->createUsageCharge($ucd);
43+
8644
if (! $response) {
8745
// Could not make usage charge, limit possibly reached
8846
return false;
8947
}
9048

91-
// Create the transfer
92-
$uct = new UsageChargeTransfer();
93-
$uct->shopId = $shopId;
94-
$uct->planId = $shop->plan->getId();
95-
$uct->chargeReference = ChargeReference::fromNative((int) $response['id']);
96-
$uct->details = $ucd;
49+
$transfer = new UsageChargeTransfer();
50+
$transfer->shopId = $shopId;
51+
$transfer->planId = $shop->plan->getId();
52+
$transfer->chargeReference = ChargeReference::fromNative((int) $response['id']);
53+
$transfer->details = $ucd;
9754

98-
// Save the usage charge
99-
return $this->chargeCommand->makeUsage($uct);
55+
return $this->chargeCommand->makeUsage($transfer);
10056
}
10157
}

src/Actions/AfterAuthorize.php

Lines changed: 3 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -8,55 +8,23 @@
88
use Osiset\ShopifyApp\Contracts\ShopModel as IShopModel;
99
use Osiset\ShopifyApp\Util;
1010

11-
/**
12-
* Run after authentication jobs.
13-
*/
1411
class AfterAuthorize
1512
{
16-
/**
17-
* Querier for shops.
18-
*
19-
* @var IShopQuery
20-
*/
21-
protected $shopQuery;
22-
23-
/**
24-
* Setup.
25-
*
26-
* @param IShopQuery $shopQuery The querier for the shop.
27-
*
28-
* @return void
29-
*/
30-
public function __construct(IShopQuery $shopQuery)
13+
public function __construct(protected IShopQuery $shopQuery)
3114
{
32-
$this->shopQuery = $shopQuery;
3315
}
3416

3517
/**
36-
* Execution.
3718
* TODO: Rethrow an API exception.
38-
*
39-
* @param ShopIdValue $shopId The shop ID.
40-
*
41-
* @return bool
4219
*/
4320
public function __invoke(ShopIdValue $shopId): bool
4421
{
45-
/**
46-
* Fires the job.
47-
*
48-
* @param array $config The job's configuration.
49-
* @param IShopModel $shop The shop instance.
50-
*
51-
* @return bool
52-
*/
5322
$fireJob = function (array $config, IShopModel $shop): bool {
5423
$job = Arr::get($config, 'job');
24+
5525
if (Arr::get($config, 'inline', false)) {
56-
// Run this job immediately
5726
$job::dispatchSync($shop);
5827
} else {
59-
// Run later
6028
$job::dispatch($shop)
6129
->onConnection(Util::getShopifyConfig('job_connections')['after_authenticate'])
6230
->onQueue(Util::getShopifyConfig('job_queues')['after_authenticate']);
@@ -65,25 +33,19 @@ public function __invoke(ShopIdValue $shopId): bool
6533
return true;
6634
};
6735

68-
// Get the shop
6936
$shop = $this->shopQuery->getById($shopId);
70-
71-
// Grab the jobs config
7237
$jobsConfig = Util::getShopifyConfig('after_authenticate_job');
38+
7339
if (Arr::has($jobsConfig, 0)) {
74-
// We have multi-jobs
7540
foreach ($jobsConfig as $jobConfig) {
76-
// We have a job, pass the shop object to the constructor
7741
$fireJob($jobConfig, $shop);
7842
}
7943

8044
return true;
8145
} elseif (Arr::has($jobsConfig, 'job')) {
82-
// We have a single job
8346
return $fireJob($jobsConfig, $shop);
8447
}
8548

86-
// We have no jobs
8749
return false;
8850
}
8951
}

0 commit comments

Comments
 (0)