larva/laravel-pay

This is a pay.

1.0.6 2023-03-10 07:16 UTC

This package is auto-updated.

Last update: 2024-11-30 16:37:33 UTC


README

Stable Version Total Downloads License

这是一个内部收单系统,依赖 yansongda/pay 这个组件,本收单系统,统一了调用。 备注,交易单位是分;2.x 和 3.x 版本对外接口一致,只是内部调用的第三方接口版本不同,本扩展拉齐了开发体验;

环境需求

  • PHP ^8.0.2

安装

composer require "larva/laravel-pay"

事件

AppServiceProvider 的 boot 中注册 路由

\Larva\Pay\Pay::routes();

在中间件 App\Http\Middleware\VerifyCsrfToken 排除支付回调相关的路由,如:

protected $except = [
    // ...
    'pay',
];

你自己的订单关联

/**
 * @property Charge $change
 */
class Order extends Model {

    /**
     * Get the entity's charge.
     * 这里关联付款模型
     * @return \Illuminate\Database\Eloquent\Relations\MorphOne
     */
    public function charge()
    {
        return $this->morphOne(Charge::class, 'order');
    }

    /**
     * 设置交易成功
     */
    public function markSucceeded()
    {
        $this->update(['channel' => $this->charge->trade_channel, 'status' => static::STATUS_PAY_SUCCEEDED, 'succeeded_at' => $this->freshTimestamp()]);
    }

    /**
     * 设置交易失败
     */
    public function markFailed()
    {
        $this->update(['status' => static::STATUS_FAILED]);
    }

    /**
     * 发起退款
     * @param string $reason 退款描述
     * @return Model|Refund
     * @throws Exception
     */
    public function refund(string $reason)
    {
        if ($this->paid && $this->charge->allowRefund) {
            $refund = $this->charge->refund($reason);
            $this->update(['refunded' => true]);
            return $refund;
        }
        throw new Exception ('Not paid, no refund.');
    }
}