https://github.com/larvatecn/laravel-pay
Pay Extension for Laravel.
https://github.com/larvatecn/laravel-pay
Last synced: 4 months ago
JSON representation
Pay Extension for Laravel.
- Host: GitHub
- URL: https://github.com/larvatecn/laravel-pay
- Owner: larvatecn
- License: other
- Created: 2022-04-10T05:40:46.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-01-01T14:35:31.000Z (over 1 year ago)
- Last Synced: 2025-02-05T06:13:10.107Z (5 months ago)
- Language: PHP
- Size: 34.2 KB
- Stars: 1
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# laravel-pay
这是一个内部收单系统,依赖 `yansongda/pay` 这个组件,本收单系统,统一了调用。
备注,交易单位是分;2.x 和 3.x 版本对外接口一致,只是内部调用的第三方接口版本不同,本扩展拉齐了开发体验;## 环境需求
- PHP ^8.0.2
## 安装
```bash
composer require "larva/laravel-pay"
```### 事件
| **事件** | **描述** |
|---------------------------------------|--------|
| `\Larva\Pay\Events\ChargeClosed` | 交易已关闭 |
| `\Larva\Pay\Events\ChargeFailed` | 交易失败 |
| `\Larva\Pay\Events\ChargeSucceeded` | 交易已支付 |
| `\Larva\Pay\Events\RefundFailed` | 交易已关闭 |
| `\Larva\Pay\Events\RefundSucceeded` | 交易已关闭 |
| `\Larva\Pay\Events\TransferFailed` | 交易已关闭 |
| `\Larva\Pay\Events\TransferSucceeded` | 交易已关闭 |AppServiceProvider 的 boot 中注册 路由
```php
\Larva\Pay\Pay::routes();
```在中间件 `App\Http\Middleware\VerifyCsrfToken` 排除支付回调相关的路由,如:
```php
protected $except = [
// ...
'pay',
];
```你自己的订单关联
```php
/**
* @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.');
}
}
```