Mục lục

Các bước chung

  • Xác định các dịch vụ bên thứ ba cần tích hợp
  • Định nghĩa một interface chung cho tất cả các dịch vụ
  • Triển khai interface cho từng dịch vụ
  • Tạo một lớp context để quản lý các dịch vụ
  • Sử dụng dependency injection để chọn dịch vụ phù hợp

Triển khai Strategy Pattern

Định nghĩa strategy interface:

interface PaymentGatewayInterface
{
    public function processPayment(array $paymentDetails): bool;
}

Triển khai các strategy cụ thể:

class StripePayment implements PaymentGatewayInterface
{
    public function processPayment(array $paymentDetails): bool
    {
        // Stripe-specific implementation
    }
}
class PayPalPayment implements PaymentGatewayInterface
{
    public function processPayment(array $paymentDetails): bool
    {
        // PayPal-specific implementation
    }
}

Tạo một lớp context:

class PaymentProcessor
{
    private $gateway;

    public function __construct(PaymentGatewayInterface $gateway)
    {
        $this->gateway = $gateway;
    }

    public function pay(array $paymentDetails): bool
    {
        return $this->gateway->processPayment($paymentDetails);
    }
}

Ví dụ triển khai trong Laravel

Cài đặt các package cần thiết:

composer require stripe/stripe-php
composer require paypal/rest-api-sdk-php

Tạo service providers:

php artisan make:provider StripeServiceProvider
php artisan make:provider PayPalServiceProvider

Triển khai service providers:

// app/Providers/StripeServiceProvider.php
public function register()
{
    $this->app->bind(PaymentGatewayInterface::class, function ($app) {
        return new StripePayment(config('services.stripe.secret'));
    });
}
// app/Providers/PayPalServiceProvider.php
public function register()
{
    $this->app->bind(PaymentGatewayInterface::class, function ($app) {
        return new PayPalPayment(config('services.paypal.client_id'), config('services.paypal.secret'));
    });
}

Cập nhật config/app.php:
Sự lựa chọn giữa các providers này thường được thực hiện trong tệp config/app.php hoặc thông qua các biến môi trường. Bạn sẽ kích hoạt một provider và nhận xét hoặc loại bỏ provider còn lại:

'providers' => [
    // ...
    App\Providers\StripeServiceProvider::class,
    // App\Providers\PayPalServiceProvider::class,
],

Sử dụng trong controller:

class PaymentController extends Controller
{
    private $paymentProcessor;

    public function __construct(PaymentProcessor $paymentProcessor)
    {
        $this->paymentProcessor = $paymentProcessor;
    }

    public function processPayment(Request $request)
    {
        $paymentDetails = $request->validated();
        $result = $this->paymentProcessor->pay($paymentDetails);

        return $result ? response()->json(['status' => 'success']) : response()->json(['status' => 'failure'], 400);
    }
}

Việc triển khai này cho phép dễ dàng chuyển đổi giữa các cổng thanh toán và thêm các cổng mới mà không cần sửa đổi mã hiện có.

Sử dụng Factory - Một cách triển khai khác

Phương thức thanh toán được gửi từ frontend dưới dạng 'payment_method'.
Controller lấy giá trị này bằng cách sử dụng $request->input('payment_method').

// In your controller
public function processPayment(Request $request)
{
    $gatewayName = $request->input('payment_method');
    $paymentDetails = $request->input('payment_details');

    try {
        $gateway = PaymentGatewayFactory::create($gatewayName);
        $result = $gateway->processPayment($paymentDetails);
        // Handle successful payment
    } catch (\Exception $e) {
        // Handle errors
    }
}

PaymentGatewayFactory tạo đối tượng cổng phù hợp dựa trên phương thức thanh toán.

// PaymentGatewayFactory.php
class PaymentGatewayFactory
{
    public static function create($gatewayName)
    {
        switch ($gatewayName) {
            case 'paypal':
                return new PayPalGateway();
            case 'stripe':
                return new StripeGateway();
            default:
                throw new \Exception("Unsupported gateway");
        }
    }
}

Controller sau đó sử dụng đối tượng cổng này để xử lý thanh toán.
Cách tiếp cận này cho phép bạn dễ dàng thêm các cổng thanh toán mới bằng cách tạo các lớp mới triển khai PaymentGatewayInterface và thêm chúng vào phương thức factory.

Xin chào! Cám ơn bạn đã đọc bài viết có tiêu đề Tích hợp Nhiều Dịch vụ Bên Thứ Ba với Strategy Pattern trong Laravel của tác giả Vũ Lê Huân. Trong trường hợp bạn muốn tìm hiểu thêm thông tin về tác giả, bạn có thể truy cập .