asciisd/knet

Knet 包提供了一种表达性强、流畅的接口,用于访问 KNet 的支付服务。

v3.2.0 2024-02-27 00:32 UTC

README

Latest Version on Packagist Software License Total Downloads

Knet

此包曾用于与新 Knet 支付门户集成

用法

以下是一些简短的示例,展示您可以做什么

第一步

HasKnet 特性添加到 User 模型

namespace App;

use Illuminate\Foundation\Auth\User as Authenticatable;
use Asciisd\Knet\HasKnet;

class User extends Authenticatable {
   use HasKnet;
}

第二步

用户 pay() 方法

try{
    $user = User::find(1);
    $payment = $user->pay(10);
    $payment->url; // this will return payment link
} catch(\Asciisd\Knet\Exceptions\PaymentActionRequired $exception) {
    return redirect($exception->payment->actionUrl());
}

支付完成后,您将被重定向到 /knet/response,您可以通过配置文件更改它以创建自己的处理器

另一个示例

您可以在控制器内部这样使用 pay() 方法

try{
    $payment = request()->user()->pay(request()->amount, [
        'udf1' => request()->user()->name,
        'udf2' => request()->user()->email
    ]);
} catch(\Asciisd\Knet\Exceptions\PaymentActionRequired $exception) {
    // do whatever you want with this 
    $payment = $exception->payment;
} finally {
    // redirect user to payment url to complete the payment
    return $payment->actionUrl();
}

更改环境

如果您想确保一切正常工作,可以将您的环境从本地更改为生产,为此请按以下方式更改 .env 文件

APP_ENV=local #or production

KENT_TRANSPORT_ID=
KENT_TRANSPORT_PASSWORD=
KENT_RESOURCE_KEY=
KNET_DEBUG=true #or false in production

安装

您可以通过 Composer 安装绑定。运行以下命令

$ composer require asciisd/knet

运行安装命令

此命令将安装 ServiceProviderConfigsviews

php artisan knet:install

运行发布命令

此命令将发布 knet 资产

php artisan knet:publish

迁移发布后,您可以通过运行迁移来创建 knet_transactions

php artisan migrate

KnetServiceProvider

此包提供了一个收据系统,但您应该在 KnetServiceProvider => $details 数组中填写您的身份信息,还需要在 vendor => knet 公共资产中更新您的标志

测试卡

事件

您可以将此代码添加到 EventServiceProvider

KnetTransactionCreated::class => [
    // this event hold the new transaction instance
    // you can get this transaction inside the listner by $event->transaction
];

KnetTransactionUpdated::class => [
    // this event hold the updated transaction instance
    // you can get this transaction inside the listner by $event->transaction
];

KnetResponseReceived::class => [
    // this event hold the knet response array()
    // you can get this payload inside the listner by $event->payload
];

KnetResponseHandled::class => [
    // this event hold the knet response array()
    // you can get this payload inside the listner by $event->payload
];

KnetReceiptSeen::class => [
    // this event hold the knet Payment as $payment
];