1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-14 23:22:52 +01:00
invoiceninja/app/PaymentDrivers/BlockonomicsPaymentDriver.php

147 lines
4.3 KiB
PHP
Raw Normal View History

2024-09-02 09:59:12 +02:00
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
2024-09-22 23:27:28 +02:00
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
2024-09-02 09:59:12 +02:00
*
* @license https://opensource.org/licenses/AAL
*/
namespace App\PaymentDrivers;
use App\Utils\Traits\MakesHash;
use App\Models\PaymentHash;
use App\Models\GatewayType;
use App\PaymentDrivers\Blockonomics\Blockonomics;
use App\Models\SystemLog;
use App\Models\Payment;
2024-09-11 15:14:21 +02:00
use App\Models\Gateway;
2024-09-02 09:59:12 +02:00
use App\Models\Client;
use App\Exceptions\PaymentFailed;
use App\Models\PaymentType;
use App\Http\Requests\Payments\PaymentWebhookRequest;
use App\Models\Invoice;
class BlockonomicsPaymentDriver extends BaseDriver
{
use MakesHash;
public $refundable = false; //does this gateway support refunds?
2024-09-02 09:59:12 +02:00
public $token_billing = false; //does this gateway support token billing?
public $can_authorise_credit_card = false; //does this gateway support authorizations?
public $gateway; //initialized gateway
public $payment_method; //initialized payment method
public static $methods = [
GatewayType::CRYPTO => Blockonomics::class, //maps GatewayType => Implementation class
];
public const SYSTEM_LOG_TYPE = SystemLog::TYPE_BLOCKONOMICS; //define a constant for your gateway ie TYPE_YOUR_CUSTOM_GATEWAY - set the const in the SystemLog model
2024-09-02 09:59:12 +02:00
public $BASE_URL = 'https://www.blockonomics.co';
public $NEW_ADDRESS_URL = 'https://www.blockonomics.co/api/new_address';
public $PRICE_URL = 'https://www.blockonomics.co/api/price';
2024-09-02 09:59:12 +02:00
2024-09-03 04:30:10 +02:00
public function init()
{
2024-09-03 04:30:10 +02:00
return $this; /* This is where you boot the gateway with your auth credentials*/
}
2024-09-02 09:59:12 +02:00
/* Returns an array of gateway types for the payment gateway */
public function gatewayTypes(): array
{
$types = [];
$types[] = GatewayType::CRYPTO;
return $types;
}
public function setPaymentMethod($payment_method_id)
{
$class = self::$methods[$payment_method_id];
$this->payment_method = new $class($this);
return $this;
}
public function processPaymentView(array $data)
{
2024-09-16 01:32:39 +02:00
$this->init();
2024-09-02 09:59:12 +02:00
return $this->payment_method->paymentView($data); //this is your custom implementation from here
}
public function processPaymentResponse($request)
{
2024-09-16 01:32:39 +02:00
$this->init();
2024-09-02 09:59:12 +02:00
return $this->payment_method->paymentResponse($request);
}
2024-09-18 09:12:52 +02:00
public function processWebhookRequest(PaymentWebhookRequest $request)
2024-09-02 09:59:12 +02:00
{
2024-09-18 09:12:52 +02:00
2024-09-18 23:45:00 +02:00
$company = $request->getCompany();
2024-09-18 09:12:52 +02:00
$url_callback_secret = $request->secret;
2024-09-18 13:11:25 +02:00
$db_callback_secret = $this->company_gateway->getConfigField('callbackSecret');
if ($url_callback_secret != $db_callback_secret) {
throw new PaymentFailed('Secret does not match');
}
2024-09-18 13:11:25 +02:00
$txid = $request->txid;
$value = $request->value;
$status = $request->status;
$addr = $request->addr;
2024-09-11 15:14:21 +02:00
2024-09-18 23:45:00 +02:00
$payment = Payment::query()
->where('company_id', $company->id)
->where('transaction_reference', $txid)
->firstOrFail();
if (!$payment) {
2024-09-18 23:45:00 +02:00
return response()->json([], 200);
// TODO: Implement logic to create new payment in case user sends payment to the address after closing the payment page
2024-09-11 15:14:21 +02:00
}
2024-09-13 06:03:46 +02:00
2024-09-18 23:45:00 +02:00
$statusId = Payment::STATUS_PENDING;
switch ($status) {
case 0:
$statusId = Payment::STATUS_PENDING;
break;
case 1:
$statusId = Payment::STATUS_PENDING;
break;
case 2:
$statusId = Payment::STATUS_COMPLETED;
break;
2024-09-08 13:24:13 +02:00
}
if($payment->status_id == $statusId) {
2024-09-18 09:12:52 +02:00
return response()->json([], 200);
} else {
2024-09-10 08:02:55 +02:00
$payment->status_id = $statusId;
2024-09-08 13:24:13 +02:00
$payment->save();
2024-09-18 09:12:52 +02:00
return response()->json([], 200);
2024-09-08 13:24:13 +02:00
}
2024-09-02 09:59:12 +02:00
}
public function refund(Payment $payment, $amount, $return_client_response = false)
{
$this->setPaymentMethod(GatewayType::CRYPTO);
return $this->payment_method->refund($payment, $amount); //this is your custom implementation from here
}
}