<?php
namespace addon\wechatpay\model;
use app\model\system\Config as ConfigSystemModel;
use app\model\BaseModel;
use think\facade\Log;
class WxPay extends BaseModel
{
//appId
// private $wxMerchantId = '1624955035';//商户号
// private $wxMerchantApiCertificate = './apiclient_cert.pem'; //商户证书
// private $merchantPrivateKeyFilePath = './apiclient_key.pem'; //商户私钥
// private $wxApiSerialNo = '3E45E14CCEC5E8F6B7AD0892C7226B40243C85F9'; //商户API证书序列号mch_key_v3
// private $wxApiV3Key = 'XDTFJZToqHKJP8Nfyjezu16BOchTGxbt';
// private $appid = 'wx88d0fb048346d2ff';
private $appId = '';
//appSecret
private $appSecret = '';
//商户号
private $mchId = '';
//商户密钥
private $mchKey = '';
//本地路径
private $path;
//证书文件
private $sslCert = ''; //商户证书
private $sslKey = '.';
private $platCert = 'plat_cert.json';
//api地址
private $rootUrl = 'https://api.mch.weixin.qq.com';
//api证书序列号
private $serialNo = '';
//商户私钥
private $privateKey = '';
//v3密钥
private $platKey = '';
//平台证书序列号
private $platSerialNo;
//平台密钥
private $platPublicKey ;
//加密方式
private $signAlg = 'sha256WithRSAEncryption';
private $schema = 'WECHATPAY2-SHA256-RSA2048';
//主机头
private $httpHeader;
//请求类型
private $method = 'GET';
//api接口
private $panel = '';
//随机字符串
public $nonce = '';
//时间戳
public $stamp = 0;
//报文主体
public $body = '';
//签名
public $signature = '';
/**
* 构造函数
*/
public function __construct()
{
$this->appId = isset($config['appId']) ? $config['appId'] : $this->appId;
$this->appSecret = isset($config['appSecret']) ? $config['appSecret'] : $this->appSecret;
$this->mchId = isset($config['mchId']) ? $config['mchId'] : $this->mchId;
$this->mchKey = isset($config['mchKey']) ? $config['mchKey'] : $this->mchKey;
// $this->path = str_replace('\\', '/', ROOT_PATH);
$this->sslCert = $this->sslCert;
$this->sslKey = $this->sslKey;
$this->platCert = $this->platCert;
$folder = dirname($this->platCert);
if (!is_dir($folder)) {
mkdir($folder, 0755, true);
}
// $this->serialNo = isset($config['serialNo']) ? $config['serialNo'] : '';
// $this->platKey = isset($config['platKey']) ? $config['platKey'] : '';
$this->getPrivateKey();
$this->getCertificates();
}
/**
* 预支付
*/
public function prepay(array $data)
{
$data['appid'] = $this->appId;
$data['mchid'] = $this->mchId;
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$this->method = 'POST';
$this->panel = '/v3/pay/transactions/jsapi';
$this->body = $data;
$this->buildHeader();
$url = $this->rootUrl . $this->panel;
$result = json_decode($this->post($url, $data), true);
return $result;
}
/**
* JSAPI支付
*/
public function invoke($preid)
{
$this->body = 'prepay_id=' . $preid;
$this->buildSignature();
$data = array(
'timeStamp' => (string)$this->stamp,
'nonceStr' => $this->nonce,
'package' => $this->body,
'signType' => 'RSA',
'paySign' => $this->signature,
);
return $data;
}
/**
* 原生支付
*/
public function native(array $data)
{
$data['sp_appid'] = $this->appId;
$data['sp_mchid'] = $this->mchId;
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$this->method = 'POST';
$this->panel = '/v3/pay/partner/transactions/native';
$this->body = $data;
$this->buildHeader();
$url = $this->rootUrl . $this->panel;
$result = json_decode($this->post($url, $data), true);
return $result;
}
/**
* 合单
*/
public function combine(array $data)
{
$data = json_encode($data, JSON_UNESCAPED_UNICODE);
$this->method = 'POST';
// $this->panel = '/v3/combine-transactions/native';
$this->panel = '/v3/combine-transactions/jsapi';
$this->body = $data;
$this->buildHeader();
$url = $this->rootUrl . $this->panel;
$result = json_decode($this->post($url, $data), true);
return $result;
}
public function pays(array $data){
// file_put_contents('dadada.txt',json_encode($data));
$list['combine_appid'] =$this->appId;
$list['combine_mchid'] =$this->mchId;
$list['combine_out_trade_no'] = $data['out_trade_no'];
$order = model('order')->getList([['out_trade_no','=',$data['out_trade_no']]],'order_id,member_id,site_id,balance_money');
// $ordesr[] = [
// 'mchid'=>$list['combine_mchid'],//子单发起方商户号
// 'attach'=>$o['site_name'],
// 'amount'=>['total_amount'=>(intval(($o['real_goods_money'] - $o['balance_money'] ) * 100) ),'currency'=>'CNY'],
// 'sub_mchid'=>$sub_mchidd['sub_mchid'],
// 'description'=>'小朋友商城-'.$o['goods_name'],
// 'out_trade_no'=>$o['order_no'].$o['site_id'],
// 'settle_info'=>['profit_sharing'=>true],
// ];
$num = 0;
foreach($order as $o){
$sub_mchidd = model('shop')->getInfo([['site_id','=',$o['site_id']]],'sub_mchid');
$order_goods = model('order_goods')->getList([['order_id','=',$o['order_id']]],'real_goods_money,order_no,price,site_name,sku_name,site_id,vip_discounts,goods_name,sku_id');
// dump($order_goods);die;
$count = count($order_goods);
// dump($order_goods);die;
foreach ($order_goods as $key => $value) {
$sub_mchid = model('shop')->getInfo([['site_id','=',$o['site_id']]],'sub_mchid');
if($o['balance_money'] > 0){
if($key == ($count - 1)){
$number = $o['balance_money'] - $num;
}else{
$number = floor($o['balance_money'] / $count * 100) / 100;
$num += $number;
}
}else{
$number = 0;
}
$ordesr[] = [
'mchid'=>$list['combine_mchid'],//子单发起方商户号
'attach'=>$value['site_name'],
'amount'=>['total_amount'=>(intval(($value['real_goods_money'] - $number ) * 100) ),'currency'=>'CNY'],
'sub_mchid'=>$sub_mchid['sub_mchid'],
'description'=>'小朋友商城-'.$value['goods_name'],
'out_trade_no'=>$value['order_no'].$value['sku_id'],
'settle_info'=>['profit_sharing'=>true],
];
}
}
//dump($list);die;
$list['sub_orders'] = $ordesr;
$info = model('member')->getInfo([['member_id','=',$order[0]['member_id']]],'weapp_openid');
// $list['sub_orders']['mchid'] ='';
// $list['sub_orders']['attach'] ='';
// $list['sub_orders']['amount']['total_amount'] ='';//子单金额,单位为分;
// $list['sub_orders']['amount']['currency'] ='CNY';//;
// $list['sub_orders']['sub_mchid'] ='';//子单订单号;sub_mchid
// $list['sub_orders']['description'] ='';//子单订单号;sub_mchid
$list['combine_payer_info']['openid'] =$info['weapp_openid'];
$list['notify_url'] ="https://mall.xiaogulikeji.com/pay/pay/notifyT.html";
$list['scene_info']['payer_client_ip'
thinkphp 6对接微信服务商demo
需积分: 5 47 浏览量
2024-01-18
13:19:48
上传
评论
收藏 9KB ZIP 举报
水浅不摸鱼
- 粉丝: 1
- 资源: 13
最新资源
- rdf0412-kcu116-pcie-c-2019-1.zip(XILINX KCU116 源码)
- 基于C#语言的winform界面火车票订票系统(源码+实验报告)
- 【华为OD部分真题及讲解】华为OD部分真题及讲解
- 基于Python+Django的京东商品比价系统源码+全部资料(毕业设计).zip
- G460 G560 Z460 Z560的最新BIOS 2.18版(无白名单)
- MetaJUI v0.4
- 基于Python+Django的京东商品比价系统源码+全部资料(毕业设计).zip
- linux常用命令大全
- 立体相机标定-使用OpenCV+Cpp对立体相机进行标定-calibration-附项目源码+流程教程-优质项目实战.zip
- 基于SpringBoot+Mysql开发的酒店管理系统
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈