初始上传

This commit is contained in:
2026-04-04 17:27:12 +08:00
parent 4d80d28eb4
commit b7e11774ee
11191 changed files with 1588469 additions and 0 deletions

340
addon/wechatpay/model/Config.php Executable file
View File

@@ -0,0 +1,340 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\wechatpay\model;
use app\model\system\Config as ConfigModel;
use app\model\BaseModel;
/**
* 微信支付配置
* 版本 1.0.4
*/
class Config extends BaseModel
{
private $encrypt = '******';
/**
* 设置支付配置
* @param $data
* @param int $site_id
* @param string $app_module
* @return array
*/
public function setPayConfig($data, $site_id = 0, $app_module = 'shop')
{
$config = new ConfigModel();
// 未加密前的数据
$original_config = $this->getPayConfig($site_id)['data']['value'];
// 检测数据是否发生变化,如果没有变化,则保持未加密前的数据
if (!empty($data['pay_signkey']) && $data['pay_signkey'] == $this->encrypt) {
$data['pay_signkey'] = $original_config['pay_signkey']; // APIv2密钥
}
if (!empty($data['apiclient_cert']) && $data['apiclient_cert'] == $this->encrypt) {
$data['apiclient_cert'] = $original_config['apiclient_cert']; // 支付证书cert
}
if (!empty($data['apiclient_key']) && $data['apiclient_key'] == $this->encrypt) {
$data['apiclient_key'] = $original_config['apiclient_key']; // 支付证书key
}
if (!empty($data['plateform_cert']) && $data['plateform_cert'] == $this->encrypt) {
$data['plateform_cert'] = $original_config['plateform_cert']; // 平台证书 生成的
}
if (!empty($data['plateform_certificate']) && $data['plateform_certificate'] == $this->encrypt) {
$data['plateform_certificate'] = $original_config['plateform_certificate']; // 平台证书 主动上传的
}
if (!empty($data['plateform_certificate_serial']) && $data['plateform_certificate_serial'] == $this->encrypt) {
$data['plateform_certificate_serial'] = $original_config['plateform_certificate_serial']; // 平台证书序列号
}
if (!empty($data['v3_pay_signkey']) && $data['v3_pay_signkey'] == $this->encrypt) {
$data['v3_pay_signkey'] = $original_config['v3_pay_signkey']; // APIv3密钥
}
if(!($data['transfer_status'] == 1 && $data['transfer_type'] == 'v3' && $data['transfer_v3_type'] == self::TRANSFER_V3_TYPE_USER)){
$data['member_transfer_scene'] = '';
$data['store_transfer_scene'] = '';
$data['fenxiao_transfer_scene'] = '';
$data['member_transfer_code'] = '';
$data['store_transfer_code'] = '';
$data['fenxiao_transfer_code'] = '';
$data['member_transfer_info'] = [];
$data['fenxiao_transfer_info'] = [];
$data['store_transfer_info'] = [];
$data['member_transfer_recv'] = '';
$data['store_transfer_recv'] = '';
$data['fenxiao_transfer_recv'] = '';
}
$res = $config->setConfig($data, '微信支付配置', 1, [['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'WECHAT_PAY_CONFIG']]);
return $res;
}
/**
* 获取支付配置
* @param int $site_id
* @param string $app_module
* @param bool $need_encrypt 是否需要加密数据true加密、false不加密
* @return array
*/
public function getPayConfig($site_id = 0, $app_module = 'shop', $need_encrypt = false)
{
$config = new ConfigModel();
$res = $config->getConfig([['site_id', '=', $site_id], ['app_module', '=', $app_module], ['config_key', '=', 'WECHAT_PAY_CONFIG']]);
//旧定义字段变为新定义字段
$res['data']['value'] = json_encode($res['data']['value']);
$res['data']['value'] = str_replace('member_transfer', 'member_withdraw', $res['data']['value']);
$res['data']['value'] = str_replace('store_transfer', 'store_withdraw', $res['data']['value']);
$res['data']['value'] = str_replace('fenxiao_transfer', 'fenxiao_withdraw', $res['data']['value']);
$res['data']['value'] = json_decode($res['data']['value'], true);
$res['data']['value'] = array_merge([
"appid" => '',
"mch_id" => '',
"pay_signkey" => '',
"apiclient_cert" => '',
"apiclient_key" => '',
"refund_status" => 0,
"pay_status" => 0,
"transfer_status" => 0,
'transfer_type' => 'v2',
'plateform_cert' => '',
'plateform_certificate' => '',
'plateform_certificate_serial' => '',
'api_type' => 'v2',
'v3_pay_signkey' => '',
'transfer_v3_type'=>'1',//旧版1 新版2
'member_withdraw_scene'=>'',
'store_withdraw_scene'=>'',
'fenxiao_withdraw_scene'=>'',
'member_withdraw_code'=>'',
'store_withdraw_code'=>'',
'fenxiao_withdraw_code'=>'',
'member_withdraw_info' => [],
'fenxiao_withdraw_info'=> [],
'store_withdraw_info' => [],
'member_withdraw_recv'=>'',
'store_withdraw_recv'=>'',
'fenxiao_withdraw_recv'=>'',
], $res['data']['value']);
// 加密敏感信息
if (!empty($res['data']['value']) && $need_encrypt) {
if (!empty($res['data']['value']['pay_signkey'])) {
$res['data']['value']['pay_signkey'] = $this->encrypt; // APIv2密钥
}
if (!empty($res['data']['value']['apiclient_cert'])) {
$res['data']['value']['apiclient_cert'] = $this->encrypt; // 支付证书cert
}
if (!empty($res['data']['value']['apiclient_key'])) {
$res['data']['value']['apiclient_key'] = $this->encrypt; // 支付证书key
}
if (!empty($res['data']['value']['plateform_cert'])) {
$res['data']['value']['plateform_cert'] = $this->encrypt; // 平台证书 通过接口获取和生成的
}
if (!empty($res['data']['value']['plateform_certificate'])) {
$res['data']['value']['plateform_certificate'] = $this->encrypt; // 平台证书,直接上传的
}
if (!empty($res['data']['value']['plateform_certificate_serial'])) {
$res['data']['value']['plateform_certificate_serial'] = $this->encrypt; // 平台证书ID
}
if (!empty($res['data']['value']['v3_pay_signkey'])) {
$res['data']['value']['v3_pay_signkey'] = $this->encrypt; // APIv3密钥
}
}
return $res;
}
CONST TRANSFER_V3_TYPE_SHOP = 1; //商户转账
CONST TRANSFER_V3_TYPE_USER = 2; //会员收款
public function getTransferSceneConfig()
{
$config = [
[
'num'=>1,
'title' => '现金营销',
'infos' => [
[
'info_type' => '活动名称',
'info_content' => ''
],
[
'info_type' => '奖励说明',
'info_content' => '',
],
],
'user_recv'=>[
'活动奖励',
'现金奖励'
]
],
[
'num'=>2,
'title' => '企业赔付',
'infos' => [
[
'info_type' => '赔付原因',
'info_content' => ''
],
],
'user_recv'=>[
'退款',
'商家赔付'
]
],
[
'num'=>3,
'title' => '佣金报酬',
'infos' => [
[
'info_type' => '岗位类型',
'info_content' => ''
],
[
'info_type' => '报酬说明',
'info_content' => '',
],
],
'user_recv'=>[
'劳务报酬',
'报销款',
'企业补贴',
'开工利是',
]
],
[
'num'=>4,
'title' => '采购货款',
'infos' => [
[
'info_type' => '采购商品名称',
'info_content' => ''
],
],
'user_recv'=>[
'货款',
]
],
[
'num'=>5,
'title' => '二手回收',
'infos' => [
[
'info_type' => '回收商品名称',
'info_content' => ''
],
],
'user_recv'=>[
'二手回收货款',
]
],
[
'num'=>6,
'title' => '公益补助',
'infos' => [
[
'info_type' => '公益活动名称',
'info_content' => ''
],
[
'info_type' => '公益活动备案编号',
'info_content' => ''
],
],
'user_recv'=>[
'公益补助金',
]
],
[
'num'=>7,
'title' => '行政补贴',
'infos' => [
[
'info_type' => '补贴类型',
'info_content' => ''
],
],
'user_recv'=>[
'行政补贴',
'行政奖励',
]
],
[
'num'=>8,
'title' => '保险理赔',
'infos' => [
[
'info_type' => '保险产品备案编号',
'info_content' => ''
],
[
'info_type' => '保险名称',
'info_content' => ''
],
[
'info_type' => '保险操作单号',
'info_content' => ''
],
],
'user_recv'=>[
'保险理赔款',
]
]
];
$data = [];
foreach ($config as $k=>$v){
$data[$v['num']] = $v;
}
return $data;
}
public function getTransferSceneInfo($param){
$data = [
'member_withdraw_info'=>[],
'fenxiao_withdraw_info'=>[],
'store_withdraw_info'=>[]
];
if(!isset($param['transfer_v3_type']) || $param['transfer_v3_type'] !=2){
return $data;
}
$config = $this->getTransferSceneConfig();
//会员提现场景
if(!empty($param['member_withdraw_scene'])){
foreach($config[$param['member_withdraw_scene']]['infos'] as $k=>$v){
$v['info_content'] = $param['member_withdraw_'.$k] ?? '';
$data['member_withdraw_info'][$k] = $v;
}
}
//分销提现场景
if(!empty($param['fenxiao_withdraw_scene'])){
foreach($config[$param['fenxiao_withdraw_scene']]['infos'] as $k=>$v){
$v['info_content'] = $param['fenxiao_withdraw_'.$k] ?? '';
$data['fenxiao_withdraw_info'][$k] = $v;
}
}
//店铺提现场景
if(!empty($param['store_withdraw_scene'])){
foreach($config[$param['store_withdraw_scene']]['infos'] as $k=>$v){
$v['info_content'] = $param['store_withdraw_'.$k] ?? '';
$data['store_withdraw_info'][$k] = $v;
}
}
return $data;
}
}

388
addon/wechatpay/model/Pay.php Executable file
View File

@@ -0,0 +1,388 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\wechatpay\model;
use addon\shopcomponent\model\Weapp;
use app\exception\ApiException;
use app\model\order\OrderMessage;
use app\model\system\Cron;
use app\model\BaseModel;
use addon\weapp\model\Config as WeappConfig;
use addon\wechat\model\Config as WechatConfig;
use app\model\system\Pay as PayModel;
use think\facade\Log;
use addon\wechatpay\model\Config as WechatPayConfig;
/**
* 微信支付配置
* 版本 1.0.4
*/
class Pay extends BaseModel
{
/**
* 支付接口实例
* @var
*/
private $app;
/**
* 是否是小程序端
* @var int
*/
private $is_weapp = 0;
/**
* 支付配置
* @var array|mixed
*/
private $config = [];
/**
* 微信支付接口版本
* @var string
*/
private $api = 'v2';
/**
* 站点id
* @var
*/
private $site_id;
public function __construct($is_weapp = 0, $site_id = 1)
{
$this->is_weapp = $is_weapp;
$this->site_id = $site_id;
// 支付配置
$config_model = new Config();
$this->config = $config_model->getPayConfig($site_id)[ 'data' ][ 'value' ];
if (empty($this->config)) throw new ApiException(-1, "平台未配置微信支付");
$this->api = $this->config[ 'api_type' ];
$this->config[ 'site_id' ] = $site_id;
if ($is_weapp == 0) {
$wechat_config = ( new WechatConfig() )->getWechatConfig($this->site_id)[ 'data' ][ 'value' ];
$this->config[ 'appid' ] = $wechat_config[ 'appid' ] ?? '';
} else {
$weapp_config = ( new WeappConfig() )->getWeappConfig($this->site_id)[ 'data' ][ 'value' ];
$this->config[ 'appid' ] = $weapp_config[ 'appid' ] ?? '';
}
$this->factory();
}
/**
* 实例化支付接口
*/
public function factory()
{
$class = 'addon\\wechatpay\\model\\' . ucfirst($this->api);
if (!class_exists($class)) throw new ApiException(-1, "Class '{$class}' not found");
try {
$this->app = new $class($this->config);
} catch (\Exception $e) {
Log::write('微信支付配置错误:' . $e->getMessage() . $e->getFile() . $e->getLine());
throw new ApiException(-1, $e->getMessage());
}
}
/**
*
* 生成支付
* @param $param
*/
public function pay($param)
{
if (!$this->config[ 'pay_status' ]) return $this->error([], '平台未启用微信支付');
//清除绑定支付数据
$pay_model = new PayModel();
$clear_res = $pay_model->clearMchPay($param[ "out_trade_no" ], 'wechatpay');
if($clear_res['code'] < 0) return $clear_res;
if ($param[ 'trade_type' ] == 'JSAPI' || $param[ 'trade_type' ] == 'APPLET') {
$member_info = model('member')->getInfo([ [ "member_id", "=", $param[ "member_id" ] ] ], 'weapp_openid,wx_openid');
if (empty($member_info)) return $this->error(-1, '未获取到会员信息');
$param[ 'openid' ] = $this->is_weapp ? $member_info[ 'weapp_openid' ] : $member_info[ 'wx_openid' ];
}
try {
$result = $this->app->pay($param);
//绑定支付数据
if($result['code'] >= 0){
$pay_model->bindMchPay($param[ "out_trade_no" ], [
"pay_type" => 'wechatpay',
"is_weapp" => $this->is_weapp,
"app_id" => $this->config[ "appid" ],
]);
}
// 对视频号订单做处理
if ($result[ 'code' ] == 0 && in_array($param[ 'scene' ], [ 1175, 1176, 1177, 1191, 1195 ])) {
$weapp_model = new Weapp($param[ 'site_id' ]);
$prepay_id = str_replace('prepay_id=', '', $result[ 'data' ][ 'data' ][ 'package' ]);
$order_info = $this->getOrderInfo($param[ "out_trade_no" ], $param[ 'openid' ], $prepay_id, $param[ 'scene' ]);
$res = $weapp_model->createOrder($order_info);
if ($res[ 'code' ] >= 0) {
$order_params = [
"order_id" => $res[ 'data' ][ 'data' ][ 'order_id' ],
"out_order_id" => $res[ 'data' ][ 'data' ][ 'out_order_id' ],
"openid" => $param[ "openid" ]
];
$config = $weapp_model->getPaymentParams($order_params);
$result = $this->success([
'type' => 'jsapi',
'data' => $config[ 'data' ][ 'payment_params' ]
]);
}
}
return $result;
} catch (\Exception $e) {
Log::write('微信支付接口调用失败,请求参数:' . json_encode($param) . ' 错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
return $this->error([], '微信支付接口调用失败');
}
}
/**
* 支付异步通知
* @return mixed
*/
public function payNotify()
{
return $this->app->payNotify();
}
/**
* 关闭支付
* @param $param
* @return array
*/
public function close($param)
{
try {
return $this->app->payClose($param);
} catch (\Exception $e) {
Log::write('微信订单关闭失败,请求参数:' . json_encode($param) . ' 错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
return $this->error([], '微信订单关闭失败');
}
}
/**
* 微信原路退款
* @param $param
* @return array
*/
public function refund($param)
{
if (!$this->config[ 'refund_status' ]) return $this->error([], '平台未启用微信退款');
try {
$mch_info = empty($param[ "pay_info" ][ 'mch_info' ]) ? [] : json_decode($param[ "pay_info" ][ 'mch_info' ], true);
$this->config[ "appid" ] = $mch_info[ "app_id" ] ?? $this->config[ "appid" ];//替换为商户自己的appid
$this->factory();
return $this->app->refund($param);
} catch (\Exception $e) {
Log::write('微信退款失败,请求参数:' . json_encode($param) . ' 错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
return $this->error(['file' => $e->getFile(),'line'=>$e->getLine(),'message'=>$e->getMessage()], '微信退款失败');
}
}
/**
* 微信转账到零钱
* @param array $param
* @return array
* @throws ApiException
*/
public function transfer(array $param)
{
if (!$this->config[ 'transfer_status' ]) return $this->error([], '平台未启用微信转账');
if ($this->api != $this->config[ 'transfer_type' ]) {
$this->api = $this->config[ 'transfer_type' ];
$this->factory();
}
try {
$result = $this->app->transfer($param);
return $result;
} catch (\Exception $e) {
Log::write('微信转账接口调用失败,错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
return $this->error([], '微信转账接口调用失败');
}
}
/**
* 获取支付所需订单信息
* @param $out_trade_no
* @param $openid
* @param $prepay_id
* @param int $scene
* @return array
*/
private function getOrderInfo($out_trade_no, $openid, $prepay_id, $scene = 0)
{
$order_info = model('order')->getInfo([ [ 'out_trade_no', '=', $out_trade_no ] ], 'site_id,create_time,order_id,order_type,member_id,order_money,delivery_money,name,full_address,mobile,delivery_store_info,delivery_store_name');
$data = [
'create_time' => date("Y-m-d H:i:s", $order_info[ 'create_time' ]),
'out_order_id' => $order_info[ 'order_id' ],
'openid' => $openid,
'path' => ( new OrderMessage() )->handleUrl($order_info[ 'order_type' ], $order_info[ 'order_id' ]),
'out_user_id' => $order_info[ 'member_id' ],
'order_detail' => [
'product_infos' => [],
'pay_info' => [
'pay_method' => '微信支付',
'prepay_id' => $prepay_id,
'prepay_time' => date('Y-m-d H:i:s', time())
],
'price_info' => [
'order_price' => $order_info[ 'order_money' ] * 100,
'freight' => $order_info[ 'delivery_money' ] * 100,
'discounted_price' => 0,
'additional_price' => 0
]
],
'delivery_detail' => [],
'fund_type' => 1,
'expire_time' => time() + 3600
];
// 配送方式
switch ( $order_info[ 'order_type' ] ) {
case 2:
$delivery_store_info = json_decode($order_info[ 'delivery_store_info' ], true);
$data[ 'delivery_detail' ][ 'delivery_type' ] = 4; //用户自提
$data[ 'address_info' ] = [
'receiver_name' => $order_info[ 'delivery_store_name' ],
'detailed_address' => $delivery_store_info[ 'full_address' ],
'tel_number' => $delivery_store_info[ 'telphone' ]
];
break;
case 3:
$data[ 'delivery_detail' ][ 'delivery_type' ] = 3; //线下配送
break;
case 4:
$data[ 'delivery_detail' ][ 'delivery_type' ] = 2; //无需快递
break;
default:
$data[ 'delivery_detail' ][ 'delivery_type' ] = 1; //正常快递
$data[ 'address_info' ] = [
'receiver_name' => $order_info[ 'name' ],
'detailed_address' => $order_info[ 'full_address' ],
'tel_number' => $order_info[ 'mobile' ]
];
break;
}
$order_goods = model('order_goods')->getList([ [ 'order_id', '=', $order_info[ 'order_id' ] ] ], 'goods_id,sku_id,num,price,sku_name,sku_image');
foreach ($order_goods as $goods) {
$data['order_detail']['product_infos'][] = [
'out_product_id' => $goods['goods_id'],
'out_sku_id' => $goods['sku_id'],
'product_cnt' => numberFormat($goods['num']),
'sale_price' => $goods['price'] * 100,
'path' => 'pages/goods/detail?sku_id=' . $goods['sku_id'],
'title' => $goods['sku_name'],
'head_img' => img($goods['sku_image']),
'sku_real_price' => $goods['price'] * 100
];
}
$video_number_scene = [ 1175, 1176, 1177, 1191, 1195 ]; // 视频号场景值
if (in_array($scene, $video_number_scene)) model('order')->update([ 'is_video_number' => 1 ], [ [ 'order_id', '=', $order_info[ 'order_id' ] ] ]);
return $data;
}
/**
* 付款码支付
* @param $param
* @return array|mixed|void
* @throws ApiException
*/
public function micropay($param)
{
if (!$this->config[ 'pay_status' ]) return $this->error([], '平台未启用微信支付');
if ($this->api != 'v2') {
$this->api = 'v2';
$this->factory();
}
try {
$pay_model = new PayModel();
//清空绑定支付数据
$clear_res = $pay_model->clearMchPay($param[ "out_trade_no" ], 'wechatpay');
if($clear_res['code'] < 0) return $clear_res;
//绑定支付数据
$pay_model->bindMchPay($param[ "out_trade_no" ], [
"pay_type" => 'wechatpay',
"app_id" => $this->config[ "appid" ],
]);
$res = $this->app->micropay($param);
if ($res[ 'code' ] != 0) {
if (isset($res['data']['err_code'])) {
if ($res['data']['err_code'] == 'TRADE_ERROR'){
(new PayModel())->edit(['pay_status' => PayModel::PAY_STATUS_CANCEL], ['out_trade_no' => $param[ "out_trade_no" ] ]);
}
}
return $res;
}
$res = $pay_model->onlinePay($param[ 'out_trade_no' ], 'wechatpay', $res[ 'data' ][ 'transaction_id' ], 'wechatpay');
return $res;
} catch (\Exception $e) {
Log::write('微信付款码支付失败,请求参数:' . json_encode($param) . ' 错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
return $this->error([], '微信付款码支付失败');
}
}
/**
* 微信转账到零钱新版本回调
*/
public function transferNotify()
{
if (!$this->config[ 'transfer_status' ]) return $this->error([], '平台未启用微信转账');
if ($this->api != $this->config[ 'transfer_type' ]) {
$this->api = $this->config[ 'transfer_type' ];
$this->factory();
}
try {
return $this->app->transferNotify();
} catch (\Exception $e) {
Log::write('微信转账接口回调处理失败,错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
return $this->error([], '微信转账接口回调处理失败');
}
}
/**
* 撤销微信转账
* @param $out_bill_no 商户单号
* @return array
*/
public function transferCancel($out_bill_no){
if (!$this->config[ 'transfer_status' ]) return $this->error([], '平台未启用微信转账');
if ($this->api != $this->config[ 'transfer_type' ]) {
$this->api = $this->config[ 'transfer_type' ];
$this->factory();
}
try {
return $this->app->transferCancel($out_bill_no);
} catch (\Exception $e) {
Log::write('微信转账接口回调处理失败,错误原因:' . $e->getMessage() . $e->getFile() . $e->getLine());
return $this->error([], '微信转账接口回调处理失败');
}
}
}

280
addon/wechatpay/model/V2.php Executable file
View File

@@ -0,0 +1,280 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\wechatpay\model;
use app\model\BaseModel;
use app\model\system\Cron;
use app\model\system\Pay as PayCommon;
use app\model\upload\Upload;
use EasyWeChat\Factory;
use think\facade\Log;
/**
* 微信支付v2支付
* 版本 1.0.4
*/
class V2 extends BaseModel
{
/**
* 微信支付配置
*/
private $config;
/**
* 支付实例
* @var
*/
private $app;
public function __construct($config)
{
$this->config = $config;
$this->app = Factory::payment([
'app_id' => $config[ 'appid' ], //应用id
'mch_id' => $config[ "mch_id" ] ?? '', //商户号
'key' => $config[ "pay_signkey" ] ?? '', // API 密钥
// 如需使用敏感接口(如退款、发送红包等)需要配置 API 证书路径(登录商户平台下载 API 证书)
'cert_path' => realpath($config[ "apiclient_cert" ]) ?? '', // apiclient_cert.pem XXX: 绝对路径!!!!
'key_path' => realpath($config[ "apiclient_key" ]) ?? '', // apiclient_key.pem XXX: 绝对路径!!!!
'notify_url' => '',// 你也可以在下单时单独设置来想覆盖它
'response_type' => 'array',
'log' => [
'level' => 'debug',
'permission' => 0777,
'file' => 'runtime/log/wechat/easywechat.logs',
],
'sandbox' => false, // 设置为 false 或注释则关闭沙箱模式
]);
}
/**
* 支付
* @param array $param
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function pay(array $param)
{
$data = [
'body' => str_sub($param[ "pay_body" ], 15),
'out_trade_no' => $param[ "out_trade_no" ],
'total_fee' => $param[ "pay_money" ] * 100,
'notify_url' => $param[ "notify_url" ],
'trade_type' => $param[ "trade_type" ] == 'APPLET' ? 'JSAPI' : $param[ "trade_type" ]
];
if ($param[ 'trade_type' ] == 'JSAPI' || $param[ 'trade_type' ] == 'APPLET') $data[ 'openid' ] = $param[ 'openid' ];
$result = $this->app->order->unify($data);
if ($result[ "return_code" ] == 'FAIL') return $this->error([], $result[ "return_msg" ]);
if ($result[ "result_code" ] == 'FAIL') return $this->error([], $result[ "err_code_des" ]);
$return = [];
switch ( $param[ "trade_type" ] ) {
case 'JSAPI' ://微信支付 或小程序支付
$return = [
"type" => "jsapi",
"data" => $this->app->jssdk->sdkConfig($result[ 'prepay_id' ], false)
];
break;
case 'APPLET' ://微信支付 或小程序支付
$return = [
"type" => "jsapi",
"data" => $this->app->jssdk->bridgeConfig($result[ 'prepay_id' ], false)
];
break;
case 'NATIVE' :
$upload_model = new Upload($param[ 'site_id' ]);
$qrcode_result = $upload_model->qrcode($result[ 'code_url' ]);
$return = [
"type" => "qrcode",
"qrcode" => $qrcode_result[ 'data' ] ?? ''
];
break;
case 'MWEB' ://H5支付
$return = [
"type" => "url",
"url" => $result[ 'mweb_url' ]
];
break;
case 'APP' :
$config = $this->app->jssdk->appConfig($result[ 'prepay_id' ]);
$return = [
"type" => "app",
"data" => $config
];
break;
}
return $this->success($return);
}
/**
* 异步回调
*/
public function payNotify()
{
$response = $this->app->handlePaidNotify(function($message, $fail) {
$pay_common = new PayCommon();
if ($message[ 'return_code' ] === 'SUCCESS') {
// return_code 表示通信状态,不代表支付状态
if ($message[ 'result_code' ] === 'SUCCESS') {
// 判断支付金额是否等于支付单据的金额
$pay_info = $pay_common->getPayInfo($message[ 'out_trade_no' ])[ 'data' ];
if (empty($pay_info)) return $fail('通信失败,请稍后再通知我');
if ($message[ 'total_fee' ] != round($pay_info[ 'pay_money' ] * 100)) return;
// 用户是否支付成功
$pay_common->onlinePay($message[ 'out_trade_no' ], "wechatpay", $message[ "transaction_id" ], "wechatpay");
}
} else {
return $fail('通信失败,请稍后再通知我');
}
return true;
});
$response->send();
return $response;
}
/**
* 关闭支付单据
* @param array $param
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function payClose(array $param)
{
$query_result = $this->app->order->queryByOutTradeNumber($param[ "out_trade_no" ]);
if($query_result['return_code'] == 'SUCCESS' && $query_result['result_code'] == 'SUCCESS' && $query_result['trade_state'] != 'CLOSED'){
if($query_result['trade_state'] == 'SUCCESS'){
return $this->error([ 'is_paid' => 1, 'pay_type' => 'wechatpay'], '微信已支付不可关闭');
}
$close_result = $this->app->order->close($param[ "out_trade_no" ]);
if($close_result['return_code'] != 'SUCCESS' || $close_result['result_code'] != 'SUCCESS'){
return $this->error($close_result, $close_result[ 'return_msg' ]);
}
}
return $this->success();
}
/**
* 申请退款
* @param array $param
*/
public function refund(array $param)
{
$pay_info = $param[ "pay_info" ];
$refund_no = $param[ "refund_no" ];
$total_fee = round($pay_info[ "pay_money" ] * 100);
$refund_fee = round($param[ "refund_fee" ] * 100);
$result = $this->app->refund->byOutTradeNumber($pay_info[ "out_trade_no" ], $refund_no, $total_fee, $refund_fee, []);
//调用失败
if ($result[ "return_code" ] == 'FAIL') return $this->error([], $result[ "return_msg" ]);
if ($result[ "result_code" ] == 'FAIL') return $this->error([], $result[ "err_code_des" ]);
return $this->success();
}
/**
* 转账
* @param array $param
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function transfer(array $param)
{
$data = [
'partner_trade_no' => $param[ 'out_trade_no' ], // 商户订单号,需保持唯一性(只能是字母或者数字,不能包含有符号)
'openid' => $param[ 'account_number' ],
'check_name' => 'FORCE_CHECK', // NO_CHECK不校验真实姓名, FORCE_CHECK强校验真实姓名
're_user_name' => $param[ 'real_name' ], // 如果 check_name 设置为FORCE_CHECK则必填用户真实姓名
'amount' => round($param[ 'amount' ] * 100, 2), // 转账金额
'desc' => $param[ 'desc' ]
];
$res = $this->app->transfer->toBalance($data);
if ($res[ 'return_code' ] == 'SUCCESS') {
if ($res[ 'result_code' ] == 'SUCCESS') {
return $this->success([
'out_trade_no' => $res[ 'partner_trade_no' ], // 商户交易号
'payment_no' => $res[ 'payment_no' ], // 微信付款单号
'payment_time' => $res[ 'payment_time' ] // 付款成功时间
]);
} else {
return $this->error([], $res[ 'err_code_des' ]);
}
} else {
return $this->error([], $res[ 'return_msg' ]);
}
}
/**
* 付款码支付
* @param array $param
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function micropay(array $param)
{
$data = [
'body' => str_sub($param[ "pay_body" ], 15),
'out_trade_no' => $param[ "out_trade_no" ],
'total_fee' => $param[ "pay_money" ] * 100,
'auth_code' => $param[ "auth_code" ]
];
$result = $this->app->base->pay($data);
if ($result[ 'return_code' ] == 'FAIL') {
return $this->error([], $result[ 'return_msg' ]);
}
if ($result[ 'result_code' ] == 'FAIL') {
if ($result[ 'err_code' ] != 'USERPAYING') {
( new Cron() )->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $param[ 'id' ]);
}
return $this->error($result, $result[ 'err_code_des' ]);
}
return $this->success($result);
}
/**
* 查询订单支付结果
* @param $pay_info
* @return array
* @throws \EasyWeChat\Kernel\Exceptions\InvalidArgumentException
* @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
*/
public function orderQuery($pay_info)
{
$result = $this->app->order->queryByOutTradeNumber($pay_info[ 'out_trade_no' ]);
Log::write('查询微信支付订单' . json_encode($result));
if ($result[ 'return_code' ] == 'FAIL') {
return $this->error([], $result[ 'return_msg' ]);
}
if ($result[ 'result_code' ] == 'SUCCESS') {
if ($result[ 'trade_state' ] == 'SUCCESS') {
( new PayCommon() )->onlinePay($pay_info[ 'out_trade_no' ], 'wechatpay', $result[ 'transaction_id' ], 'wechatpay');
} else if (in_array($result[ 'trade_state' ], [ 'NOTPAY', 'USERPAYING', 'ACCEPT' ])) {
$cron_model = new Cron();
$cron_model->deleteCron([ [ 'event', '=', 'PayOrderQuery' ], [ 'relate_id', '=', $pay_info[ 'id' ] ] ]);
$cron_model->addCron(1, 0, "查询付款码支付结果", "PayOrderQuery", time() + 3, $pay_info[ 'id' ]);
}else if($result[ 'trade_state' ] == 'PAYERROR'){
(new PayCommon())->edit(['pay_status' => PayCommon::PAY_STATUS_CANCEL], ['out_trade_no' => $pay_info[ "out_trade_no" ] ]);
}
}
return $this->success($result);
}
}

849
addon/wechatpay/model/V3.php Executable file
View File

@@ -0,0 +1,849 @@
<?php
/**
* Niushop商城系统 - 团队十年电商经验汇集巨献!
* =========================================================
* Copy right 2019-2029 杭州牛之云科技有限公司, 保留所有权利。
* ----------------------------------------------
* 官方网址: https://www.niushop.com
* =========================================================
*/
namespace addon\wechatpay\model;
use app\exception\ApiException;
use app\model\BaseModel;
use app\model\system\Cron;
use app\model\system\Pay as PayCommon;
use app\model\system\PayTransfer;
use app\model\upload\Upload;
use think\exception\HttpException;
use think\facade\Cache;
use think\facade\Log;
use WeChatPay\Builder;
use WeChatPay\ClientDecoratorInterface;
use WeChatPay\Crypto\AesGcm;
use WeChatPay\Crypto\Rsa;
use WeChatPay\Formatter;
use WeChatPay\Util\PemUtil;
use GuzzleHttp\Middleware;
use Psr\Http\Message\ResponseInterface;
use addon\wechatpay\model\Config as WechatPayConfig;
/**
* 微信支付v3支付
* 版本 1.0.4
*/
class V3 extends BaseModel
{
/**
* 应用实例
* @var \WeChatPay\BuilderChainable
*/
private $app;
// 平台证书实例
private $plateform_certificate_instance;
// 平台证书序列号
private $plateform_certificate_serial;
/**
* 微信支付配置
*/
private $config;
public function __construct($config)
{
$this->config = $config;
$merchant_certificate_instance = PemUtil::loadCertificate(realpath($config['apiclient_cert']));
// 证书序列号
$merchant_certificate_serial = PemUtil::parseCertificateSerialNo($merchant_certificate_instance);
// var_dump($this->config);
//平台证书和序列号处理
//plateform_cert 是系统自动生成的 plateform_certificate 是直接上传的
if (empty($this->config['plateform_certificate_serial'])) {
// 检测平台证书是否存在
if (empty($config['plateform_cert']) || !is_file($config['plateform_cert'])) {
$create_res = $this->certificates();
if ($create_res['code'] != 0) throw new ApiException(-1, $create_res['message'] ?? "微信支付配置错误");
// 保存平台证书
$this->config['plateform_cert'] = $create_res['data']['cert_path'];
(new Config())->setPayConfig($this->config, $this->config['site_id']);
}
// 加载平台证书
$this->plateform_certificate_instance = PemUtil::loadCertificate(realpath($this->config['plateform_cert']));
// 平台证书序列号
$this->plateform_certificate_serial = PemUtil::parseCertificateSerialNo($this->plateform_certificate_instance);
} else {
// 加载平台证书
$this->plateform_certificate_instance = file_get_contents(realpath($this->config['plateform_certificate']));
// 平台证书序列号
$this->plateform_certificate_serial = $this->config['plateform_certificate_serial'];
// 具体业务有很多需要用这个字段,值与新字段保持相同
$this->config[ 'plateform_cert' ] = $this->config[ 'plateform_certificate' ];
}
$this->app = Builder::factory([
// 商户号
'mchid' => $config['mch_id'],
// 商户证书序列号
'serial' => $merchant_certificate_serial,
// 商户API私钥
'privateKey' => PemUtil::loadPrivateKey(realpath($config['apiclient_key'])),
'certs' => [
$this->plateform_certificate_serial => $this->plateform_certificate_instance
]
]);
}
/**
* 生成平台证书
*/
private function certificates()
{
try {
$merchant_certificate_instance = PemUtil::loadCertificate(realpath($this->config['apiclient_cert']));
// 证书序列号
$merchant_certificate_serial = PemUtil::parseCertificateSerialNo($merchant_certificate_instance);
$certs = ['any' => null];
$app = Builder::factory([
// 商户号
'mchid' => $this->config['mch_id'],
// 商户证书序列号
'serial' => $merchant_certificate_serial,
// 商户API私钥
'privateKey' => PemUtil::loadPrivateKey(realpath($this->config['apiclient_key'])),
'certs' => &$certs
]);
$stack = $app->getDriver()->select(ClientDecoratorInterface::JSON_BASED)->getConfig('handler');
$stack->after('verifier', Middleware::mapResponse(self::certsInjector($this->config['v3_pay_signkey'], $certs)), 'injector');
$stack->before('verifier', Middleware::mapResponse(self::certsRecorder((string)dirname($this->config['apiclient_key']), $certs)), 'recorder');
$param = [
'url' => '/v3/certificates',
'timestamp' => (string)Formatter::timestamp(),
'noncestr' => uniqid()
];
$resp = $app->chain("v3/certificates")
->get([
'headers' => [
'Authorization' => Rsa::sign(
Formatter::joinedByLineFeed(...array_values($param)),
Rsa::from('file://' . realpath($this->config['apiclient_key']))
)
]
]);
$result = json_decode($resp->getBody()->getContents(), true);
$file_path = dirname($this->config['apiclient_key']) . '/plateform_cert.pem';
return $this->success(['cert_path' => $file_path]);
} catch (\Exception $e) {
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$result = json_decode($e->getResponse()->getBody()->getContents(), true);
return $this->error($result, $result['message']);
} else {
return $this->error([], $e->getMessage());
}
}
}
private static function certsInjector(string $apiv3Key, array &$certs): callable
{
return static function (ResponseInterface $response) use ($apiv3Key, &$certs): ResponseInterface {
$body = (string)$response->getBody();
/** @var object{data:array<object{encrypt_certificate:object{serial_no:string,nonce:string,associated_data:string}}>} $json */
$json = \json_decode($body);
$data = \is_object($json) && isset($json->data) && \is_array($json->data) ? $json->data : [];
\array_map(static function ($row) use ($apiv3Key, &$certs) {
$cert = $row->encrypt_certificate;
$certs[$row->serial_no] = AesGcm::decrypt($cert->ciphertext, $apiv3Key, $cert->nonce, $cert->associated_data);
}, $data);
return $response;
};
}
private static function certsRecorder(string $outputDir, array &$certs): callable
{
return static function (ResponseInterface $response) use ($outputDir, &$certs): ResponseInterface {
$body = (string)$response->getBody();
/** @var object{data:array<object{effective_time:string,expire_time:string:serial_no:string}>} $json */
$json = \json_decode($body);
$data = \is_object($json) && isset($json->data) && \is_array($json->data) ? $json->data : [];
\array_walk($data, static function ($row, $index, $certs) use ($outputDir) {
$serialNo = $row->serial_no;
$outpath = $outputDir . \DIRECTORY_SEPARATOR . 'plateform_cert.pem';
\file_put_contents($outpath, $certs[$serialNo]);
}, $certs);
return $response;
};
}
/**
* 支付
* @param array $param
* @return array
*/
public function pay(array $param)
{
$self = $this;
$site_id = $param['site_id'];
$data = [
'json' => [
'appid' => $this->config['appid'],
'mchid' => $this->config['mch_id'],
'description' => str_sub($param["pay_body"], 15),
'out_trade_no' => $param["out_trade_no"],
'notify_url' => $param["notify_url"],
'amount' => [
'total' => round($param["pay_money"] * 100)
]
]
];
switch ($param["trade_type"]) {
case 'JSAPI':
$data['json']['payer'] = ['openid' => $param['openid']];
$data['trade_type'] = 'jsapi';
$data['callback'] = function ($result) use ($self) {
return success(0, '', [
"type" => "jsapi",
"data" => $self->jsskdConfig($result['prepay_id'])
]);
};
break;
case 'APPLET':
$data['json']['payer'] = ['openid' => $param['openid']];
$data['trade_type'] = 'jsapi';
$data['callback'] = function ($result) use ($self) {
return success(0, '', [
"type" => "jsapi",
"data" => $self->jsskdConfig($result['prepay_id'])
]);
};
break;
case 'NATIVE':
$data['trade_type'] = 'native';
$data['callback'] = function ($result) use ($site_id) {
$upload_model = new Upload($site_id);
$qrcode_result = $upload_model->qrcode($result['code_url']);
return success(0, '', [
"type" => "qrcode",
"qrcode" => $qrcode_result['data'] ?? ''
]);
};
break;
case 'MWEB':
$data['trade_type'] = 'h5';
$data['json']['scene_info'] = [
'payer_client_ip' => request()->ip(),
'h5_info' => [
'type' => 'Wap'
]
];
$data['callback'] = function ($result) {
return success(0, '', [
"type" => "url",
"url" => $result['h5_url']
]);
};
break;
case 'APP':
$data['trade_type'] = 'app';
$data['callback'] = function ($result) use ($self) {
return success(0, '', [
"type" => "app",
"data" => $self->appConfig($result['prepay_id'])
]);
};
break;
}
$result = $this->unify($data);
if ($result['code'] != 0) return $result;
$result = $data['callback']($result['data']);
return $result;
}
/**
* 统一下单接口
* @param array $param
*/
public function unify(array $param)
{
try {
$resp = $this->app->chain('v3/pay/transactions/' . $param['trade_type'])->post([
'json' => $param['json'],
'headers' => [
'Wechatpay-Serial' => $this->plateform_certificate_serial
]
]);
$result = json_decode($resp->getBody()->getContents(), true);
return $this->success($result);
} catch (\Exception $e) {
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$result = json_decode($e->getResponse()->getBody()->getContents(), true);
$result = json_decode($e->getMessage(), true);
return $this->error($result, $result['message']);
} else {
return $this->error([], $e->getMessage());
}
}
}
/**
* 生成支付配置
* @param string $prepay_id
* @return array
*/
private function jsskdConfig(string $prepay_id)
{
$param = [
'appId' => $this->config['appid'],
'timeStamp' => (string)Formatter::timestamp(),
'nonceStr' => uniqid(),
'package' => "prepay_id=$prepay_id"
];
$param += ['paySign' => Rsa::sign(
Formatter::joinedByLineFeed(...array_values($param)),
Rsa::from('file://' . realpath($this->config['apiclient_key']))
), 'signType' => 'RSA'];
return $param;
}
/**
* 生成支付配置
* @param string $prepay_id
* @return array
*/
private function appConfig(string $prepay_id)
{
$param = [
'appid' => $this->config['appid'],
'timestamp' => (string)Formatter::timestamp(),
'noncestr' => uniqid(),
'prepayid' => $prepay_id
];
$param += [
'sign' => Rsa::sign(
Formatter::joinedByLineFeed(...array_values($param)),
Rsa::from('file://' . realpath($this->config['apiclient_key']))
),
'package' => 'Sign=WXPay',
'partnerid' => $this->config['mch_id']
];
return $param;
}
/**
* 异步回调
*/
public function payNotify()
{
$inWechatpaySignature = request()->header('Wechatpay-Signature'); // 从请求头中拿到 签名
$inWechatpayTimestamp = request()->header('Wechatpay-Timestamp'); // 从请求头中拿到 时间戳
$inWechatpaySerial = request()->header('Wechatpay-Serial'); // 从请求头中拿到 时间戳
$inWechatpayNonce = request()->header('Wechatpay-Nonce'); // 从请求头中拿到 时间戳
$inBody = file_get_contents('php://input');
$platformPublicKeyInstance = Rsa::from('file://' . realpath($this->config['plateform_cert']), Rsa::KEY_TYPE_PUBLIC);
$timeOffsetStatus = 300 >= abs(Formatter::timestamp() - (int)$inWechatpayTimestamp);
$verifiedStatus = Rsa::verify(
// 构造验签名串
Formatter::joinedByLineFeed($inWechatpayTimestamp, $inWechatpayNonce, file_get_contents('php://input')),
$inWechatpaySignature,
$platformPublicKeyInstance
);
if ($timeOffsetStatus && $verifiedStatus) {
// 转换通知的JSON文本消息为PHP Array数组
$inBodyArray = (array)json_decode($inBody, true);
// 使用PHP7的数据解构语法从Array中解构并赋值变量
['resource' => [
'ciphertext' => $ciphertext,
'nonce' => $nonce,
'associated_data' => $aad
]] = $inBodyArray;
// 加密文本消息解密
$inBodyResource = AesGcm::decrypt($ciphertext, $this->config['v3_pay_signkey'], $nonce, $aad);
// 把解密后的文本转换为PHP Array数组
$message = json_decode($inBodyResource, true);
Log::write('message' . $inBodyResource);
// 交易状态为成功
if (isset($message['trade_state']) && $message['trade_state'] == 'SUCCESS') {
if (isset($message['out_trade_no'])) {
$pay_common = new PayCommon();
$pay_info = $pay_common->getPayInfo($message['out_trade_no'])['data'];
if (empty($pay_info)) return;
if ($message['amount']['total'] != round($pay_info['pay_money'] * 100)) return;
// 用户是否支付成功
$pay_common->onlinePay($message['out_trade_no'], "wechatpay", $message["transaction_id"], "wechatpay");
header('', '', 200);
}
} else {
$this->payNotifyFail(501, 'FAIL', '没有trade_state字段或不是SUCCESS');
}
} else {
$this->payNotifyFail(401, 'FAIL', '验签失败');
}
}
/**
* 支付回调失败处理
* @param $response_code
* @param $code
* @param $message
*/
protected function payNotifyFail($response_code, $code, $message)
{
//记录日志
Log::write('V3支付回调失败');
Log::write(['response_code' => $response_code, 'code' => $code, 'message' => $message]);
// 设置HTTP响应头
header('Content-Type: application/json; charset=utf-8');
http_response_code($response_code); // 或400等适当的状态码
// 构造错误响应
$errorResponse = [
'code' => $code,
'message' => $message
];
// 输出JSON响应
echo json_encode($errorResponse, JSON_UNESCAPED_UNICODE);
exit;
}
/**
* 支付单据关闭
* @param array $param
* @return array
*/
public function payClose(array $param)
{
$get_res = $this->get($param['out_trade_no']);
if ($get_res['code'] >= 0 && $get_res['data']['trade_state'] != 'CLOSED') {
if ($get_res['data']['trade_state'] == 'SUCCESS') {
return $this->error(['is_paid' => 1, 'pay_type' => 'wechatpay'], '微信已支付不可关闭');
}
try {
$resp = $this->app->chain("v3/pay/transactions/out-trade-no/{$param['out_trade_no']}/close")->post([
'json' => [
'mchid' => $this->config['mch_id']
]
]);
$result = json_decode($resp->getBody()->getContents(), true);
return $this->success($result);
} catch (\Exception $e) {
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$result = json_decode($e->getResponse()->getBody()->getContents(), true);
return $this->error($result, $result['message']);
}
return $this->error(['file' => $e->getFile(), 'line' => $e->getLine(), 'message' => $e->getMessage()], $e->getMessage());
}
}
return $this->success();
}
/**
* 申请退款
* @param array $param
* @return array
*/
public function refund(array $param)
{
$pay_info = $param["pay_info"];
try {
$resp = $this->app->chain("v3/refund/domestic/refunds")->post([
'json' => [
'out_trade_no' => $pay_info['out_trade_no'],
'out_refund_no' => $param['refund_no'],
'notify_url' => addon_url("pay/pay/refundnotify"),
'amount' => [
'refund' => round($param['refund_fee'] * 100),
'total' => round($pay_info['pay_money'] * 100),
'currency' => $param['currency'] ?? 'CNY'
]
]
]);
$result = json_decode($resp->getBody()->getContents(), true);
if (isset($result['status']) && ($result['status'] == 'SUCCESS' || $result['status'] == 'PROCESSING'))
return $this->success($result);
else return $this->success($result, '退款异常');
} catch (\Exception $e) {
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$result = json_decode($e->getResponse()->getBody()->getContents(), true);
return $this->error($result, $result['message']);
} else {
return $this->error([], $e->getMessage());
}
}
}
/**
* 转账
* @param array $param
* @return array|mixed
*/
public function transfer(array $param)
{
if($this->config['transfer_v3_type'] == WechatPayConfig::TRANSFER_V3_TYPE_SHOP){
return $this->transferByShop($param);
}else{
return $this->transferByUser($param);
}
}
/**
* 商户直接转账
* @param array $param
* @return mixed
*/
public function transferByShop(array $param)
{
$data = [
'appid' => $this->config['appid'],
'out_batch_no' => $param['out_trade_no'],
'batch_name' => '客户提现转账',
'batch_remark' => '客户提现转账提现交易号' . $param['out_trade_no'],
'total_amount' => round($param['amount'] * 100),
'total_num' => 1,
'transfer_detail_list' => [
[
'out_detail_no' => $param['out_trade_no'],
'transfer_amount' => round($param['amount'] * 100, 2),
'transfer_remark' => $param['desc'],
'openid' => $param['account_number'],
'user_name' => $this->encryptor($param['real_name'])
]
]
];
\think\facade\Log::write('转账发起数据');
\think\facade\Log::write($data);
$this->app->chain('v3/transfer/batches')
->postAsync([
'json' => $data,
'headers' => [
'Wechatpay-Serial' => $this->plateform_certificate_serial
]
])->then(static function ($response) use (&$result) {
$pay_transfer_model = new PayTransfer();
$result = json_decode($response->getBody()->getContents(), true);
\think\facade\Log::write('转账成功返回');
\think\facade\Log::write($result);
$result = $pay_transfer_model->success([
'status' => $pay_transfer_model::STATUS_IN_PROCESS,
]);
})->otherwise(static function ($exception) use (&$result) {
if ($exception instanceof \GuzzleHttp\Exception\RequestException && $exception->hasResponse()) {
$result = json_decode($exception->getResponse()->getBody()->getContents(), true);
\think\facade\Log::write('转账其他返回');
\think\facade\Log::write($result);
$pay_transfer_model = new PayTransfer();
if (isset($result['batch_status'])) {
if (in_array($result['batch_status'], ['ACCEPTED', 'PROCESSING'])) {
$result = $pay_transfer_model->success([
'status' => $pay_transfer_model::STATUS_IN_PROCESS,
]);
} else if ($result['batch_status'] == 'FINISHED') {
$result = $pay_transfer_model->success([
'status' => $pay_transfer_model::STATUS_SUCCESS,
]);
} else {
$result = $pay_transfer_model->error($result, '转账失败');
}
} else {
$result = error(-1, '转账错误:' . $result['message'] ?? '未知错误', $result);
}
} else {
$result = error(-1, '转账报错:' . $exception->getMessage());
}
})->wait();
return $result;
}
/**
* 查询转账明细
* @param string $out_batch_no
* @param string $out_detail_no
* @return array
*/
public function transferDetail(string $out_batch_no, string $out_detail_no): array
{
try {
$resp = $this->app->chain("v3/transfer/batches/out-batch-no/{$out_batch_no}/details/out-detail-no/{$out_detail_no}")
->get();
$result = json_decode($resp->getBody()->getContents(), true);
\think\facade\Log::write('转账查询正常返回');
\think\facade\Log::write($result);
return $this->success($result);
} catch (\Exception $e) {
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$result = json_decode($e->getResponse()->getBody()->getContents(), true);
\think\facade\Log::write('转账查询其他返回');
\think\facade\Log::write($result);
if (isset($result['detail_status'])) {
return $this->success($result);
} else {
return $this->error($result, $result['message'] ?? '未知错误');
}
} else {
return $this->error([], $e->getMessage());
}
}
}
/**
* 加密数据
* @param string $str
* @return string
*/
public function encryptor(string $str)
{
$publicKey = $this->plateform_certificate_instance;
// 加密方法
$encryptor = function ($msg) use ($publicKey) {
return Rsa::encrypt($msg, $publicKey);
};
return $encryptor($str);
}
/**
* 获取转账结果
* @param $withdraw_info
* @return array
*/
public function getTransferResult($withdraw_info)
{
$pay_transfer_model = new PayTransfer();
$result = $this->transferDetail($withdraw_info['out_trade_no'], $withdraw_info['out_trade_no']);
if ($result['code'] == 0) {
switch ($result['data']['detail_status']) {
case 'SUCCESS':
$status = $pay_transfer_model::STATUS_SUCCESS;
break;
case 'FAIL':
$status = $pay_transfer_model::STATUS_FAIL;
$reason_config = [
'ACCOUNT_FROZEN' => '账户冻结',
'REAL_NAME_CHECK_FAIL' => '用户未实名',
'NAME_NOT_CORRECT' => '用户姓名校验失败',
'OPENID_INVALID' => 'Openid校验失败',
'TRANSFER_QUOTA_EXCEED' => '超过用户单笔收款额度',
'DAY_RECEIVED_QUOTA_EXCEED' => '超过用户单日收款额度',
'MONTH_RECEIVED_QUOTA_EXCEED' => '超过用户单月收款额度',
'DAY_RECEIVED_COUNT_EXCEED' => '超过用户单日收款次数',
'PRODUCT_AUTH_CHECK_FAIL' => '产品权限校验失败',
'OVERDUE_CLOSE' => '转账关闭',
'ID_CARD_NOT_CORRECT' => '用户身份证校验失败',
'ACCOUNT_NOT_EXIST' => '用户账户不存在',
'TRANSFER_RISK' => '转账存在风险',
'REALNAME_ACCOUNT_RECEIVED_QUOTA_EXCEED' => '用户账户收款受限,请引导用户在微信支付查看详情',
'RECEIVE_ACCOUNT_NOT_PERMMIT' => '未配置该用户为转账收款人',
'PAYER_ACCOUNT_ABNORMAL' => '商户账户付款受限,可前往商户平台-违约记录获取解除功能限制指引',
'PAYEE_ACCOUNT_ABNORMAL' => '用户账户收款异常,请引导用户完善其在微信支付的身份信息以继续收款',
'MERCHANT_REJECT' => '商家拒绝',
];
$reason_code = $result['data']['fail_reason'] ?? '';
$fail_reason = $reason_config[$reason_code] ?? $reason_code;
break;
default:
$status = $pay_transfer_model::STATUS_IN_PROCESS;
}
return $this->success([
'status' => $status,
'fail_reason' => $fail_reason ?? '',
'reason_code' => $reason_code ?? '',
]);
} else {
return $result;
}
}
/**
* 查询订单信息
* @param $out_trade_no
* @return array
*/
public function get($out_trade_no)
{
try {
$resp = $this->app->chain("/v3/pay/transactions/out-trade-no/{$out_trade_no}")->get();
$result = json_decode($resp->getBody()->getContents(), true);
return $this->success($result);
} catch (\Exception $e) {
if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
$result = json_decode($e->getResponse()->getBody()->getContents(), true);
return $this->error($result, $result['message']);
} else {
return $this->error([], $e->getMessage());
}
}
}
/**
* 会员转账下单
* @param array $param
*/
public function transferByUser(array $param)
{
$data = [
'appid' => $this->config['appid'],
'out_bill_no' => $param['out_trade_no'],
'transfer_scene_id' => $this->config[$param['from_type'].'_code'],
'openid' => $param['account_number'],
'user_name' => $param['amount'] >= 2000 ? $this->encryptor($param['real_name']) : '',
'transfer_amount' => round($param['amount'] * 100),
'transfer_remark' => '客户提现转账',
'notify_url' => addon_url("wechatpay://api/transfer/notify"),
'user_recv_perception'=> $this->config[$param['from_type'].'_recv'] ?? '',
'transfer_scene_report_infos' => $this->config[$param['from_type'].'_info'],
];
Log::write('会员发起转账数据');
Log::write($data);
try {
$resp = $this->app
->chain('/v3/fund-app/mch-transfer/transfer-bills')
->postAsync([
'json' => $data,
'headers' => [
'Wechatpay-Serial' => $this->plateform_certificate_serial
]
])->then(static function ($response) use (&$result) {
$result = json_decode($response->getBody()->getContents(), true);
\think\facade\Log::write('申请转账返回');
\think\facade\Log::write($result);
})->otherwise(static function ($exception) use (&$result) {
if ($exception instanceof \GuzzleHttp\Exception\RequestException && $exception->hasResponse()) {
$result = json_decode($exception->getResponse()->getBody()->getContents(), true);
\think\facade\Log::write('申请转账返回异常');
\think\facade\Log::write($result);
$result = error(-1, '转账异常:' .$result['message'] ?? '');
} else {
$result = error(-1, '转账异常:' . $exception->getMessage());
}
})->wait();
if(isset($result['code']) && $result['code'] != 0){
return $result;
}
//状态为待转账
$result['status'] = PayTransfer::STATUS_WAIT;
return $this->success($result);
} catch (\Exception $e) {
\think\facade\Log::write('会员提现转账失败,原因'.$e->getMessage());
return $this->error([], $e->getMessage());
}
}
/**
* 撤销转账
* @param $out_bill_no
*/
public function transferCancel($out_bill_no){
Log::write('会员撤销转账');
Log::write($out_bill_no);
try {
$resp = $this->app
->chain('/v3/fund-app/mch-transfer/transfer-bills/out-bill-no/'.$out_bill_no.'/cancel')
->postAsync()
->then(static function ($response) use (&$result) {
$result = json_decode($response->getBody()->getContents(), true);
Log::write('会员撤销转账数据返回');
Log::write($result);
})->otherwise(static function ($exception) use (&$result) {
if ($exception instanceof \GuzzleHttp\Exception\RequestException && $exception->hasResponse()) {
$result = json_decode($exception->getResponse()->getBody()->getContents(), true);
Log::write('撤销转账返回异常');
Log::write($result);
} else {
$result = error(-1, '撤销转账报错:' . $exception->getMessage());
}
})->wait();
return $this->success($result);
} catch (\Exception $e) {
Log::write('会员撤销转账失败,原因'.$e->getMessage());
return $this->error([], $e->getMessage());
}
}
/**
* 会员提现回调
* @throws \Exception
*/
public function transferNotify(){
$inWechatpaySignature = request()->header('Wechatpay-Signature'); // 从请求头中拿到 签名
$inWechatpayTimestamp = request()->header('Wechatpay-Timestamp'); // 从请求头中拿到 时间戳
$inWechatpaySerial = request()->header('Wechatpay-Serial'); // 从请求头中拿到 时间戳
$inWechatpayNonce = request()->header('Wechatpay-Nonce'); // 从请求头中拿到 时间戳
$inBody = file_get_contents('php://input');
Log::write('transferNotifyHeader'.json_encode(request()->header()));
Log::write('transferNotifyBody' . $inBody);
$platformPublicKeyInstance = Rsa::from('file://' . realpath($this->config['plateform_cert']), Rsa::KEY_TYPE_PUBLIC);
$timeOffsetStatus = 300 >= abs(Formatter::timestamp() - (int)$inWechatpayTimestamp);
$verifiedStatus = Rsa::verify(
// 构造验签名串
Formatter::joinedByLineFeed($inWechatpayTimestamp, $inWechatpayNonce, file_get_contents('php://input')),
$inWechatpaySignature,
$platformPublicKeyInstance
);
if ($timeOffsetStatus && $verifiedStatus) {
// 转换通知的JSON文本消息为PHP Array数组
$inBodyArray = (array)json_decode($inBody, true);
// 使用PHP7的数据解构语法从Array中解构并赋值变量
['resource' => [
'ciphertext' => $ciphertext,
'nonce' => $nonce,
'associated_data' => $aad
]] = $inBodyArray;
// 加密文本消息解密
$inBodyResource = AesGcm::decrypt($ciphertext, $this->config['v3_pay_signkey'], $nonce, $aad);
// 把解密后的文本转换为PHP Array数组
$message = json_decode($inBodyResource, true);
Log::write('transferNotifyMessage' . $inBodyResource);
// 交易状态为成功
$pay_transfer_model = new PayTransfer();
if (isset($message['state']) && $message['state'] == 'SUCCESS') {
if (isset($message['out_bill_no'])) {
$transfer_info = $pay_transfer_model->getTransferInfo([['out_trade_no', '=', $message['out_bill_no']]],'id')['data'];
$update_result = $pay_transfer_model->updateStatus(['status' => PayTransfer::STATUS_SUCCESS, 'result' => $message], $transfer_info['id']);
Log::write('transferNotify' . json_encode(['transfer_info'=>$transfer_info,'update_result'=>$update_result]));
header('', '', 200);
}
} else {
if (isset($message['out_bill_no'])) {
$transfer_info = $pay_transfer_model->getTransferInfo([['out_trade_no', '=', $message['out_bill_no']]],'id')['data'];
$update_result = $pay_transfer_model->updateStatus(['status' => PayTransfer::STATUS_FAIL, 'result' => $message], $transfer_info['id']);
Log::write('transferNotify' . json_encode(['transfer_info'=>$transfer_info,'update_result'=>$update_result]));
}
throw new HttpException(500, '失败', null, [], 'FAIL');
}
} else {
throw new HttpException(500, '失败', null, [], 'FAIL');
}
}
}