simplify logic and add txid as transaction id

This commit is contained in:
cnohall 2024-09-06 11:39:31 +09:00
parent 926b2870ed
commit b4000ca91a
3 changed files with 19 additions and 97 deletions

View File

@ -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());
}
}

View File

@ -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();
}
}

View File

@ -22,6 +22,7 @@
<input type="hidden" name="amount" value="{{ $amount }}">
<input type="hidden" name="currency" value="{{ $currency }}">
<input type="hidden" name="payment_hash" value="{{ $payment_hash }}">
<input type="hidden" name="txid" value="">
</form>
<script>
@ -67,6 +68,7 @@
console.log('Payment status:', data.status);
const isPaymentConfirmed = data.status == 2;
if (isPaymentConfirmed) {
document.querySelector('input[name="txid"]').value = data.txid || '';
document.getElementById('server-response').submit();
}
};