From b49a0253e63f1b74e6e1435de032186107cf89a0 Mon Sep 17 00:00:00 2001 From: Sahag Arzoumanian Date: Thu, 3 Jul 2025 16:49:07 -0700 Subject: [PATCH] Get variables from config in get payment url --- BitCart.php | 133 ++++++++++++++++++++-------------------------------- 1 file changed, 50 insertions(+), 83 deletions(-) diff --git a/BitCart.php b/BitCart.php index 5d4860e..0003fd4 100644 --- a/BitCart.php +++ b/BitCart.php @@ -1,135 +1,102 @@ 'BitCart', - 'version' => '0.0.1', - 'author' => '0ut0f.space', - 'website' => 'https://0ut0f.space', - ]; - } - /** - * Get all the configuration for the extension - * - * @return array - */ public function getConfig($values = []) { return [ - [ - 'name' => 'api_endpoint', - 'friendlyName' => 'BitCart API endpoint', - 'type' => 'text', - 'required' => true, - ], - [ + [ + 'name' => 'api_endpoint', + 'label' => 'BitCart API endpoint', + 'type' => 'text', + 'required' => true, + ], + [ 'name' => 'store_id', - 'friendlyName' => 'BitCart store id', + 'label' => 'BitCart store id', 'type' => 'text', 'required' => true, - ], - [ - 'name'=>'admin_url', - 'friendlyName' => 'BitCart admin URL', + ], + [ + 'name' => 'admin_url', + 'label' => 'BitCart admin URL', 'type' => 'text', 'required' => true, - ], + ], ]; } - /** - * Get the URL to redirect to - * - * @param int $total - * @param array $products - * @param Invoice $invoice - * @param int $invoiceId - * @return string - */ public function pay(Invoice $invoice, $total) { - $invoiceId = $invoice->id; - $payment_url = $this->get_payment_url($invoiceId,$total); + $payment_url = $this->get_payment_url($invoice->id, $total); return $payment_url; } - public function get_payment_url ($invoiceId, $amount) { - $api_domain = ExtensionHelper::getConfig('Bitcart', 'api_endpoint'); - $admin_domain = ExtensionHelper::getConfig('Bitcart','admin_url'); - $params = array( + 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' => ExtensionHelper::getConfig('Bitcart','store_id'), + '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, - ); - $invoice = $this->send_request(sprintf('%s/%s', $api_domain, 'invoices/order_id/' . urlencode($invoiceId)), $params); - return $admin_domain . '/i/' . $invoice->id; + ]; + + $response = $this->send_request($api_domain . '/invoices/order_id/' . urlencode($invoiceId), $params); + + return $admin_domain . '/i/' . $response->id; } - public function send_request($url, $data, $post = 1) + private function send_request($url, $data) { $post_fields = json_encode($data); - $request_headers = array(); - $request_headers[] = 'Content-Type: application/json'; - $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_HTTPHEADER, $request_headers); - if ($post) { - curl_setopt($ch, CURLOPT_POST, $post); - curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields); - } + 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); + $result = curl_exec($ch); curl_close($ch); return json_decode($result); } - public function webhook(Request $request) - { - $body = $request->getContent(); - $api_domain = ExtensionHelper::getConfig('Bitcart', 'api_endpoint'); - $data = json_decode($body, true); - $url_check = sprintf('%s/%s', $api_domain, 'invoices/' . $data['id']); - $checkData = $this->send_request($url_check, array(), 0); - $invoiceId = $data['id']; - $status = $data['status']; - if ($invoiceId != $checkData->id) { - return response()->json(['success' => false]); - } - if ($status != 'complete' || $status != $checkData->status) { - return response()->json(['success' => false]); - } - ExtensionHelper::paymentDone($checkData -> order_id,'BitCart',null); - return response()->json(['success' => true]); - } + 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]); + } } +