rainwaves/paygate-payment

一个用于集成PayGate支付网关的软件包

v1.2.0 2023-07-09 18:39 UTC

This package is auto-updated.

Last update: 2024-09-09 21:12:23 UTC


README

PayGate支付包提供了一种方便的方式,用于与PayGate支付网关集成以处理在线支付。如需更多信息,请访问PayGate文档

安装

您可以通过Composer安装PayGate支付包。在您的项目目录中运行以下命令

composer require rainwaves/paygate-payment

配置

安装包后,您需要使用您的PayGate凭据进行配置。在Laravel中,您需要发布配置文件并在您的.env文件中设置凭据:- PAYGATE_ID=XXXXXXXX

  • PAYGATE_SECRET=XXXXXXXXXX
  • PAYGATE_NOTIFICATION_URL=XXXXXXXX
  • PAYGATE_RETURN_URL=XXXXXXXXXXXX
  • PAYGATE_SUBS_RETURN_URL=XXXXXXXXXXXX

用法

纯PHP

require 'vendor/autoload.php';

// Create an instance of PayWebClient with your PayGate credentials
$client = new PayWeb('10011072130', 'secret');
$transactionDate = Carbon::now()->format('Y-m-d H:i:s');

// Initialize payment
$inputData = [
    'reference' => 'order_123',
    'amount' => 1599.00,
    'currency' => 'ZAR',
    'returnUrl' => 'https://example.com/return',
    'notifyUrl' => 'https://example.com/notify',
    'transactionDate'   => $transactionDate,
    'locale' => 'en-za',
    'country' => 'ZAF',
    'email' => 'customer@paygate.co.za',
];
$response = $client->initiatePayment($inputData);

// Generate the payment form
$formHtml = $client->createForm();

// Display the payment form to the user
echo $formHtml;

将生成一个表单,用于将数据提交给Paygate进行支付。

Laravel

假设您已设置必要的路由和视图,以下是Laravel中使用PayGate支付包的示例

class PaymentController extends Controller
{
    public Collection $products;
    protected PayWebInterface $payWeb;
    protected PaySubsInterface $paySubs;

    public function __construct(PayWebInterface $payWeb, PaySubsInterface $paySubs)
    {
        $this->products = $this->setProducts();
        $this->payWeb = $payWeb;
        $this->paySubs = $paySubs;

    }

    public function products()
    {
        $products = $this->products->toArray();
        return view('welcome', compact('products'));
    }
    public function response(Request $request)
    {
        $response = new PayGateNotifyResponse($request->toArray());
        Log::debug(json_encode("========================Return response================================"));
        Log::debug(json_encode($request->all()));
    }

    public function notify(Request $request)
    {
        $response = new PayGateNotifyResponse($request->toArray());
        Log::debug(json_encode("========================Notify response================================"));
        Log::debug(json_encode($request->all()));
    }

    public function payment($id)
    {
        $transactionDate = Carbon::now()->format('Y-m-d H:i:s');

        $product = $this->products->first(function ($product) use ($id) {
            return $product['id'] === (int)$id;
        });
        $inputData = [
            'reference' => uniqid('pgtest_'),
            'amount' => $product['price'],
            'currency' => 'ZAR',
            'returnUrl' => config('paygate.return_url'),
            'notifyUrl' => config('paygate.notification_url'),
            'transactionDate'   => $transactionDate,
            'locale' => 'en-za',
            'country' => 'ZAF',
            'email' => 'customer@paygate.co.za',
        ];

        $this->payWeb->initiatePayment($inputData);
        $formHtml = $this->payWeb->createForm();

        return view('payment', compact('product', 'formHtml'));
    }

    public function subscription()
    {
        $transactionDate = Carbon::now()->format('Y-m-d H:i:s');
        $startDate = Carbon::now()->addMonth()->startOfMonth()->format('Y-m-d');
        $endDate = Carbon::now()->addYear()->endOfMonth()->format('Y-m-d');
        $data = array(
            'version'            => 21,
            'reference'          => 'pgtest_123456789',
            'amount'             => 20000.00,
            'currency'           => 'ZAR',
            'returnUrl'         => 'https://fec0-41-216-202-254.ngrok-free.app/sub-response',
            'transactionDate'   => $transactionDate,
            'subsStartDate'    => $startDate,
            'subsEndDate'      => $endDate,
            'subsFrequency'     => 228,
            'processNow'        => 'YES',
            'processNowAmount' => 32.99,
        );

        $htmlForm = $this->paySubs->createSubscriptionAndChain($data)->createForm();

        return view('form', compact('htmlForm'));
    }

    public function subResponse(Request $request)
    {
        Log::debug(json_encode("========================Subscription response================================"));
        Log::debug(json_encode($request->all()));
    }
    private function setProducts(): Collection
    {
        return collect([
             [
                'id'=> 1,
                'name' => 'Product 1',
                'description' => 'Description of Product 1',
                'price' => 321.99,
            ],
            [
                'id'=> 2,
                'name' => 'Product 2',
                'description' => 'Description of Product 2',
                'price' => 245.99,
            ],
            [
                'id'=> 3,
                'name' => 'Product 3',
                'description' => 'Description of Product 3',
                'price' => 159.99,
            ],
        ]);
    }
}

请确保将 'your_paygate_id' 和 'your_encryption_key' 替换为您实际的PayGate ID和加密密钥。

测试

要运行包的PHPUnit测试用例,请使用以下命令

vendor/bin/phpunit

许可证

本软件包是开源软件,根据MIT许可证授权。

请确保将 'your_paygate_id''your_encryption_key' 替换为您实际的PayGate ID和加密密钥。

请根据您的包的具体功能和用法说明,自由地进一步自定义README。