-
Notifications
You must be signed in to change notification settings - Fork 5
/
callback.php
69 lines (47 loc) · 1.8 KB
/
callback.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
<?php
require_once 'lib/PayBearOrder.php';
require_once 'lib/PayBearTxn.php';
require_once('lib/CmsOrder.php');
$payBearOrder = new PayBearOrder();
$payment_txn = new PayBearTxn();
$cms_order = new CmsOrder();
$order_id = $_GET['order_id'];
if (empty($order_id)) return;
$payment = $payBearOrder->findByOrderId($order_id);
if (empty($payment)) return;
$orderAmount = $payment->amount;
$cmsOrder = $cms_order->findByIncrementId($order_id);
$data = file_get_contents('php://input');
if ($data) {
$params = json_decode($data);
$invoice = $payment->invoice;
$orderTotal = $payment->amount;
//save number of confirmations to DB: $params->confirmations
//compare $invoice with one saved in the database to ensure callback is legitimate
if ($params->invoice == $invoice) {
$newPayment = $payment_txn->isNewOrder($order_id);
$payment->confirmations = $params->confirmations;
if ($newPayment) {
$payment->paid_at = date('Y-m-d H:i:s');
}
$payment->save();
$payment_txn->setTxn($params, $order_id);
$total_confirmed = $payment_txn->getTotalConfirmed($order_id, $payment->max_confirmation);
if ($params->confirmations >= $params->maxConfirmations) {
//compare $amountPaid with order total
if ($total_confirmed >= $orderAmount) {
//mark the order as paid
$cmsOrder->status = 'Complete';
$cmsOrder->save();
echo $invoice; //stop further callbacks
}else{
$cmsOrder->status = 'Order mispayment';
$cmsOrder->save();
}
} else {
$cmsOrder->status = 'Waiting for confirmations';
$cmsOrder->save();
die("waiting for confirmations");
}
}
}