From b4000ca91ad68ec22e0c693a90e4b9f5bcda7b1d Mon Sep 17 00:00:00 2001 From: cnohall Date: Fri, 6 Sep 2024 11:39:31 +0900 Subject: [PATCH] simplify logic and add txid as transaction id --- .../Blockonomics/Blockonomics.php | 39 +++++----- .../BlockonomicsPaymentDriver.php | 75 ------------------- .../gateways/blockonomics/pay.blade.php | 2 + 3 files changed, 19 insertions(+), 97 deletions(-) diff --git a/app/PaymentDrivers/Blockonomics/Blockonomics.php b/app/PaymentDrivers/Blockonomics/Blockonomics.php index 3fa41439a4c4..82a5c4869066 100644 --- a/app/PaymentDrivers/Blockonomics/Blockonomics.php +++ b/app/PaymentDrivers/Blockonomics/Blockonomics.php @@ -31,14 +31,12 @@ class Blockonomics implements MethodInterface { use MakesHash; - public $driver_class; + protected BlockonomicsPaymentDriver $blockonomics; public function __construct(BlockonomicsPaymentDriver $driver_class) { - $this->driver_class = $driver_class; - $this->driver_class->init(); - // TODO: set invoice_id - $this->invoice_id = "123"; + $this->blockonomics = $driver_class; + $this->blockonomics->init(); } public function authorizeView($data) @@ -138,7 +136,7 @@ class Blockonomics implements MethodInterface public function getBTCPrice() { - $currency_code = $this->driver_class->client->getCurrencyCode(); + $currency_code = $this->blockonomics->client->getCurrencyCode(); $BLOCKONOMICS_BASE_URL = 'https://www.blockonomics.co'; $BLOCKONOMICS_PRICE_URL = $BLOCKONOMICS_BASE_URL . '/api/price?currency='; $response = file_get_contents($BLOCKONOMICS_PRICE_URL . $currency_code); @@ -149,14 +147,15 @@ class Blockonomics implements MethodInterface public function paymentView($data) { - $data['gateway'] = $this->driver_class; + $data['gateway'] = $this->blockonomics; $data['amount'] = $data['total']['amount_with_fee']; - $data['currency'] = $this->driver_class->client->getCurrencyCode(); + $data['currency'] = $this->blockonomics->client->getCurrencyCode(); $btc_amount = $data['amount'] / $this->getBTCPrice(); $data['btc_amount'] = number_format($btc_amount, 10, '.', ''); $btc_address = $this->getBTCAddress(); $data['btc_address'] = $btc_address; - $data['invoice_id'] = $this->invoice_id; + //TODO: set invoice_id + $data['invoice_id'] = "123"; $data['end_time'] = $this->getTenMinutesCountDownEndTime(); $data['callback_url'] = $this->setCallbackUrl(); @@ -170,29 +169,25 @@ class Blockonomics implements MethodInterface 'payment_hash' => ['required'], 'amount' => ['required'], 'currency' => ['required'], + 'payment_method_id' => ['required'], + 'txid' => ['required'], ]); - $drv = $this->driver_class; - if ( - strlen($drv->api_key) < 1 || - strlen($drv->callback_secret) < 1 || - strlen($drv->callback_url) < 1 - ) { - throw new PaymentFailed('Blockonomics is not well configured'); - } try { $data = [ - 'payment_method' => '', + 'payment_method' => $request->payment_method_id, 'payment_type' => PaymentType::CRYPTO, - 'amount' => 200, - 'transaction_reference' => 123, + 'amount' => $request->amount, 'gateway_type_id' => GatewayType::CRYPTO, + 'transaction_reference' => $request->txid, ]; - return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey(6)]); + $payment = $this->blockonomics->createPayment($data, Payment::STATUS_COMPLETED); + return redirect()->route('client.payments.show', ['payment' => $this->encodePrimaryKey($payment->id)]); } catch (\Throwable $e) { - PaymentFailureMailer::dispatch($drv->client, $drv->payment_hash->data, $drv->client->company, $request->amount); + $blockonomics = $this->blockonomics; + PaymentFailureMailer::dispatch($blockonomics->client, $blockonomics->payment_hash->data, $blockonomics->client->company, $request->amount); throw new PaymentFailed('Error during Blockonomics payment : ' . $e->getMessage()); } } diff --git a/app/PaymentDrivers/BlockonomicsPaymentDriver.php b/app/PaymentDrivers/BlockonomicsPaymentDriver.php index 9d954cf9ce48..94d1fa935fcc 100644 --- a/app/PaymentDrivers/BlockonomicsPaymentDriver.php +++ b/app/PaymentDrivers/BlockonomicsPaymentDriver.php @@ -125,86 +125,11 @@ class BlockonomicsPaymentDriver extends BaseDriver public function processPaymentResponse($request) { - echo "It reached the processPaymentResponse"; return $this->payment_method->paymentResponse($request); } public function processWebhookRequest() { - $webhook_payload = file_get_contents('php://input'); - - /** @var \stdClass $blockonomicsRep */ - $blockonomicsRep = json_decode($webhook_payload); - if ($blockonomicsRep == null) { - throw new PaymentFailed('Empty data'); - } - if (true === empty($blockonomicsRep->invoiceId)) { - throw new PaymentFailed( - 'Invalid payment notification- did not receive invoice ID.' - ); - } - if ( - str_starts_with($blockonomicsRep->invoiceId, "__test__") - || $blockonomicsRep->type == "InvoiceProcessing" - || $blockonomicsRep->type == "InvoiceCreated" - ) { - return; - } - - $sig = ''; - $headers = getallheaders(); - foreach ($headers as $key => $value) { - if (strtolower($key) === 'blockonomics-sig') { - $sig = $value; - } - } - - $this->init(); - $webhookClient = new Webhook($this->callback_url , $this->api_key); - - if (!$webhookClient->isIncomingWebhookRequestValid($webhook_payload, $sig, $this->webhook_secret)) { - throw new \RuntimeException( - 'Invalid blockonomics payment notification message received - signature did not match.' - ); - } - - $this->setPaymentMethod(GatewayType::CRYPTO); - $this->payment_hash = PaymentHash::whereRaw('BINARY `hash`= ?', [$blockonomicsRep->metadata->InvoiceNinjaPaymentHash])->firstOrFail(); - $StatusId = Payment::STATUS_PENDING; - if ($this->payment_hash->payment_id == null) { - - $_invoice = Invoice::with('client')->withTrashed()->find($this->payment_hash->fee_invoice_id); - - $this->client = $_invoice->client; - - $dataPayment = [ - 'payment_method' => $this->payment_method, - 'payment_type' => PaymentType::CRYPTO, - 'amount' => $_invoice->amount, - 'gateway_type_id' => GatewayType::CRYPTO, - 'transaction_reference' => $blockonomicsRep->invoiceId - ]; - $payment = $this->createPayment($dataPayment, $StatusId); - } else { - /** @var \App\Models\Payment $payment */ - $payment = Payment::withTrashed()->find($this->payment_hash->payment_id); - $StatusId = $payment->status_id; - } - switch ($blockonomicsRep->type) { - case "InvoiceExpired": - $StatusId = Payment::STATUS_CANCELLED; - break; - case "InvoiceInvalid": - $StatusId = Payment::STATUS_FAILED; - break; - case "InvoiceSettled": - $StatusId = Payment::STATUS_COMPLETED; - break; - } - if ($payment->status_id != $StatusId) { - $payment->status_id = $StatusId; - $payment->save(); - } } diff --git a/resources/views/portal/ninja2020/gateways/blockonomics/pay.blade.php b/resources/views/portal/ninja2020/gateways/blockonomics/pay.blade.php index eb7a6d84ec71..aa17ac630d23 100644 --- a/resources/views/portal/ninja2020/gateways/blockonomics/pay.blade.php +++ b/resources/views/portal/ninja2020/gateways/blockonomics/pay.blade.php @@ -22,6 +22,7 @@ +