无惧-ajs/smart-payment-routing

这是一个为Laravel设计的智能支付路由包,它检查执行交易的最佳支付处理器

1.0.0 2024-07-07 21:20 UTC

This package is not auto-updated.

Last update: 2024-09-30 20:59:56 UTC


README

智能支付路由是一个Laravel包,用于根据定义的标准智能路由交易到支付处理器(网关)

安装

使用包管理器 composer 安装智能支付路由

composer require fearless-ajs/smart-payment-routing

用法

  1. 在您的 .env 文件中输入支付处理器的环境变量,或者您也可以发布 smart-payment-config.php。
  PAYSTACK_SECRET=*************
  PAYSTACK_REDIRECT_URL=http://example.com/redirect

  FLUTTERWAVE_SECRET=*************
  FLUTTERWAVE_REDIRECT_URL=http://example.com/redirect
  1. 将 ProcessorManager 类注入到处理支付交易的类中,并通过初始化所需的支付处理器来配置处理器管理器,如下所示。
namespace App\Http\Controllers;

use App\Http\Requests\CreatePaymentRequest;
use App\Http\Services\StripeProcessor;
use Fearless\SmartPaymentRouting\core\adapter\ProcessorManager;
use Fearless\SmartPaymentRouting\core\PaymentRoutingService;
use Fearless\SmartPaymentRouting\core\processors\FlutterwaveProcessor;
use Fearless\SmartPaymentRouting\core\processors\PaystackProcessor;
use Illuminate\Http\Request;


class TransactionController extends Controller
{
   private readonly ProcessorManager $processorManager;

    public function __construct(ProcessorManager $processorManager)
    {
        $this->processorManager = $processorManager;
        // Register the processor for transactions within this class
        $processorManager->initialize([
            [
                'name' => 'paystack',
                'processor' => new PaystackProcessor(),
            ],
            [
                'name'  => 'flutterwave',
                'processor' => new FlutterwaveProcessor(),
            ]
        ]);
    }

}
  1. 然后实例化 PaymentRoutingService 并将 ProcessorManager 作为参数传递。
  2. 然后调用 paymentRoutingService 上的 makeCharge 方法,并传递您的交易数据(用户电子邮件、金额和货币)以执行支付交易(默认支付处理器将仅返回支付URL,用户需要重定向到支付URL以进行支付)
namespace App\Http\Controllers;

use App\Http\Requests\CreatePaymentRequest;
use App\Http\Services\StripeProcessor;
use Fearless\SmartPaymentRouting\core\adapter\ProcessorManager;
use Fearless\SmartPaymentRouting\core\PaymentRoutingService;
use Fearless\SmartPaymentRouting\core\processors\FlutterwaveProcessor;
use Fearless\SmartPaymentRouting\core\processors\PaystackProcessor;
use Illuminate\Http\Request;

class TransactionController extends Controller
{
        private readonly ProcessorManager $processorManager;

    public function __construct(ProcessorManager $processorManager)
    {
        $this->processorManager = $processorManager;
        // Register the processor for transactions within this class
        $processorManager->initialize([
            [
                'name' => 'paystack',
                'processor' => new PaystackProcessor(),
            ],
            [
                'name'  => 'flutterwave',
                'processor' => new FlutterwaveProcessor(),
            ]
        ]);
    }

    /**
     * Store a newly created resource in storage.
     */
    public function store(CreatePaymentRequest $request)
    {

        // Initialize the payment routing service
        $paymentRoutingService = new PaymentRoutingService($this->processorManager);
        $transaction = [
            'user_email' => $request->get('email'),
            'amount' => $request->get('amount'),
            'currency' => $request->get('currency'),
        ];

        // Route transaction to the best processor
        return $paymentRoutingService->makeCharge($this->processorManager, $transaction);
    }

}
  1. 要添加任何其他支付处理器,只需运行以下命令发布配置文件。
  php artisan vendor:publish --tag smart-payment-routing-config 

然后创建一个新的支付处理器类(例如 StripeProcessor)并实现 PaymentProcessor 接口,如下所示。

<?php

namespace App\Http\Services;

use Fearless\SmartPaymentRouting\core\contract\PaymentProcessor;
use GuzzleHttp\Exception\GuzzleException;
use MusahMusah\LaravelMultipaymentGateways\Exceptions\HttpMethodFoundException;
use MusahMusah\LaravelMultipaymentGateways\Exceptions\InvalidConfigurationException;
use Symfony\Component\HttpKernel\Exception\NotAcceptableHttpException;

class StripeProcessor implements PaymentProcessor
{
    public function createPayment($user_email, $amount, $currency)
    {
        $priceId = '***************';

        try {

            $stripe = new \Stripe\StripeClient(getenv('STRIPE_SECRET'));
           return $stripe->checkout->sessions->create([
                'success_url' => 'https://example.com/success',
                'line_items' => [
                    [
                        'price' => $priceId,
                        'quantity' => 2,
                    ],
                ],
                'mode' => 'payment',
            ]);
        } catch (GuzzleException|HttpMethodFoundException|InvalidConfigurationException $e) {
            throw new NotAcceptableHttpException($e->getMessage());
        }
    }
}

确保您已将所需的环境变量添加到 .env 文件中,如下所示。

 
 STRIPE_SECRET=**********
 STRIPE_WEBHOOK_SECRET=**********

最后,如以下所示更新 processorManager 以注册新的支付处理器。

class TransactionController extends Controller
{
   private readonly ProcessorManager $processorManager;

    public function __construct(ProcessorManager $processorManager)
    {
        $this->processorManager = $processorManager;
        // Register the processor for transactions within this class
        $processorManager->initialize([
            [
                'name' => 'paystack',
                'processor' => new PaystackProcessor(),
            ],
            [
                'name'  => 'flutterwave',
                'processor' => new FlutterwaveProcessor(),
            ],
            [
                'name'  => 'stripe',
                'processor' => new StripeProcessor(),
            ]
        ]);
    }

}

贡献

欢迎拉取请求。对于重大更改,请首先打开一个问题以讨论您想要更改的内容。

请确保适当更新测试。

许可证

MIT