微信支付服务
parent
8c9e14ec0c
commit
334e6013aa
|
|
@ -0,0 +1,36 @@
|
|||
<?php
|
||||
|
||||
namespace App\Exceptions;
|
||||
|
||||
use Exception;
|
||||
|
||||
class WeChatPayException extends Exception
|
||||
{
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
protected $raw;
|
||||
|
||||
/**
|
||||
* @param string|null $message
|
||||
* @param array $raw
|
||||
*/
|
||||
public function __construct(?string $message = null, array $raw = [])
|
||||
{
|
||||
parent::__construct($message);
|
||||
|
||||
$this->raw = $raw;
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信支付上下文
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function context()
|
||||
{
|
||||
return [
|
||||
'wechat_raw' => $this->raw,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,90 @@
|
|||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Exceptions\WeChatPayException;
|
||||
use EasyWeChat\Factory;
|
||||
use EasyWeChat\Payment\Application;
|
||||
|
||||
class WeChatPayService
|
||||
{
|
||||
// 小程序交易类型
|
||||
// https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=4_2
|
||||
public const TRADE_TYPE_JSAPI = 'JSAPI'; // JSAPI支付
|
||||
public const TRADE_TYPE_APP = 'APP'; // App支付
|
||||
public const TRADE_TYPE_NATIVE = 'NATIVE'; // Native支付
|
||||
public const TRADE_TYPE_H5 = 'MWEB'; // H5支付
|
||||
|
||||
/**
|
||||
* @var array
|
||||
*/
|
||||
public static $allowTradeTypes = [
|
||||
self::TRADE_TYPE_JSAPI,
|
||||
self::TRADE_TYPE_APP,
|
||||
];
|
||||
|
||||
/**
|
||||
* @var \EasyWeChat\Payment\Application
|
||||
*/
|
||||
protected $app;
|
||||
|
||||
/**
|
||||
* @param \EasyWeChat\Payment\Application|null $app
|
||||
*/
|
||||
public function __construct(?Application $app = null)
|
||||
{
|
||||
if ($app === null) {
|
||||
$app = Factory::payment(config('wechat.payment.default'));
|
||||
}
|
||||
|
||||
$this->app = $app;
|
||||
}
|
||||
|
||||
/**
|
||||
* 支付
|
||||
*
|
||||
* @param array $params
|
||||
* @return array
|
||||
*
|
||||
* @throws \App\Exceptions\WeChatPayException
|
||||
*/
|
||||
public function pay(array $params)
|
||||
{
|
||||
// 如果交易类型不存在,则使用 App 支付
|
||||
if (! isset($params['trade_type'])) {
|
||||
$params['trade_type'] = static::TRADE_TYPE_APP;
|
||||
}
|
||||
|
||||
if (! in_array($params['trade_type'], static::$allowTradeTypes)) {
|
||||
throw new WeChatPayException(sprintf('交易类型 [%s] 暂不支持', $params['trade_type']), $params);
|
||||
}
|
||||
|
||||
$result = $this->app->order->unify($params);
|
||||
|
||||
if (data_get($result, 'return_code') !== 'SUCCESS') {
|
||||
throw new WeChatPayException(
|
||||
data_get($result, 'return_msg', '请求失败'),
|
||||
$params
|
||||
);
|
||||
}
|
||||
|
||||
if (data_get($result, 'err_code') !== 'SUCCESS') {
|
||||
throw new WeChatPayException(
|
||||
sprintf(
|
||||
'[%s] %s',
|
||||
data_get($result, 'err_code', '-1'),
|
||||
data_get($result, 'err_code_des', '交易失败')
|
||||
),
|
||||
$params
|
||||
);
|
||||
}
|
||||
|
||||
$prepayId = $result['prepay_id'];
|
||||
|
||||
if ($params['trade_type'] === static::TRADE_TYPE_APP) {
|
||||
return $this->app->jssdk->appConfig($prepayId);
|
||||
}
|
||||
|
||||
return $this->app->jssdk->bridgeConfig($prepayId, false);
|
||||
}
|
||||
}
|
||||
|
|
@ -21,7 +21,8 @@
|
|||
"laravel/sanctum": "^2.12",
|
||||
"laravel/tinker": "^2.5",
|
||||
"overtrue/easy-sms": "^2.0",
|
||||
"tucker-eric/eloquentfilter": "^3.0"
|
||||
"tucker-eric/eloquentfilter": "^3.0",
|
||||
"w7corp/easywechat": "^5.10"
|
||||
},
|
||||
"require-dev": {
|
||||
"facade/ignition": "^2.5",
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -0,0 +1,15 @@
|
|||
<?php
|
||||
|
||||
return [
|
||||
'payment' => [
|
||||
'default' => [
|
||||
'sandbox' => env('WECHAT_PAYMENT_SANDBOX', false),
|
||||
'app_id' => env('WECHAT_PAYMENT_APPID'),
|
||||
'mch_id' => env('WECHAT_PAYMENT_MCH_ID'),
|
||||
'key' => env('WECHAT_PAYMENT_KEY'),
|
||||
'cert_path' => env('WECHAT_PAYMENT_CERT_PATH'), // 绝对地址
|
||||
'key_path' => env('WECHAT_PAYMENT_KEY_PATH'), // 绝对地址
|
||||
'notify_url' => env('WECHAT_PAYMENT_NOTIFY_URL'),
|
||||
],
|
||||
],
|
||||
];
|
||||
Loading…
Reference in New Issue