376 lines
15 KiB
PHP
376 lines
15 KiB
PHP
<?php
|
|
|
|
namespace Modules\Linksehat\Http\Controllers\Api;
|
|
|
|
use App\Helpers\Helper;
|
|
use App\Notifications\SendNotification;
|
|
use App\Models\Organization;
|
|
use App\Models\Speciality;
|
|
use App\Models\Livechat;
|
|
use App\Models\Channel;
|
|
use App\Models\Message;
|
|
use App\Models\UserChannel;
|
|
use App\Models\User as UserAso;
|
|
use App\Models\OLDLMS\User;
|
|
use Illuminate\Contracts\Support\Renderable;
|
|
use Illuminate\Database\Eloquent\Builder;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Exception;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use Illuminate\Support\Facades\Redirect;
|
|
|
|
class DuitkuController extends Controller
|
|
{
|
|
public function configuration()
|
|
{
|
|
$duitkuConfig = new \Duitku\Config(env('API_KEY_DUITKU'), env('CODE_MERCHANT_DUITKU'));
|
|
// false for production mode
|
|
// true for sandbox mode
|
|
$duitkuConfig->setSandboxMode(true);
|
|
// set sanitizer (default : true)
|
|
$duitkuConfig->setSanitizedMode(false);
|
|
// set log parameter (default : true)
|
|
$duitkuConfig->setDuitkuLogs(false);
|
|
return $duitkuConfig;
|
|
}
|
|
|
|
public function createInvoice(Request $request)
|
|
{
|
|
$data = [
|
|
'paymentMethod' => $request->paymentMethod,
|
|
'paymentAmount' => $request->paymentAmount,
|
|
'email' => $request->email,
|
|
'phoneNumber' => $request->phoneNumber,
|
|
'productDetails' => $request->productDetails,
|
|
'merchantOrderId' => $request->merchantOrderId,
|
|
'additionalParam' => $request->additionalParam,
|
|
'merchantUserInfo' => $request->merchantUserInfo,
|
|
'customerVaName' => $request->customerVaName,
|
|
// 'callbackUrl' => $request->callbackUrl,
|
|
// 'returnUrl' => $request->returnUrl,
|
|
// 'expiryPeriod' => $request->expiryPeriod,
|
|
'firstName' => $request->firstName,
|
|
'lastName' => $request->lastName,
|
|
'alamat' => $request->alamat,
|
|
'city' => $request->city,
|
|
'postalCode' => $request->postalCode,
|
|
// 'countryCode' => $request->countryCode
|
|
];
|
|
$validator = Validator::make($request->all(), [
|
|
'paymentMethod' => 'nullable',
|
|
'paymentAmount' => 'required',
|
|
'email' => 'required|email',
|
|
'phoneNumber' => 'nullable',
|
|
'productDetails' => 'required',
|
|
'merchantOrderId' => 'required',
|
|
'additionalParam' => 'nullable',
|
|
'merchantUserInfo' => 'nullable',
|
|
'customerVaName' => 'required',
|
|
// 'callbackUrl' => 'required',
|
|
// 'returnUrl' => 'nullable',
|
|
// 'expiryPeriod' => 'required',
|
|
'firstName' => 'required',
|
|
'lastName' => 'required',
|
|
'alamat' => 'required',
|
|
'city' => 'required',
|
|
'postalCode' => 'required',
|
|
// 'countryCode' => 'required'
|
|
|
|
], [
|
|
'paymentAmount.required' => 'Jumlah pembayaran harus diisi',
|
|
'email.required' => 'Email harus diisi',
|
|
'email.email' => 'Format email salah',
|
|
'productDetails.required' => 'Judul pembayaran harus diisi',
|
|
'merchantOrderId.required' => 'Order ID harus diisi',
|
|
'customerVaName.required' => 'Nama panggilan pelanggan harus diisi',
|
|
'firstName.required' => 'Nama depan pelanggan harus diisi',
|
|
'lastName.required' => 'Nama belakang pelanggan harus diisi',
|
|
'alamat.required' => 'Alamat pelanggan harus diisi',
|
|
'city.required' => 'Kota pelanggan harus diisi',
|
|
'postalCode.required' => 'Kode pos pelanggan harus diisi',
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
{
|
|
return Helper::responseJson(
|
|
data: $data,
|
|
status: 'Bad Request',
|
|
statusCode: 400,
|
|
message: $validator->errors()
|
|
);
|
|
}
|
|
else
|
|
{
|
|
#CONTOH DARI DUITKU
|
|
// $paymentMethod = ""; // PaymentMethod list => https://docs.duitku.com/pop/id/#payment-method
|
|
// $paymentAmount = 10000; // Amount
|
|
// $email = "customer@gmail.com"; // your customer email
|
|
// $phoneNumber = "081234567890"; // your customer phone number (optional)
|
|
// $productDetails = "Test Payment";
|
|
// $merchantOrderId = "2"; // from merchant, unique
|
|
// $additionalParam = ''; // optional
|
|
// $merchantUserInfo = ''; // optional
|
|
// $customerVaName = 'John Doe'; // display name on bank confirmation display
|
|
// $callbackUrl = 'http://YOUR_SERVER/callback'; // url for callback
|
|
// $returnUrl = 'http://YOUR_SERVER/return'; // url for redirect
|
|
// $expiryPeriod = 60; // set the expired time in minutes
|
|
|
|
// // Customer Detail
|
|
// $firstName = "John";
|
|
// $lastName = "Doe";
|
|
|
|
// // Address
|
|
// $alamat = "Jl. Kembangan Raya";
|
|
// $city = "Jakarta";
|
|
// $postalCode = "11530";
|
|
// $countryCode = "ID";
|
|
|
|
$paymentMethod = $request->paymentMethod; // PaymentMethod list => https://docs.duitku.com/pop/id/#payment-method
|
|
$paymentAmount = $request->paymentAmount; // Amount
|
|
$email = $request->email; // your customer email
|
|
$phoneNumber = $request->phoneNumber; // your customer phone number (optional)
|
|
$productDetails = $request->productDetails;
|
|
$merchantOrderId = $request->merchantOrderId; // from merchant, unique
|
|
$additionalParam = $request->additionalParam; // optional
|
|
$merchantUserInfo = $request->merchantUserInfo; // optional
|
|
$customerVaName = $request->customerVaName; // display name on bank confirmation display
|
|
$callbackUrl = env('APP_URL').'/api/linksehat/callback-duitku'; // url for callback
|
|
$returnUrl = env('APP_URL').'/api/linksehat/redirect-duitku';; // url for redirect
|
|
$expiryPeriod = 60; // set the expired time in minutes
|
|
|
|
// Customer Detail
|
|
$firstName = $request->firstName;
|
|
$lastName = $request->lastName;
|
|
|
|
// Address
|
|
$alamat = $request->alamat;
|
|
$city = $request->city;
|
|
$postalCode = $request->postalCode;
|
|
$countryCode = "ID";
|
|
|
|
$address = array(
|
|
'firstName' => $firstName,
|
|
'lastName' => $lastName,
|
|
'address' => $alamat,
|
|
'city' => $city,
|
|
'postalCode' => $postalCode,
|
|
'phone' => $phoneNumber,
|
|
'countryCode' => $countryCode
|
|
);
|
|
|
|
$customerDetail = array(
|
|
'firstName' => $firstName,
|
|
'lastName' => $lastName,
|
|
'email' => $email,
|
|
'phoneNumber' => $phoneNumber,
|
|
'billingAddress' => $address,
|
|
'shippingAddress' => $address
|
|
);
|
|
|
|
// Item Details
|
|
$item1 = array(
|
|
'name' => $productDetails,
|
|
'price' => $paymentAmount,
|
|
'quantity' => 1
|
|
);
|
|
|
|
$itemDetails = array(
|
|
$item1
|
|
);
|
|
|
|
$params = array(
|
|
'paymentAmount' => $paymentAmount,
|
|
'merchantOrderId' => $merchantOrderId,
|
|
'productDetails' => $productDetails,
|
|
'additionalParam' => $additionalParam,
|
|
'merchantUserInfo' => $merchantUserInfo,
|
|
'customerVaName' => $customerVaName,
|
|
'email' => $email,
|
|
'phoneNumber' => $phoneNumber,
|
|
'itemDetails' => $itemDetails,
|
|
'customerDetail' => $customerDetail,
|
|
'callbackUrl' => $callbackUrl,
|
|
'returnUrl' => $returnUrl,
|
|
'expiryPeriod' => $expiryPeriod
|
|
);
|
|
$duitkuConfig = $this->configuration();
|
|
try {
|
|
// createInvoice Request
|
|
$responseDuitkuPop = \Duitku\Pop::createInvoice($params, $duitkuConfig);
|
|
|
|
header('Content-Type: application/json');
|
|
echo $responseDuitkuPop;
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
public function paymentMethod(Request $request)
|
|
{
|
|
$duitkuConfig = $this->configuration();
|
|
try {
|
|
$paymentAmount = "10000"; //"YOUR_AMOUNT";
|
|
$paymentMethodList = \Duitku\Pop::getPaymentMethod($paymentAmount, $duitkuConfig);
|
|
|
|
header('Content-Type: application/json');
|
|
echo $paymentMethodList;
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function checkStatus(Request $request)
|
|
{
|
|
$duitkuConfig = $this->configuration();
|
|
$data = [
|
|
'merchantOrderId' => $request->merchantOrderId
|
|
];
|
|
$validator = Validator::make($request->all(), [
|
|
'merchantOrderId' => 'required',
|
|
], [
|
|
'merchantOrderId.required' => 'Order ID harus diisi',
|
|
]);
|
|
|
|
if ($validator->fails())
|
|
{
|
|
return Helper::responseJson(
|
|
data: $data,
|
|
status: 'Bad Request',
|
|
statusCode: 400,
|
|
message: $validator->errors()
|
|
);
|
|
}
|
|
else
|
|
{
|
|
try {
|
|
$merchantOrderId = $request->merchantOrderId;
|
|
$transactionList = \Duitku\Pop::transactionStatus($merchantOrderId, $duitkuConfig);
|
|
|
|
header('Content-Type: application/json');
|
|
$transaction = json_decode($transactionList);
|
|
|
|
// var_dump($transactionList);
|
|
|
|
if ($transaction->statusCode == "00") {
|
|
// Action Success
|
|
} else if ($transaction->statusCode == "01") {
|
|
// Action Pending
|
|
} else {
|
|
// Action Failed Or Expired
|
|
}
|
|
echo $transaction->statusCode;
|
|
} catch (Exception $e) {
|
|
echo $e->getMessage();
|
|
}
|
|
}
|
|
}
|
|
|
|
public function callback(Request $request)
|
|
{
|
|
$duitkuConfig = $this->configuration();
|
|
try {
|
|
$callback = \Duitku\Pop::callback($duitkuConfig);
|
|
|
|
header('Content-Type: application/json');
|
|
$notif = json_decode($callback);
|
|
|
|
// $notif = $request; //ini untuk di local
|
|
// $callback = $notif; //ini untuk di local
|
|
|
|
DB::table('api_logs')
|
|
->insert([
|
|
'type' => 'in',
|
|
'target' => env('APP_URL').'/api/linksehat/callback-duitku',
|
|
'request' => $callback,
|
|
'created_by' => auth()->check() ? auth()->user()->id : null,
|
|
'created_at' => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
if ($notif->resultCode == "00") { // berhasil melakukan pembayaran
|
|
// Action Success
|
|
$livechat = Livechat::where('uuid', $notif->merchantOrderId)->first();
|
|
// Update status pembayaran
|
|
$livechat->payment_method = $notif->paymentCode;
|
|
$livechat->status = 5; // success payment
|
|
// Update start chat
|
|
$livechat->start_date = date('Y-m-d H:i:s');
|
|
$livechat->save();
|
|
|
|
// Send Notification
|
|
$doctorId = $livechat->doctor_id;
|
|
$userDokter = UserAso::where('person_id',$doctorId)->first();
|
|
$title = 'Payment Succes Livechat';
|
|
$patient = User::where('nID', $livechat->patient_id)->first();
|
|
$body = 'Payment Succes Livechat from ' . $patient->sFirstName . ' ' . $patient->sLastName;
|
|
$channel = Channel::where([
|
|
'member_id' => $livechat->patient_id,
|
|
'doctor_id' => $livechat->doctor_id
|
|
])->first();
|
|
$dataNotif = [
|
|
'channel_id' => (string) $channel->id,
|
|
'livechat_id' => (string) $livechat->id,
|
|
'type' => 'success-payment'
|
|
];
|
|
|
|
$question = $livechat->descriptions;
|
|
// Ambil data dari request
|
|
$message = Message::create([
|
|
'content' => $question,
|
|
'from_user' => $livechat->patient_id,
|
|
'channel_id' => $channel->id,
|
|
'type' => 'first_chat'
|
|
]);
|
|
|
|
$userDokter->notify(new SendNotification($title, $body, $dataNotif));
|
|
$patient->notify(new SendNotification($title, $body, $dataNotif));
|
|
|
|
// Berikan respons yang sesuai ke klien
|
|
return response()->json(['message' => 'Channel created successfully', 'channel' => $channel]);
|
|
|
|
} else if ($notif->resultCode == "01") {
|
|
// Action Failed
|
|
$livechat = Livechat::where('uuid', $notif->merchantOrderId)->first();
|
|
// Update status pembayaran
|
|
$livechat->payment_method = $notif->paymentCode;
|
|
$livechat->status = 7; // failed payment
|
|
$livechat->save();
|
|
|
|
// Send Notification
|
|
$doctorId = $livechat->doctor_id;
|
|
$userDokter = UserAso::where('person_id',$doctorId)->first();
|
|
$title = 'Payment Failed Livechat';
|
|
$patient = User::where('nID', $livechat->patient_id)->first();
|
|
$body = 'Payment Failed Livechat from ' . $patient->sFirstName . ' ' . $patient->sLastName;
|
|
$channel = Channel::where([
|
|
'member_id' => $livechat->patient_id,
|
|
'doctor_id' => $livechat->doctor_id
|
|
])->first();
|
|
$dataNotif = [
|
|
'channel_id' => (string) $channel->id,
|
|
'livechat_id' => (string) $livechat->id,
|
|
'type' => 'failed-payment'
|
|
];
|
|
$userDokter->notify(new SendNotification($title, $body, $dataNotif));
|
|
$patient->notify(new SendNotification($title, $body, $dataNotif));
|
|
return response()->json(['message' => 'User Gagal melakukan pembayaran']);
|
|
}
|
|
|
|
} catch (Exception $e) {
|
|
http_response_code(400);
|
|
echo $e->getMessage();
|
|
}
|
|
}
|
|
|
|
public function redirect(Request $request)
|
|
{
|
|
$resultCode = $request->input('resultCode');
|
|
$merchantOrderId = $request->input('merchantOrderId');
|
|
$reference = $request->input('reference');
|
|
return Redirect::to('https://linksehat.com/');
|
|
}
|
|
}
|