0

我想让我的付款流程自动注入,但实例将取决于当前用户使用的货币。

我创建了这样的界面

<?php

namespace App\Payment;

use App\Models\Transaction;
use App\Models\User;
use Illuminate\Http\Request;

interface PaymentService
{
    public function withdraw(\App\Models\Withdrawal $withdrawal, String $currency) : array;
    public function makePayment(Transaction $transaction, String $callback_url) : String;

    // Should return a 1-level array/object with status, currency and other relevant data
    public function verifyTransaction(String $transactionID = null);
    public function verifyWebHook() : bool;
    public function fetchBanks() : array;
    public function resolveAccount(String $accountnumber, String $bank);
    public function verifyTransfer(String $id) : array;
    public function generateReference() : String;
    public function getReferenceFromCallback() : String;
    public function isSuccessfulAndValid($data, Transaction $transaction) : bool;
}

然后我创建了另外两个实现接口的类

颤振支付

<?php
namespace App\Payment;


class FlutterPayment implements PaymentService
{
    ...
}

柜员付款

<?php
namespace App\Payment;


class ThetellerPayment implements PaymentService
{
    ...
}

下面是我的PaymentServiceProvider的注册功能

public function register()
    {
        $this->app->bind(PaymentService::class, function ($app) {
            $currency = auth()->user()->currency();

            if ($currency == config('currency.list.nigeria')) {
                return FlutterPayment::class;
            }
            if ($currency == config('currency.list.ghana')) {
                return ThetellerPayment::class;
            }

            throw new \Exception('Invalid currency processing');
        });
    }

我现在面临的问题是我无法访问经过身份验证的用户,这对于决定返回哪个类非常重要。

如果您有另一种在 laravel 中实现策略模式的方法,我也将不胜感激。

4

0 回答 0