From 69682665c5b587c673c10a341cfcbf839e377a42 Mon Sep 17 00:00:00 2001 From: Gru Date: Thu, 11 Apr 2024 15:42:15 +1000 Subject: [PATCH] FEATURE: CryptoBot payments --- .../CryptoBot/CryptoBotExtension.php | 117 ++++++++++++++++++ .../CryptoBot/CryptoBotSettings.php | 36 ++++++ ...03_26_215801_create_cryptobot_settings.php | 18 +++ .../PaymentGateways/CryptoBot/web_routes.php | 18 +++ 4 files changed, 189 insertions(+) create mode 100644 app/Extensions/PaymentGateways/CryptoBot/CryptoBotExtension.php create mode 100644 app/Extensions/PaymentGateways/CryptoBot/CryptoBotSettings.php create mode 100644 app/Extensions/PaymentGateways/CryptoBot/migrations/2023_03_26_215801_create_cryptobot_settings.php create mode 100644 app/Extensions/PaymentGateways/CryptoBot/web_routes.php diff --git a/app/Extensions/PaymentGateways/CryptoBot/CryptoBotExtension.php b/app/Extensions/PaymentGateways/CryptoBot/CryptoBotExtension.php new file mode 100644 index 00000000..a2fb097c --- /dev/null +++ b/app/Extensions/PaymentGateways/CryptoBot/CryptoBotExtension.php @@ -0,0 +1,117 @@ + "CryptoBot", + "RoutesIgnoreCsrf" => [ + "payment/CryptoBotWebhook" + ], + ]; + } + + public static function getRedirectUrl(Payment $payment, ShopProduct $shopProduct, string $totalPriceString): string + { + $url = 'https://pay.crypt.bot/api/createInvoice'; + $settings = new CryptoBotSettings(); + try { + $response = Http::withHeaders([ + 'Accept' => 'application/json', + 'Content-Type' => 'application/json', + 'Crypto-Pay-API-Token' => $settings->api_key + ])->post($url, [ + 'amount' => $totalPriceString, + 'payload' => strval($payment->id), + 'description' => "Заказ #{$payment->id} - " . $shopProduct->name, + 'currency_type' => 'fiat', + 'fiat' => $shopProduct->currency_code, + 'hidden_message' => 'Спасибо за перевод 💖', + 'paid_btn_name' => 'callback', + 'paid_btn_url' => route('payment.CryptoBotSuccess').'?payment=YGqvOf4I' + ]); + + if($response->json('ok') == false){ + return $response->json('error.name'); + } + + return $response->json('result.pay_url'); + } catch (Exception $ex) { + Log::error('CryptoBot Payment: ' . $ex->getMessage()); + throw new Exception('Payment failed'); + } + } + + static function success(Request $request): void + { + Redirect::route('home')->with('success', 'Ваш платёж в обработке 💖')->send(); + return; + } + + static function webhook(Request $request): JsonResponse + { + + $settings = new CryptoBotSettings(); + /*** Проверка подписи ***/ + $sign_header = $request->header('crypto-pay-api-signature'); + + $secret_key = hash('sha256', $settings->api_key, true); + $calculated_signature = hash_hmac('sha256', $request->getContent(), $secret_key); + + if (!hash_equals($sign_header, $calculated_signature)) { + return response()->json(['status' => 'invalid sign.']); + } + /*** ***/ + + $payment = Payment::findOrFail($request->json('payload.payload')); + $shopProduct = ShopProduct::findOrFail($payment->shop_item_product_id); + $user = User::findOrFail($payment->user_id); + + //update payment + $payment->update([ + 'status' => PaymentStatus::PAID, + 'payment_id' => $request->input('payload.invoice_id'), + ]); + try { + $user->increment('credits', $payment->amount); + } catch (Exception $exception) { + throw $exception; + } + + + event(new PaymentEvent($user, $payment, $shopProduct)); + event(new UserUpdateCreditsEvent($user)); + $user->notify(new ConfirmPaymentNotification($payment)); + + // return a 200 status code + return response()->json(['success' => true]); + } +} diff --git a/app/Extensions/PaymentGateways/CryptoBot/CryptoBotSettings.php b/app/Extensions/PaymentGateways/CryptoBot/CryptoBotSettings.php new file mode 100644 index 00000000..69b7415c --- /dev/null +++ b/app/Extensions/PaymentGateways/CryptoBot/CryptoBotSettings.php @@ -0,0 +1,36 @@ + 'fas fa-dollar-sign', + 'api_key' => [ + 'type' => 'string', + 'label' => 'API Key', + 'description' => 'The API Key of your Mollie App', + ], + 'enabled' => [ + 'type' => 'boolean', + 'label' => 'Enabled', + 'description' => 'Enable or disable this payment gateway', + ], + ]; + } +} diff --git a/app/Extensions/PaymentGateways/CryptoBot/migrations/2023_03_26_215801_create_cryptobot_settings.php b/app/Extensions/PaymentGateways/CryptoBot/migrations/2023_03_26_215801_create_cryptobot_settings.php new file mode 100644 index 00000000..d0850264 --- /dev/null +++ b/app/Extensions/PaymentGateways/CryptoBot/migrations/2023_03_26_215801_create_cryptobot_settings.php @@ -0,0 +1,18 @@ +migrator->addEncrypted('cryptobot.api_key', null); + $this->migrator->add('cryptobot.enabled', false); + } + + public function down(): void + { + $this->migrator->delete('cryptobot.api_key'); + $this->migrator->delete('cryptobot.enabled'); + } +} diff --git a/app/Extensions/PaymentGateways/CryptoBot/web_routes.php b/app/Extensions/PaymentGateways/CryptoBot/web_routes.php new file mode 100644 index 00000000..4a16ef66 --- /dev/null +++ b/app/Extensions/PaymentGateways/CryptoBot/web_routes.php @@ -0,0 +1,18 @@ +group(function () { + Route::get( + 'payment/CryptoBotSuccess', + function () { + CryptoBotExtension::success(request()); + } + )->name('payment.CryptoBotSuccess'); +}); + + +Route::post('payment/CryptoBotWebhook', function () { + CryptoBotExtension::webhook(request()); +})->name('payment.CryptoBotWebhook');