<?php
declare (strict_types = 1);
namespace app\index\controller;
use think\Request;
class WxPay
{
/**
* wxPay
*/
public function wxPays(Request $request)
{
$ss = $this->weixinapp();
return json(['code'=>200,'data'=>$ss]);
}
//统一下单接口
private function unifiedorder() {
$url = 'https://api.mch.weixin.qq.com/pay/unifiedorder';
$parameters = array(
'appid' => '', //小程序ID
'mch_id' => '', //商户号
'nonce_str' => $this->createNoncestr(), //随机字符串
'body' => '商品支付', //商品描述
'out_trade_no' => '2015450806125348', //商户订单号
'total_fee' => floatval(10 * 100), //总金额 单位 分
'spbill_create_ip' => $_SERVER['REMOTE_ADDR'], //终端IP
'notify_url' => 'http://www.weixin.qq.com/wxpay/pay.php', //通知地址 确保外网能正常访问
'openid' => "okiww5XIrgqVNHJBEahMQN9-PYjk", //用户openid
'trade_type' => 'JSAPI'//交易类型
);
//统一下单签名
$parameters['sign'] = $this->getSign($parameters);
$xmlData = $this->arrayToXml($parameters);
$return = $this->xmlToArray($this->postXmlCurl($xmlData, $url, 60));
// print_r($return);die;
return $return;
}
/**
* 配置信息
* */
public function wxClass(){
$data['sub_appid'] = "";//商户appid
$data['sub_mch_id'] = "";//商户(商户号)
$data['sub_api'] = "";//商户密匙
$data['sub_cert'] = "/wxClass/wxPay/sub/apiclient_cert.pem";//商证书cert
$data['sub_key'] = "/wxClass/wxPay/sub/apiclient_key.pem";//商证书key
return $data;
}
//签名(调起数据签名)
private function weixinapp() {
$wxClass = $this->wxClass();
//统一下单接口
$unifiedorder = $this->unifiedorder();
$parameters = array(
'appId' => $wxClass['sub_appid'], //小程序ID
'timeStamp' => '' . time() . '', //时间戳
'nonceStr' => $this->createNoncestr(), //随机串
'package' => 'prepay_id=' . $unifiedorder['prepay_id'], //数据包
'signType' => 'MD5'//签名方式
);
//签名
$parameters['paySign'] = $this->getSign($parameters);
return $parameters;
}
//产生随机字符串,不长于32位
private function createNoncestr($length = 32) {
$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
$str = "";
for ($i = 0; $i < $length; $i++) {
$str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
}
return $str;
}
//生成签名
private function getSign($Obj) {
foreach ($Obj as $k => $v) {
$Parameters[$k] = $v;
}
//签名步骤一:按字典序排序参数
ksort($Parameters);
$String = $this->formatBizQueryParaMap($Parameters, false);
//签名步骤二:在string后加入KEY
// $String = $String . "&key=" . $this->key;
$String = $String . "&key=xudadexudadexudadexudadexudade13";
//签名步骤三:MD5加密
$String = md5($String);
//签名步骤四:所有字符转为大写
$result_ = strtoupper($String);
return $result_;
}
//格式化参数,签名过程需要使用
private function formatBizQueryParaMap($paraMap, $urlencode) {
$buff = "";
ksort($paraMap);
foreach ($paraMap as $k => $v) {
if ($urlencode) {
$v = urlencode($v);
}
$buff .= $k . "=" . $v . "&";
}
$reqPar = '';
if (strlen($buff) > 0) {
$reqPar = substr($buff, 0, strlen($buff) - 1);
}
return $reqPar;
}
//数组转换成xml
private function arrayToXml($arr) {
$xml = "<root>";
foreach ($arr as $key => $val) {
if (is_array($val)) {
$xml .= "<" . $key . ">" . arrayToXml($val) . "</" . $key . ">";
} else {
$xml .= "<" . $key . ">" . $val . "</" . $key . ">";
}
}
$xml .= "</root>";
return $xml;
}
//xml转换成数组
private function xmlToArray($xml) {
//禁止引用外部xml实体
libxml_disable_entity_loader(true);
$xmlstring = simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA);
$val = json_decode(json_encode($xmlstring), true);
return $val;
}
private static function postXmlCurl($xml, $url, $second = 30)
{
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); //严格校验
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_TIMEOUT, 40);
set_time_limit(0);
//运行curl
$data = curl_exec($ch);
//返回结果
if ($data) {
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
throw new WxPayException("curl出错,错误码:$error");
}
}
}
根据返回参数,小程序调取支付
var url = app.globalData.apis+'/index/wxPays';
http.post(url,{},req=>{
var data = req.data.data;
console.log(data);
wx.requestPayment({
timeStamp: data.timeStamp,
nonceStr: data.nonceStr,
package: data.package,
signType: 'MD5',
paySign: data.paySign,
success (res) {
console.log(res)
},
fail (res) { }
})
});
退款
/**
* 配置信息
* */
public function wxClass(){
$system = Db::query("select * from ims_zh_jdgjb_system where id = 2");
$data['sub_appid'] = $system[0]['appid'];//商户appid(小程序)
if($this->type == 1){
$data['sub_mch_id'] = $system[0]['mchid'];//商户(商户号)
$data['sub_api'] = $system[0]['wxkey'];//商户密匙
$apiclient_cert = $system[0]['apiclient_cert'];
$apiclient_key = $system[0]['apiclient_key'];
}else{
$seller_id = Db::query("select * from ims_zh_jdgjb_system_gl where seller_id='{$this->seller_id}' and type=2");
$data['sub_mch_id'] = $seller_id[0]['mchid'];//商户(商户号)
$data['sub_api'] = $seller_id[0]['wxkey'];//商户密匙
$apiclient_cert = $seller_id[0]['apiclient_cert'];
$apiclient_key = $seller_id[0]['apiclient_key'];
}
//证书写退款在写
$fp = fopen("wxClass/wxPay/sub/apiclient_cert.pem", 'w+');
$str =$apiclient_cert;
fwrite($fp,$str);
fclose($fp);
$fp = fopen("wxClass/wxPay/sub/apiclient_key.pem", 'w+');
$str =$apiclient_key;
fwrite($fp,$str);
fclose($fp);
$data['sub_cert'] = "wxClass/wxPay/sub/apiclient_cert.pem";//商证书cert
$data['sub_key'] = "wxClass/wxPay/sub/apiclient_key.pem";//商证书key
return $data;
}
/**
* 退款
* */
public function out(Request $request){
$id = $request->post("id");
$token = $request->post("token");
if($token != "shahjkdhasjkdjkasjk55621545sdasdasdsa65as4dGSHDDAH"){
return json(['code'=>201,'data'=>'','message'=>'不合法']);
}
$order = Db::query("select * from ims_zh_jdgjb_order where id='{$id}'");
$seller_id = Db::query("select * from ims_zh_jdgjb_system_gl where seller_id='{$order[0]['seller_id']}' and type=2");
if(!empty($seller_id[0]['id'])){
$this->type = 2;
$this->seller_id = $order[0]['seller_id'];
}else{
$this->type = 1;
}
//$this->appid = $order[0]['appid'];//appid
$wxClass = $this->wxClass();
$url = 'https://api.mch.weixin.qq.com/secapi/pay/refund';
$out_refund_no = 'wxtk'.time();//退款单到
$total_fee = $order[0]['price'];
$parameters = array(
'appid' =>"{$wxClass['sub_appid']}", //应用ID,固定
'mch_id' => "{$wxClass['sub_mch_id']}", //商户号,固定
'nonce_str' => $this->createNoncestr(), //随机字符串
'out_refund_no' => $out_refund_no, //商户内部唯一退款单号
'out_trade_no' => $order[0]['out_trade_no'], //商户订单号,pay_sn码 1.1二选一,微信生成的订单号,在支付通知中有返回
// 'transaction_id'=>'1',//微信订单号 1.2二选一,商户侧传给微信的订单号
'refund_fee' => floatval($total_fee*100), //退款金额
'total_fee' => floatval($total_fee*100), //总金额
);
$parameters['sign'] = $this->getSign($parameters);
$xmlData = $this->arrayToXml($parameters);
$path_cert = 'wxClass/wxPay/sub/apiclient_cert.pem';
$path_key = 'wxClass/wxPay/sub/apiclient_key.pem';
$useCert = true;
$return = $this->xmlToArray($this->postXmlCurls($xmlData, $url,$useCert, 60,$path_cert,$path_key));
if($return['result_code'] == "SUCCESS" && $return['return_code'] == "SUCCESS" && $return['return_msg'] == "OK"){
$data['status'] = 7;
Order::where('id',$id)->update($data);
return json(['code'=>200,'data'=>$return,'message'=>'退款成功']);
}else{
if($return['result_code'] == "FAIL" && $return['return_code'] == "SUCCESS" && $return['return_msg'] == "OK"){
$data['status'] = 7;
Order::where('id',$id)->update($data);
return json(['code'=>200,'data'=>$return,'message'=>'已退款']);
}else{
return json(['code'=>201,'data'=>$return,'message'=>'退款失败']);
}
}
return json(['code'=>200,'data'=>$return,'message'=>'退款成功']);
}
private static function postXmlCurls($xml, $url, $useCert, $second = 30,$path_cert,$path_key)
{
$ch = curl_init();
//设置超时
curl_setopt($ch, CURLOPT_TIMEOUT, $second);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,FALSE);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,FALSE);//严格校验
//设置header
curl_setopt($ch, CURLOPT_HEADER, FALSE);
//要求结果为字符串且输出到屏幕上
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if($useCert == true){
//设置证书
//使用证书:cert 与 key 分别属于两个.pem文件
curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLCERT, $path_cert);
curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
curl_setopt($ch,CURLOPT_SSLKEY, $path_key);
}
//post提交方式
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml);
//运行curl
$data = curl_exec($ch);
//返回结果
if($data){
curl_close($ch);
return $data;
} else {
$error = curl_errno($ch);
curl_close($ch);
return ['code'=>201,'error'=>$error];
//message("退款失败,CURL出错,错误码:".$error);exit;
}
}