-
Notifications
You must be signed in to change notification settings - Fork 3
/
Webhook.php
105 lines (76 loc) · 3.1 KB
/
Webhook.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
<?php
function createWebhookMessage($tr_hash, $server_name, $store_id, $store_ref, $type_tr, $from_add, $dest,$amount){
$time = time();
$base_link = (isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on') ? "https" : "http";
$base_link .= "://";
$base_link .= $_SERVER['HTTP_HOST'];
$link = array (
"href"=>$base_link."/api.php?hash=".$tr_hash,
"method"=>"GET"
);
$amount = array (
'sent'=> "".$amount,
'type'=> $type_tr,
'currency' => $server_name
);
$resources = array (
'id'=>$tr_hash,
'create_time'=> "".$time,
'state'=>'completed',
'store_id' => $store_id,
'reference' => $store_ref,
'links'=>[$link],
'addr_to' => $dest,
'amount'=>$amount
);
if (strlen($from_add)>0) {
$resources['addr_from']=$from_add;
}
$data = array ('id'=>$tr_hash,
'create_time'=>"".$time,
'resource_type'=>'sale',
'event_type'=> 'PAYMENT.SALE.COMPLETED',
'summary'=>'A sale has been completed. The payement has been processed.',
'links'=>[$link],
'resources'=>$resources
);
return $data;
}
// Sign and send message
function sendWebhook($url, $message) {
$private_key_path ='../ComChainKey/comchainwebhook_rsa';
$public_key_url ='https://com-chain.org/comchainwebhook_rsa.pub';
$json_message = json_encode($message);
$hash = crc32($json_message);
$sign = "";
$sign_key = openssl_pkey_get_private(file_get_contents($private_key_path));
if (openssl_sign($hash, $sign, $sign_key)) {
$signed = base64_encode($sign);
$url_list = explode(",", $url);
$passed=true;
foreach ($url_list as $send_url) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $send_url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('COMCHAIN-TRANSMISSION-SIG:'.$signed,
'COMCHAIN-AUTH-ALGO:RSA',
'COMCHAIN-CERT-URL:'.$public_key_url));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($message));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
if (!$response = curl_exec( $ch )){
$passed=false;
}
if ($passed) {
$code = curl_getinfo($ch, CURLINFO_RESPONSE_CODE);
$passed = $code>=200 and $code<300;
}
curl_close($ch);
}
return $passed;
} else {
return false;
}
}
?>