Loading

适用于 Laravel 的内部收单模块

适用于 Laravel 的内部收单模块

这是一个内部收单系统,依赖 yansongda/laravel-pay 这个组件,本收单系统,统一了调用。
备注,交易单位是分;

环境需求

  • PHP >= 7.1.3

安装

composer require larva/laravel-transaction -vv

设置

add the config file: config/transaction.php

AppServiceProvider 的 boot 中注册 路由

Transaction::routes();

订单关联

/**
 * @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 setSucceeded()
    {
        $this->update(['pay_channel' => $this->charge->channel, 'status' => static::STATUS_PAY_SUCCEEDED, 'pay_succeeded_at' => $this->freshTimestamp()]);
    }

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

    /**
     * 发起退款
     * @param string $description 退款描述
     * @return Model|Refund
     * @throws Exception
     */
    public function setRefund($description)
    {
        if ($this->paid && $this->charge->allowRefund) {
            $refund = $this->charge->refunds()->create([
                'user_id' => $this->user_id,
                'amount' => $this->amount,
                'description' => $description,
                'charge_id' => $this->charge->id,
                'charge_order_id' => $this->id,
            ]);
            $this->update(['refunded' => true]);
            return $refund;
        }
        throw new Exception ('Not paid, no refund.');
    }
}
posted @ 2020-04-01 14:37  方圆百里找对手  阅读(225)  评论(0编辑  收藏  举报