<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
victorybiz / laravel-crypto-payment-gateway example snippets
// routes/web.php
// You can protect the 'payments.crypto.pay' route with `auth` middleware to allow access by only authenticated user
Route::match(['get', 'post'], '/payments/crypto/pay', Victorybiz\LaravelCryptoPaymentGateway\Http\Controllers\CryptoPaymentController::class)
->name('payments.crypto.pay');
// You you need to create your own callback controller and define the route below
// The callback route should be a publicly accessible route with no auth
// However, you may also exclude the route from CSRF Protection by adding their URIs to the $except property of the VerifyCsrfToken middleware.
Route::post('/payments/crypto/callback', [App\Http\Controllers\Payment\PaymentController::class, 'callback'])
->withoutMiddleware(['web', 'auth']);
// This could be in a controller method or livewire component method
use Victorybiz\LaravelCryptoPaymentGateway\LaravelCryptoPaymentGateway;
$payment_url = LaravelCryptoPaymentGateway::startPaymentSession([
'amountUSD' => $validatedData['amount'], // OR 'amount' when sending BTC value
'orderID' => $product->id,
'userID' => Auth::user()->id,
'redirect' => url()->full(),
]);
// redirect to the payment page
redirect()->to($payment_url);
namespace App\Http\Controllers\Payment;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Victorybiz\LaravelCryptoPaymentGateway\LaravelCryptoPaymentGateway;
class PaymentController extends Controller
{
/**
* Cryptobox callback.
*/
public function callback(Request $request)
{
return LaravelCryptoPaymentGateway::callback();
}
}
namespace App\Http\Controllers\Payment;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Victorybiz\LaravelCryptoPaymentGateway\LaravelCryptoPaymentGateway;
class PaymentController extends Controller
{
//...
/**
* Cryptobox IPN Example
*
* @param \Victorybiz\LaravelCryptoPaymentGateway\Models\CryptoPaymentModel $cryptoPaymentModel
* @param array $payment_details
* @param string $box_status
* @return bool
*/
public static function ipn($cryptoPaymentModel, $payment_details, $box_status)
{
if ($cryptoPaymentModel) {
/*
// ADD YOUR CODE HERE
// ------------------
// For example, you have a model `UserOrder`, you can create new Bitcoin payment record to this model
$userOrder = UserOrder::where('payment_id', $cryptoPaymentModel->paymentID)->first();
if (!$userOrder)
{
UserOrder::create([
'payment_id' => $cryptoPaymentModel->paymentID,
'user_id' => $payment_details["user"],
'order_id' => $payment_details["order"],
'amount' => floatval($payment_details["amount"]),
'amountusd' => floatval($payment_details["amountusd"]),
'coinlabel' => $payment_details["coinlabel"],
'txconfirmed' => $payment_details["confirmed"],
'status' => $payment_details["status"],
]);
}
// ------------------
// Received second IPN notification (optional) - Bitcoin payment confirmed (6+ transaction confirmations)
if ($userOrder && $box_status == "cryptobox_updated")
{
$userOrder->txconfirmed = $payment_details["confirmed"];
$userOrder->save();
}
// ------------------
*/
// Onetime action when payment confirmed (6+ transaction confirmations)
if (!$cryptoPaymentModel->processed && $payment_details["confirmed"])
{
// Add your custom logic here to give value to the user.
// ------------------
// set the status of the payment to processed
// $cryptoPaymentModel->setStatusAsProcessed();
// ------------------
// Add logic to send notification of confirmed/processed payment to the user if any
}
}
return true;
}
}
// config/laravel-crypto-payment-gateway.php
/**
* Hook IPN (Instant payment notification) to the following static class method.
* In this static class method, you can add your custom logic and give value to the user once your confirmed payment box status.
* You can also add payment notification logic.
* This can be a static class method defined anywhere in your application.
* This can also be static method/action defined in controller class but route must not be defined for the action.
*
* The Static Method Definition in your class:
* @param \Victorybiz\LaravelCryptoPaymentGateway\Models\CryptoPaymentModel $cryptoPaymentModel
* @param array $payment_details
* @param string $box_status
* @return bool
* public static function ipn($cryptoPaymentModel, $payment_details, $box_status)
* {
* // Add your custom logic here.
* return true;
* }
*
* Example: [\Victorybiz\LaravelCryptoPaymentGateway\Http\Controllers\CryptoPaymentController::class, 'ipn']
*/
'hook_ipn' => [\App\Http\Controllers\Payment\PaymentController, 'ipn'],