-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathnotification_handler.php
169 lines (131 loc) · 6.83 KB
/
notification_handler.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
<?php
require_once 'vendor/autoload.php';
require_once dirname(__FILE__) . '/vendor/midtrans/midtrans-php/Midtrans.php';
// PHP Error Display
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
use \PHPMailer\PHPMailer\PHPMailer;
$dotenv = Dotenv\Dotenv::createImmutable(__DIR__);
$dotenv->load();
$dotenv->required('MIDTRANS_SERVER_KEY')->notEmpty();
$dotenv->required('IS_PRODUCTION')->notEmpty();
\Midtrans\Config::$isProduction = filter_var($_ENV['IS_PRODUCTION'], FILTER_VALIDATE_BOOLEAN);
\Midtrans\Config::$serverKey = $_ENV['MIDTRANS_SERVER_KEY'];
$notif = new \Midtrans\Notification();
$transaction_status = $notif->transaction_status;
$payment_type = $notif->payment_type;
$order_id = $notif->order_id;
$payment_date = $notif->transaction_time;
$amount = $notif->gross_amount;
$message = 'ok';
$mail = new PHPMailer(true);
// Mail Configuration
$mail->SMTPDebug = 0;
$mail->isSMTP();
$mail->Host = 'in-v3.mailjet.com';
$mail->SMTPAuth = true;
$mail->Username = $_ENV['MAILTRAP_USERNAME'];
$mail->Password = $_ENV['MAILTRAP_PASSWORD'];
$mail->SMTPSecure = 'tls';
$mail->Port = 587;
require dirname(__FILE__) . '/model/Payments.php';
$objPay = new Payments;
$objPay->set_order_id($order_id);
$objPay->set_amount($amount);
$objPay->set_payment_date($payment_date);
$objPay->set_payment_type($payment_type);
$objPay->set_status($transaction_status);
$check = $objPay->payment_check();
(!$check) ? $objPay->create_payment() : $objPay->update_payment_status();
$payment = $objPay->get_total_amount();
$total_payment_amount = $payment['amount'];
require dirname(__FILE__) . '/model/Invoices.php';
$objInv = new Invoices;
$objInv->set_order_id($order_id);
$invoice = $objInv->get_invoice();
$invoice_amount = $invoice['amount'];
$mail->setFrom($_ENV['MAILTRAP_EMAIL'], 'Lumintu Logic');
$mail->addReplyTo($_ENV['MAILTRAP_EMAIL'], '<no-reply>');
$mail->addAddress($invoice['email'], $invoice['first_name'] . " " . $invoice['last_name']);
if ($transaction_status == 'capture') {
// For credit card transaction, we need to check whether transaction is challenge by FDS or not
if ($type == 'credit_card') {
if ($fraud == 'challenge') {
// TODO set payment status in merchant's database to 'Challenge by FDS'
// TODO merchant should decide whether this transaction is authorized or not in MAP
$message = "Transaction order_id: " . $order_id . " is challenged by FDS";
} else {
// TODO set payment status in merchant's database to 'Success'
$message = "Transaction order_id: " . $order_id . " successfully captured using " . $type;
}
}
if($total_payment_amount == $invoice_amount) {
$objInv->set_status('paid');
$objInv->update_status();
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Pembayaran berhasil untuk order_id = ' . $order_id;
$mail->Body = file_get_contents('templates/email_success.html');
$key = array('{order_id}', '{product_id}', '{price}', '{status}', '{payment_method}');
$val = array($order_id, $invoice['item_id'], $amount, $transaction_status, $payment_type);
$mail->Body = str_replace($key, $val, $mail->Body);
$mail->send();
}
} elseif ($transaction_status == 'settlement') {
// TODO set payment status in merchant's database to 'Settlement'
if($total_payment_amount == $invoice_amount) {
$objInv->set_status('paid');
$objInv->update_status();
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Pembayaran berhasil untuk order_id = ' . $order_id;
$mail->Body = file_get_contents('templates/email_success.html');
$key = array('{order_id}', '{product_id}', '{price}', '{status}', '{payment_method}');
$val = array($order_id, $invoice['item_id'], $amount, $transaction_status, $payment_type);
$mail->Body = str_replace($key, $val, $mail->Body);
$mail->send();
}
$message = "Transaction order_id: " . $order_id . " successfully transfered using " . $type . " Pada tanggal " . $tanggal . "Dibayar dengan uang ". $matauang;
} elseif ($transaction_status == 'pending') {
// TODO set payment status in merchant's database to 'Pending'
$message = "Waiting customer to finish transaction order_id: " . $order_id . " using " . $type;
} elseif ($transaction_status == 'deny') {
// TODO set payment status in merchant's database to 'Denied'
$objInv->set_status('unpaid');
$objInv->update_status();
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Pembayaran gagal untuk order_id = ' . $order_id;
$mail->Body = file_get_contents('templates/email_failed.html');
$key = array('{order_id}', '{product_id}', '{price}', '{status}', '{payment_method}');
$val = array($order_id, $invoice['item_id'], $amount, $transaction_status, $payment_type);
$mail->Body = str_replace($key, $val, $mail->Body);
$mail->send();
$message = "Payment using " . $type . " for transaction order_id: " . $order_id . " is denied.";
} elseif ($transaction_status == 'expire') {
// TODO set payment status in merchant's database to 'expire'
$objInv->set_status('unpaid');
$objInv->update_status();
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Pembayaran gagal untuk order_id = ' . $order_id;
$mail->Body = file_get_contents('templates/email_failed.html');
$key = array('{order_id}', '{product_id}', '{price}', '{status}', '{payment_method}');
$val = array($order_id, $invoice['item_id'], $amount, $transaction_status, $payment_type);
$mail->Body = str_replace($key, $val, $mail->Body);
$mail->send();
$message = "Payment using " . $type . " for transaction order_id: " . $order_id . " is expired.";
} elseif ($transaction_status == 'cancel') {
// TODO set payment status in merchant's database to 'Denied'
$objInv->set_status('unpaid');
$objInv->update_status();
$mail->isHTML(true); //Set email format to HTML
$mail->Subject = 'Pembayaran gagal untuk order_id = ' . $order_id;
$mail->Body = file_get_contents('templates/email_failed.html');
$key = array('{order_id}', '{product_id}', '{price}', '{status}', '{payment_method}');
$val = array($order_id, $invoice['item_id'], $amount, $transaction_status, $payment_type);
$mail->Body = str_replace($key, $val, $mail->Body);
$mail->send();
$message = "Payment using " . $type . " for transaction order_id: " . $order_id . " is canceled.";
}
// $filename = $order_id . ".txt";
// $dirpath = 'log';
// is_dir($dirpath) || mkdir($dirpath, 0777, true);
// json_decode( file_put_contents($dirpath . "/" . $filename, $message));