This repository has been archived by the owner on Apr 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
OrderCompleted.php
148 lines (133 loc) · 3.44 KB
/
OrderCompleted.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
<?php
namespace OFFLINE\SnipcartShop\Classes;
use Carbon\Carbon;
use Event;
use OFFLINE\SnipcartShop\Models\Order;
use OFFLINE\SnipcartShop\Models\OrderItem;
class OrderCompleted
{
protected $orderFields = [
'token',
'invoiceNumber',
'currency',
'creationDate',
'modificationDate',
'completionDate',
'status',
'paymentStatus',
'email',
'willBePaidLater',
'shippingAddressSameAsBilling',
'billingAddress',
'shippingAddress',
'creditCardLast4Digits',
'trackingNumber',
'trackingUrl',
'shippingMethod',
'cardHolderName',
'cardType',
'paymentMethod',
'paymentGatewayUsed',
'taxProvider',
'lang',
'refundsAmount',
'adjustedAmount',
'rebateAmount',
'taxes',
'itemsTotal',
'subtotal',
'taxableTotal',
'grandTotal',
'totalWeight',
'totalRebateRate',
'notes',
'customFields',
'shippingEnabled',
'paymentTransactionId',
'metadata',
'ipAddress',
'userId',
'discounts'
];
protected $itemFields = [
'uniqueId',
'orderId',
'name',
'price',
'totalPrice',
'quantity',
'maxQuantity',
'url',
'weight',
'width',
'length',
'height',
'totalWeight',
'description',
'image',
'stackable',
'duplicatable',
'shippable',
'taxable',
'customFields',
'taxes',
'addedOn',
];
public function handle($data)
{
$itemsData = $data['items'];
$orderData = $this->normalizeData($data, $this->orderFields);
$order = Order::firstOrCreate(['token' => $orderData['token']]);
$order->fill($orderData);
$order->save();
$items = [];
foreach ($itemsData as $item) {
$data = $this->normalizeData($item, $this->itemFields);
$data['product_id'] = $item['id'];
$model = new OrderItem();
$model->fill($data);
$items[] = $model;
}
// Remove existing items from this order to prevent
// duplicate items if the webhook is sent a second time
$order->items()->delete();
$order->items()->saveMany($items);
Event::fire('snipcartshop.order.created', $order);
}
/**
* Select relveant fields and convert all keys
* to snake_case.
*
* @param $data
* @param $useFields
*
* @return array
*/
protected function normalizeData($data, array $useFields)
{
$normalized = [];
foreach ($data as $key => $value) {
if (in_array($key, $useFields)) {
$normalized[snake_case($key)] = $value;
}
}
return $this->reformatDates($normalized);
}
/**
* Convert the Snipcart date format to a standard carbon instance.
*
* @param $data
*
* @return mixed
*/
protected function reformatDates($data)
{
foreach (['creation_date', 'modification_date', 'completion_date', 'added_on'] as $date) {
if ( ! array_key_exists($date, $data)) {
continue;
}
$data[$date] = Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $data[$date]);
}
return $data;
}
}