103 lines
2.8 KiB
PHP
103 lines
2.8 KiB
PHP
<?php
|
|
|
|
namespace Paymenter\Extensions\Gateways\BitCart;
|
|
|
|
use App\Classes\Extension\Gateway;
|
|
use App\Helpers\ExtensionHelper;
|
|
use App\Models\Invoice;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Http;
|
|
|
|
class BitCart extends Gateway
|
|
{
|
|
public function boot()
|
|
{
|
|
require __DIR__ . '/routes.php';
|
|
}
|
|
|
|
public function getConfig($values = [])
|
|
{
|
|
return [
|
|
[
|
|
'name' => 'api_endpoint',
|
|
'label' => 'BitCart API endpoint',
|
|
'type' => 'text',
|
|
'required' => true,
|
|
],
|
|
[
|
|
'name' => 'store_id',
|
|
'label' => 'BitCart store id',
|
|
'type' => 'text',
|
|
'required' => true,
|
|
],
|
|
[
|
|
'name' => 'admin_url',
|
|
'label' => 'BitCart admin URL',
|
|
'type' => 'text',
|
|
'required' => true,
|
|
],
|
|
];
|
|
}
|
|
|
|
public function pay(Invoice $invoice, $total)
|
|
{
|
|
$payment_url = $this->get_payment_url($invoice->id, $total);
|
|
return $payment_url;
|
|
}
|
|
|
|
private function get_payment_url($invoiceId, $amount)
|
|
{
|
|
$api_domain = $this->config('api_endpoint');
|
|
$admin_domain = $this->config('admin_url');
|
|
$params = [
|
|
'price' => number_format($amount, 2, '.', ''),
|
|
'store_id' => $this->config('store_id'),
|
|
'currency' => ExtensionHelper::getCurrency(),
|
|
'buyer_email' => auth()->user()->email,
|
|
'redirect_url' => route('clients.invoice.show', $invoiceId),
|
|
'notification_url' => url('/extensions/bitcart/webhook'),
|
|
'order_id' => $invoiceId,
|
|
];
|
|
|
|
$response = $this->send_request($api_domain . '/invoices/order_id/' . urlencode($invoiceId), $params);
|
|
|
|
return $admin_domain . '/i/' . $response->id;
|
|
}
|
|
|
|
private function send_request($url, $data)
|
|
{
|
|
$post_fields = json_encode($data);
|
|
|
|
$ch = curl_init();
|
|
curl_setopt($ch, CURLOPT_URL, $url);
|
|
curl_setopt($ch, CURLOPT_HTTPHEADER, [
|
|
'Content-Type: application/json',
|
|
]);
|
|
curl_setopt($ch, CURLOPT_POST, 1);
|
|
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
|
|
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
|
|
|
|
$result = curl_exec($ch);
|
|
curl_close($ch);
|
|
|
|
return json_decode($result);
|
|
}
|
|
|
|
public function webhook(Request $request)
|
|
{
|
|
$body = $request->getContent();
|
|
$data = json_decode($body, true);
|
|
|
|
$invoiceId = $data['id'];
|
|
$status = $data['status'];
|
|
|
|
if ($status === 'complete') {
|
|
ExtensionHelper::paymentDone($invoiceId, 'BitCart', null);
|
|
return response()->json(['success' => true]);
|
|
}
|
|
|
|
return response()->json(['success' => false]);
|
|
}
|
|
}
|
|
|