-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathfullfillment_created.php
More file actions
105 lines (86 loc) · 3.11 KB
/
fullfillment_created.php
File metadata and controls
105 lines (86 loc) · 3.11 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
<?php
$SHOPIFY_WEBHOOK_SECRET = "";
$API_KEY = "";
$PASSWORD = "";
$SHARED_SECRET ="";
$STOREFRONT_TOKEN="";
function verifyWebhook($data, $hmacHeader)
{
global $SHOPIFY_WEBHOOK_SECRET;
$calculatedHmac = base64_encode(hash_hmac('sha256', $data, $SHOPIFY_WEBHOOK_SECRET, true));
return ($hmacHeader == $calculatedHmac);
}
function my_log($object){
$req_dump = print_r( $object, true );
file_put_contents( 'request.log', PHP_EOL . $req_dump, FILE_APPEND );
}
/**
* Call Delivery API
* @param $fulfillment [description]
* @return
*/
function callDeliveryApi ($fulfillment)
{
$delivery = new Paymentwall_GenerericApiObject('delivery');
my_log("sending delivery data");
return $delivery->post(prepareDeliveryData($fulfillment));
}
/**
* Prepare Delivery Data
* @param $fulfillment
* @return array
*/
function prepareDeliveryData ($fulfillment)
{
$data = array(
'payment_id' => $fulfillment['order_id'],
'merchant_reference_id' => $fulfillment['order_id'],
'status' => 'delivered',
'estimated_delivery_datetime' => $fulfillment['created_at'],
'estimated_update_datetime' => $fulfillment['updated_at'],
'refundable' => true, //change to false if you don't support refund
'details' => 'Item will be delivered by ' . $fulfillment['created_at'],
'shipping_address[email]' => $fulfillment['email'],
'shipping_address[firstname]' => $fulfillment['destination']['first_name'],
'shipping_address[lastname]' => $fulfillment['destination']['last_name'],
'shipping_address[country]' => $fulfillment['destination']['country'],
'shipping_address[street]' => $fulfillment['destination']['address1'],
'shipping_address[state]' => $fulfillment['destination']['province_code'] ? $fulfillment['destination']['province_code'] : 'NA',
'shipping_address[phone]' => $fulfillment['destination']['phone'] ? $fulfillment['destination']['phone'] : 'NA',
'shipping_address[zip]' => $fulfillment['destination']['zip'],
'shipping_address[city]' => $fulfillment['destination']['city'],
'carrier_type' => $fulfillment['tracking_company'],
'reason' => 'none',
'carrier_trackind_id' => $fulfillment['tracking_number'],
'is_test' => 1 // change to 0 iff you're on live mode
);
if(!empty($fulfillment['destination'])) {
$data['type'] = 'physical';
} else {
$data['type'] = 'digital';
}
return $data;
}
header('Content-Type: application/json');
$request = file_get_contents('php://input');
my_log($request);
$hmacHeader = $_SERVER['HTTP_X_SHOPIFY_HMAC_SHA256'];
$verified = verifyWebhook($request, $hmacHeader);
if (false === $verified)
{
my_log('HMAC HASH not verified, exiting.');
exit;
}else{
my_log("webhook validated");
if (!class_exists('Paymentwall_Config')) {
require_once(__DIR__ . '/paymentwall/lib/paymentwall.php');
}
Paymentwall_Config::getInstance()->set(array(
'private_key' => $PASSWORD,
'public_key' => $SHARED_SECRET
));
$webhook = $request;
$fulfillment = json_decode($webhook, TRUE);
callDeliveryApi($fulfillment);
}
?>