-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebhook.php
75 lines (62 loc) · 1.88 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
<?php
require "init.php";
$merchant_id = "91f3379c88bafee6e78be9d99ffa34d5";
$ipn_secret = "1@3$5^7*";
$debug_email = "[email protected]";
$txn_id = $_POST['txn_id'];
$payment = Payment::where("gateway_id", $txn_id)->first();
$order_currency = $payment->to_currency; //BTC
$order_total = $payment->amount; //BTC
function edie($error_msg)
{
global $debug_email;
$report = "ERROR : " . $error_msg . "\n\n";
$report.= "POST DATA\n\n";
foreach ($_POST as $key => $value) {
$report .= "|$k| = |$v| \n";
}
mail($debug_email, "Payment Error", $report);
die($error_msg);
}
if (!isset($_POST['ipn_mode']) || $_POST['ipn_mode'] != 'hmac') {
edie("IPN Mode is not HMAC");
}
if (!isset($_SERVER['HTTP_HMAC']) || empty($_SERVER['HTTP_HMAC'])) {
edie("No HMAC Signature Sent.");
}
$request = file_get_contents('php://input');
if ($request === false || empty($request)) {
edie("Error in reading Post Data");
}
if (!isset($_POST['merchant']) || $_POST['merchant'] != trim($merchant_id)) {
edie("No or incorrect merchant id.");
}
$hmac = hash_hmac("sha512", $request, trim($ipn_secret));
if (!hash_equals($hmac, $_SERVER['HTTP_HMAC'])) {
edie("HMAC signature does not match.");
}
$amount1 = floatval($_POST['amount1']); //IN USD
$amount2 = floatval($_POST['amount2']); //IN BTC
$currency1 = $_POST['currency1']; //USD
$currency2 = $_POST['currency2']; //BTC
$status = intval($_POST['status']);
if ($currency2 != $order_currency) {
edie("Currency Mismatch");
}
if ($amount2 < $order_total) {
edie("Amount is lesser than order total");
}
if ($status >= 100 || $status == 2) {
// Payment is complete
$payment->status = "success";
$payment->save();
} else if ($status < 0) {
// Payment Error
$payment->status = "error";
$payment->save();
} else {
// Payment Pending
$payment->status = "pending";
$payment->save();
}
die("IPN OK");