This commit is contained in:
cnohall 2024-09-02 16:59:12 +09:00
parent ce8e8c620c
commit a32ef1a055
8 changed files with 387 additions and 5 deletions

View File

@ -156,6 +156,7 @@ class CompanyGateway extends BaseModel
'80af24a6a691230bbec33e930ab40666' => 323,
'vpyfbmdrkqcicpkjqdusgjfluebftuva' => 324, //BTPay
'91be24c7b792230bced33e930ac61676' => 325,
'wbhf02us6owgo7p4nfjd0ymssdshks4d' => 326, //Blockonomics
];
protected $touches = [];

View File

@ -107,7 +107,9 @@ class Gateway extends StaticModel
$link = 'https://docs.btcpayserver.org/InvoiceNinja/';
} elseif ($this->id == 63) {
$link = 'https://rotessa.com';
}
} elseif ($this->id == 64) {
$link = 'https://blockonomics.co';
}
return $link;
}
@ -226,8 +228,8 @@ class Gateway extends StaticModel
return [
GatewayType::CRYPTO => ['refund' => true, 'token_billing' => false, 'webhooks' => ['confirmed', 'paid_out', 'failed', 'fulfilled']],
]; //BTCPay
case 63:
return [
case 63:
return [
GatewayType::BANK_TRANSFER => [
'refund' => false,
'token_billing' => true,
@ -235,6 +237,10 @@ class Gateway extends StaticModel
],
GatewayType::ACSS => ['refund' => false, 'token_billing' => true, 'webhooks' => []]
]; // Rotessa
case 64:
return [
GatewayType::CRYPTO => ['refund' => true, 'token_billing' => false, 'webhooks' => ['confirmed', 'paid_out', 'failed', 'fulfilled']],
]; //Blockonomics
default:
return [];
}

View File

@ -81,6 +81,7 @@ class PaymentType extends StaticModel
public const STRIPE_BANK_TRANSFER = 50;
public const CASH_APP = 51;
public const PAY_LATER = 52;
public const BLOCKONOMICS = 64;
public array $type_names = [
self::BANK_TRANSFER => 'payment_type_Bank Transfer',
@ -129,6 +130,7 @@ class PaymentType extends StaticModel
self::CASH_APP => 'payment_type_Cash App',
self::VENMO => 'payment_type_Venmo',
self::PAY_LATER => 'payment_type_Pay Later',
self::BLOCKONOMICS => 'payment_type_Blockonomics',
];
public static function parseCardType($cardName)

View File

@ -153,7 +153,9 @@ class SystemLog extends Model
public const TYPE_BTC_PAY = 324;
public const TYPE_ROTESSA = 325;
public const TYPE_BLOCKONOMICS = 326;
public const TYPE_QUOTA_EXCEEDED = 400;
public const TYPE_UPSTREAM_FAILURE = 401;

View File

@ -0,0 +1,149 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2024. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://www.elastic.co/licensing/elastic-license
*/
namespace App\PaymentDrivers\Blockonomics;
use App\Models\Payment;
use App\PaymentDrivers\BlockonomicsPaymentDriver;
use App\Utils\Traits\MakesHash;
use App\PaymentDrivers\Common\MethodInterface;
use App\Http\Requests\ClientPortal\Payments\PaymentResponseRequest;
use App\Exceptions\PaymentFailed;
use App\Jobs\Mail\PaymentFailureMailer;
use Illuminate\Mail\Mailables\Address;
use App\Services\Email\EmailObject;
use App\Services\Email\Email;
use Illuminate\Support\Facades\App;
class Blockonomics implements MethodInterface
{
use MakesHash;
public $driver_class;
public function __construct(BlockonomicsPaymentDriver $driver_class)
{
$this->driver_class = $driver_class;
$this->driver_class->init();
}
public function paymentView($data)
{
$data['gateway'] = $this->driver_class;
$data['amount'] = $data['total']['amount_with_fee'];
$data['currency'] = $this->driver_class->client->getCurrencyCode();
return render('gateways.blockonomics.pay', $data);
}
public function paymentResponse(PaymentResponseRequest $request)
{
$request->validate([
'payment_hash' => ['required'],
'amount' => ['required'],
'currency' => ['required'],
]);
$drv = $this->driver_class;
if (
strlen($drv->blockonomics_url) < 1
|| strlen($drv->api_key) < 1
|| strlen($drv->store_id) < 1
|| strlen($drv->webhook_secret) < 1
) {
throw new PaymentFailed('Blockonomics is not well configured');
}
if (!filter_var($this->driver_class->blockonomics_url, FILTER_VALIDATE_URL)) {
throw new PaymentFailed('Wrong format for Blockonomics Url');
}
try {
$_invoice = collect($drv->payment_hash->data->invoices)->first();
$cli = $drv->client;
$metaData = [
'buyerName' => $cli->name,
'buyerAddress1' => $cli->address1,
'buyerAddress2' => $cli->address2,
'buyerCity' => $cli->city,
'buyerState' => $cli->state,
'buyerZip' => $cli->postal_code,
'buyerCountry' => $cli->country_id,
'buyerPhone' => $cli->phone,
'itemDesc' => "From InvoiceNinja",
'InvoiceNinjaPaymentHash' => $drv->payment_hash->hash
];
$urlRedirect = redirect()->route('client.invoice.show', ['invoice' => $_invoice->invoice_id])->getTargetUrl();
$rep = $client->createInvoice(
$drv->store_id,
$request->currency,
$_invoice->invoice_number,
$cli->present()->email(),
$metaData,
$checkoutOptions
);
return redirect($rep->getCheckoutLink());
} catch (\Throwable $e) {
PaymentFailureMailer::dispatch($drv->client, $drv->payment_hash->data, $drv->client->company, $request->amount);
throw new PaymentFailed('Error during Blockonomics payment : ' . $e->getMessage());
}
}
public function refund(Payment $payment, $amount)
{
try {
if ($amount == $payment->amount) {
$refundVariant = "Fiat";
$refundPaymentMethod = "BTC";
$refundDescription = "Full refund";
$refundCustomCurrency = null;
$refundCustomAmount = null;
} else {
$refundVariant = "Custom";
$refundPaymentMethod = "";
$refundDescription = "Partial refund";
$refundCustomCurrency = $payment->currency;
$refundCustomAmount = $amount;
}
App::setLocale($payment->company->getLocale());
$email_object = new EmailObject();
$email_object->subject = ctrans('texts.blockonomics_refund_subject');
$email_object->body = ctrans('texts.blockonomics_refund_body') . '<br>' . $refund->getViewLink();
$email_object->text_body = ctrans('texts.blockonomics_refund_body') . '\n' . $refund->getViewLink();
$email_object->company_key = $payment->company->company_key;
$email_object->html_template = 'email.template.generic';
$email_object->to = [new Address($payment->client->present()->email(), $payment->client->present()->name())];
$email_object->email_template_body = 'blockonomics_refund_subject';
$email_object->email_template_subject = 'blockonomics_refund_body';
Email::dispatch($email_object, $payment->company);
$data = [
'transaction_reference' => $refund->getId(),
'transaction_response' => json_encode($refund),
'success' => true,
'description' => "Please follow this link to claim your refund: " . $refund->getViewLink(),
'code' => 202,
];
return $data;
} catch (\Throwable $e) {
throw new PaymentFailed('Error during Blockonomics refund : ' . $e->getMessage());
}
}
}

View File

@ -0,0 +1,174 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @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;
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 = true; //does this gateway support refunds?
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_CHECKOUT; //define a constant for your gateway ie TYPE_YOUR_CUSTOM_GATEWAY - set the const in the SystemLog model
public $blockonomics_url = "";
public $api_key = "";
public $store_id = "";
public $webhook_secret = "";
public $blockonomics;
public function init()
{
$this->blockonomics_url = $this->company_gateway->getConfigField('blockonomicsUrl');
$this->api_key = $this->company_gateway->getConfigField('apiKey');
$this->store_id = $this->company_gateway->getConfigField('storeId');
$this->webhook_secret = $this->company_gateway->getConfigField('webhookSecret');
return $this; /* This is where you boot the gateway with your auth credentials*/
}
/* 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)
{
return $this->payment_method->paymentView($data); //this is your custom implementation from here
}
public function processPaymentResponse($request)
{
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->blockonomics_url, $this->api_key);
if (!$webhookClient->isIncomingWebhookRequestValid($webhook_payload, $sig, $this->webhook_secret)) {
throw new \RuntimeException(
'Invalid 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();
}
}
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
}
}

View File

@ -0,0 +1,47 @@
<?php
use Illuminate\Database\Migrations\Migration;
use App\Models\Gateway;
use App\Models\GatewayType;
use Illuminate\Support\Str;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
if(!Gateway::find(64))
{
$fields = new \stdClass;
$fields->blockonomicsUrl = "";
$fields->apiKey = "";
$fields->storeId = "";
$fields->webhookSecret = "";
$gateway = new Gateway;
$gateway->id = 64;
$gateway->name = 'Blockonomics';
$gateway->key = 'wbhf02us6owgo7p4nfjd0ymssdshks4d';
$gateway->provider = 'Blockonomics';
$gateway->is_offsite = true;
$gateway->fields = \json_encode($fields);
$gateway->visible = 1;
$gateway->site_url = 'https://blockonomics.co';
$gateway->default_gateway_type_id = GatewayType::CRYPTO;
$gateway->save();
}
}
/**
* Reverse the migrations.
*/
public function down(): void
{
//
}
};

View File

@ -89,6 +89,7 @@ class PaymentLibrariesSeeder extends Seeder
['id' => 61, 'name' => 'PayPal Platform', 'provider' => 'PayPal_PPCP', 'key' => '80af24a6a691230bbec33e930ab40666', 'fields' => '{"testMode":false}'],
['id' => 62, 'name' => 'BTCPay', 'provider' => 'BTCPay', 'key' => 'vpyfbmdrkqcicpkjqdusgjfluebftuva', 'fields' => '{"btcpayUrl":"", "apiKey":"", "storeId":"", "webhookSecret":""}'],
['id' => 63, 'name' => 'Rotessa', 'is_offsite' => false, 'sort_order' => 22, 'provider' => 'Rotessa', 'key' => '91be24c7b792230bced33e930ac61676', 'fields' => '{"apiKey":"", "testMode":""}'],
['id' => 64, 'name' => 'Blockonomics', 'provider' => 'Blockonomics', 'key' => 'wbhf02us6owgo7p4nfjd0ymssdshks4d', 'fields' => '{"blockonomicsUrl":"", "apiKey":"", "storeId":"", "webhookSecret":""}'],
];
foreach ($gateways as $gateway) {
@ -105,7 +106,7 @@ class PaymentLibrariesSeeder extends Seeder
Gateway::query()->update(['visible' => 0]);
Gateway::whereIn('id', [1, 3, 7, 11, 15, 20, 39, 46, 55, 50, 57, 52, 58, 59, 60, 62, 63])->update(['visible' => 1]);
Gateway::whereIn('id', [1, 3, 7, 11, 15, 20, 39, 46, 55, 50, 57, 52, 58, 59, 60, 62, 63, 64])->update(['visible' => 1]);
if (Ninja::isHosted()) {
Gateway::whereIn('id', [20, 49])->update(['visible' => 0]);