-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathFirstPaymentHandler.php
More file actions
178 lines (148 loc) · 5.07 KB
/
Copy pathFirstPaymentHandler.php
File metadata and controls
178 lines (148 loc) · 5.07 KB
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
<?php
namespace Laravel\Cashier\FirstPayment;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\DB;
use Laravel\Cashier\Cashier;
use Laravel\Cashier\Events\MandateUpdated;
use Laravel\Cashier\FirstPayment\Actions\BaseAction;
use Laravel\Cashier\Order\OrderItemCollection;
use Mollie\Api\Resources\Payment as MolliePayment;
class FirstPaymentHandler
{
/** @var \Illuminate\Database\Eloquent\Model */
protected $owner;
/** @var \Mollie\Api\Resources\Payment */
protected $molliePayment;
/** @var \Illuminate\Support\Collection */
protected $actions;
/** @var bool */
protected $alreadyProcessed = false;
/**
* FirstPaymentHandler constructor.
*
* @param \Mollie\Api\Resources\Payment $molliePayment
*/
public function __construct(MolliePayment $molliePayment)
{
$this->molliePayment = $molliePayment;
$this->owner = $this->extractOwner();
$this->actions = $this->extractActions();
}
/**
* Execute all actions for the mandate payment and return the created Order.
*
* @return \Laravel\Cashier\Order\Order
*/
public function execute()
{
$this->alreadyProcessed = false;
$order = DB::transaction(function () {
$localPayment = Cashier::$paymentModel::query()
->where('mollie_payment_id', $this->molliePayment->id)
->lockForUpdate()
->first();
if ($localPayment?->order) {
$this->alreadyProcessed = true;
return $localPayment->order;
}
$this->owner->mollie_mandate_id = $this->molliePayment->mandateId;
$this->owner->save();
$orderItems = $this->executeActions();
$order = Cashier::$orderModel::createProcessedFromItems($orderItems, [
'mollie_payment_id' => $this->molliePayment->id,
'mollie_payment_status' => $this->molliePayment->status,
]);
// It's possible a payment from Cashier v1 is not yet tracked in the Cashier database.
// In that case we create a record here.
$localPayment = $localPayment ?: Cashier::$paymentModel::findByMolliePaymentOrCreate(
$this->molliePayment,
$this->owner,
$this->actions->all()
);
$localPayment->update([
'order_id' => $order->id,
'mollie_payment_status' => $this->molliePayment->status,
'mollie_mandate_id' => $this->molliePayment->mandateId,
]);
return $order;
});
if (! $this->alreadyProcessed) {
event(new MandateUpdated($this->owner, $this->molliePayment));
}
return $order;
}
/**
* Determine if execute() returned an already processed first payment.
*
* @return bool
*/
public function wasAlreadyProcessed()
{
return $this->alreadyProcessed;
}
/**
* Fetch the owner model using the mandate payment metadata.
*
* @return \Illuminate\Database\Eloquent\Model
*/
protected function extractOwner()
{
$ownerType = $this->molliePayment->metadata->owner->type;
$ownerID = $this->molliePayment->metadata->owner->id;
$ownerClass = Relation::getMorphedModel($ownerType) ?? $ownerType;
return $ownerClass::query()->findOrFail($ownerID);
}
/**
* Build the action objects from the payment metadata.
*
* @return \Illuminate\Support\Collection
*/
protected function extractActions()
{
$payment = Cashier::$paymentModel::findByPaymentId($this->molliePayment->id);
$actions = $payment?->first_payment_actions;
/*
* For legacy reasons, the first_payment_actions value is (de)serialized on the Payment model as an object
* instead of an array. This is handled by `collect()` just fine.
*/
return collect($actions)
->map(function ($action) {
return $action->handler::createFromPayload(
object_to_array_recursive($action),
$this->owner
);
});
}
/**
* Execute the Actions and return a collection of the resulting OrderItems.
* These OrderItems are already paid for using the mandate payment.
*
* @return \Laravel\Cashier\Order\OrderItemCollection
*/
protected function executeActions()
{
$orderItems = new OrderItemCollection();
$this->actions->each(function (BaseAction $action) use (&$orderItems) {
$orderItems = $orderItems->concat($action->execute());
});
return $orderItems;
}
/**
* Retrieve the owner object.
*
* @return \Illuminate\Database\Eloquent\Model
*/
public function getOwner()
{
return $this->owner;
}
/**
* Retrieve all Action objects.
*
* @return \Illuminate\Support\Collection
*/
public function getActions()
{
return $this->actions;
}
}