123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324 |
- <?php
- namespace weixin;
- /**
- * @title : 微信支付API接口插件
- * @desc :
- * @Author : Rock
- * @Date : 2023-04-06 19:06:59
- */
- class Wxpay
- {
- protected $WxPayConfig=array();
- public function __construct()
- {
- $this->WxPayConfig=array(
- 'APPID'=>sysconfig('wchat.wxmini_user_appid'),//微信小程序/公众号APPID
- 'MCHID'=>sysconfig('wchat.wxmini_user_mchid'),//微信商户ID
- 'KEY'=>sysconfig('wchat.wxmini_user_paysecret'),//微信商户支付秘钥
- 'SSLCERT_PATH'=>dirname(__FILE__).'/cert/apiclient_cert.pem',//支付证书位置
- 'SSLKEY_PATH'=>dirname(__FILE__).'/cert/apiclient_key.pem',//支付证书秘钥位置
- 'NOTIFY_URL'=> $_SERVER['REQUEST_SCHEME']."://".$_SERVER['HTTP_HOST'].sysconfig('wchat.notify_url'),//支付结果通知
- 'wapurl' => sysconfig('wchat.wapurl'),//支付成功跳转地址
- );
- }
-
- /**
- * @title: 微信H5支付统一下单
- * @desc: 描述
- * @param {string} {oid} {} {订单号}
- * @param {string} {amount} {} {支付金额}
- * @param {string} {body} {} {支付说明}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:14:03
- */
- public function H5Pay($oid,$amount,$body='微信H5支付')
- {
- $wapurl=$this->WxPayConfig['wapurl'];
- $notify_url=$this->WxPayConfig['NOTIFY_URL'];
- $scene_info='{"h5_info":{"type": "wap","wap_url": "'.$wapurl.'","wap_name": "'.$body.'"}}';
- $nonce_str=GetRandStr(32); //创建随机码
- $URL='https://api.mch.weixin.qq.com/pay/unifiedorder'; //接口地址
- $order['appid']=$this->WxPayConfig['APPID']; //应用ID
- $order['mch_id']=$this->WxPayConfig['MCHID']; //商户号
- $order['device_info']='WEB'; //设备号
- $order['nonce_str']=$nonce_str; //随机字符串
- $order['sign_type']='MD5'; //签名类型
- $order['body']=$body; //商品描述
- $order['out_trade_no']=$oid; //商户订单号
- $order['total_fee']=$amount*100; //总金额
- $order['spbill_create_ip']=GetIP(); //终端IP
- $order['time_start']=date("YmdHis"); //交易起始时间
- $order['time_expire']=date("YmdHis", time() + 600); //交易结束时间
- $order['notify_url']=$notify_url; //通知地址
- $order['trade_type']='MWEB'; //交易类型
- $order['scene_info']=$scene_info; //交易场景信息
- $Sign_Data=$order;
- $sign=Wxcommon::MakeSign_MD5($Sign_Data,$this->WxPayConfig['KEY']);//生成签名数据
- $order['sign']=$sign;
- $xml=Wxcommon::ToXml($order);
- $result=Wxcommon::postXmlCurl($xml,$URL); //POST方法发送XML请求
- $result=XmlToArray($result); //把返回结果转换成数组
- if(!isset($result['prepay_id'])){
- return $result['return_msg'];
- }
- $prepay_id=$result['prepay_id'];
- $trade_type=$result['MWEB'];
- WLog('WxPay_H5','请求:'.array2string($order));
- WLog('WxPay_H5','响应:'.array2string($result));
- if(isset($result['mweb_url'])){
- return $result['mweb_url'];
- }else{
- return $result['return_msg'];
- }
- }
- /**
- * @title: 微信扫码支付统一下单
- * @desc: 描述
- * @param {string} {oid} {} {订单号}
- * @param {string} {amount} {} {支付金额}
- * @param {string} {body} {} {支付备注}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:15:09
- */
- public function NativePay($oid,$amount,$body='微信扫码支付')
- {
- $notify_url=$this->WxPayConfig['NOTIFY_URL'];
- $nonce_str=GetRandStr(16); //创建随机码
- $URL='https://api.mch.weixin.qq.com/pay/unifiedorder'; //接口地址
- $order['appid']=$this->WxPayConfig['APPID']; //应用ID
- $order['mch_id']=$this->WxPayConfig['MCHID']; //商户号
- $order['device_info']='WEB'; //设备号
- $order['nonce_str']=$nonce_str; //随机字符串
- $order['sign_type']='MD5'; //签名类型
- $order['body']=$body; //商品描述
- $order['out_trade_no']=$oid; //商户订单号
- $order['total_fee']=$amount*100; //总金额
- $order['spbill_create_ip']=GetIP(); //终端IP
- $order['time_start']=(date("YmdHis")); //交易起始时间
- $order['time_expire']=(date("YmdHis", time() + 600)); //交易结束时间
- $order['notify_url']=$notify_url;
- $order['trade_type']='NATIVE'; //交易类型
- $Sign_Data=$order;
- $sign=Wxcommon::MakeSign_MD5($Sign_Data,$this->WxPayConfig['KEY']);//生成签名数据
- $order['sign']=$sign;
- $xml=Wxcommon::ToXml($order);
- $result=Wxcommon::postXmlCurl($xml,$URL);
- $result=XmlToArray($result);
-
- WLog('WxPay_Native','请求:'.$this->WxPayConfig['KEY'].array2string($order));
- WLog('WxPay_Native','响应:'.array2string($result));
-
- return $result;
- }
- /**
- * @title: 微信公众号/小程序支付统一下单
- * @desc: 描述
- * @param {string} {oid} {} {订单号}
- * @param {string} {amount} {} {支付金额}
- * @param {string} {openid} {} {用户OPENID}
- * @param {string} {body} {} {支付备注}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:16:34
- */
- public function JsApiPay($oid,$amount,$openid,$body='微信公众号支付')
- {
- $notify_url=$this->WxPayConfig['NOTIFY_URL'];
- $nonce_str=GetRandStr(32); //创建随机码
- $URL='https://api.mch.weixin.qq.com/pay/unifiedorder'; //接口地址
- $order['appid']=$this->WxPayConfig['APPID']; //应用ID
- $order['mch_id']=$this->WxPayConfig['MCHID']; //商户号
- $order['device_info']='WEB'; //设备号
- $order['nonce_str']=$nonce_str; //随机字符串
- $order['sign_type']='MD5'; //签名类型
- $order['body']=$body; //商品描述
- $order['out_trade_no']=$oid; //商户订单号
- $order['total_fee']=$amount*100; //总金额
- $order['spbill_create_ip']=GetIP(); //终端IP
- $order['time_start']=date("YmdHis"); //交易起始时间
- $order['time_expire']=date("YmdHis", time() + 600); //交易结束时间
- $order['notify_url']=$notify_url; //通知地址
- $order['trade_type']='JSAPI'; //交易类型
- $order['openid']=$openid;
- $Sign_Data=$order;
- $sign=Wxcommon::MakeSign_MD5($Sign_Data,$this->WxPayConfig['KEY']); //生成签名数据
- $order['sign']=$sign;
- $xml=Wxcommon::ToXml($order);
- $result=Wxcommon::postXmlCurl($xml,$URL);
- $result=XmlToArray($result);
- WLog('WxPay_JsApi','请求:'.array2string($order));
- WLog('WxPay_JsApi','响应:'.array2string($result));
- //开始公众号发起支付参数签名
- $nonce_str=GetRandStr(32);
- $arr['appId']=$this->WxPayConfig['APPID'];
- $arr['timeStamp']=''.time().'';
- $arr['nonceStr']=$nonce_str;
- if(!isset($result['prepay_id'])){
- return $result['return_msg'];
- }
- $arr['package']="prepay_id=".$result['prepay_id'];
- $arr['signType']="MD5";
- $sign=Wxcommon::MakeSign_MD5($arr,$this->WxPayConfig['KEY']);
- $arr['paySign']=$sign;
- WLog('WxPay_JsApi','JsApi发起参数:'.array2string($arr));
- return $arr;
- }
- /**
- * @title: 微信发红包
- * @desc: 描述
- * @param {string} {openid} {} {用户OPENID}
- * @param {string} {amount} {} {红包金额}
- * @param {string} {send_name} {} {商户名称}
- * @param {string} {wishing} {} {红包祝福语}
- * @param {string} {act_name} {} {活动名称}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:18:09
- */
- public function SendRedPack($openid,$amount,$send_name,$wishing="恭喜发财",$act_name='红包活动')
- {
- $nonce_str=GetRandStr(32); //创建随机码
- $mch_billno=time().mt_rand(1000000,9999999); //创建订单号
- $URL='https://api.mch.weixin.qq.com/mmpaymkttransfers/sendredpack'; //接口地址
- $order['wxappid']=$this->WxPayConfig['APPID']; //应用ID
- $order['mch_id']=$this->WxPayConfig['MCHID']; //商户号
- $order['nonce_str']=$nonce_str; //随机字符串
- $order['re_openid']=$openid; //微信用户OPENID
- $order['send_name']=$send_name; //商户名称
- $order['wishing']=$wishing; //红包祝福语
- $order['act_name']=$act_name; //活动名称
- $order['mch_billno']=$mch_billno; //订单号
- $order['total_amount']=$amount*100; //总金额
- $order['total_num']=1; //红包人数
- $order['client_ip']=GetIP(); //IP地址
- $Sign_Data=$order; //生成签名数据
- $sign=Wxcommon::MakeSign_MD5($Sign_Data,$this->WxPayConfig['KEY']); //签名
- $order['sign']=$sign;
- $xml=Wxcommon::ToXml($order); //把参数转换成XML
- $SSLCERT_PATH =$this->WxPayConfig['SSLCERT_PATH'];
- $SSLKEY_PATH =$this->WxPayConfig['SSLKEY_PATH'];
- $result=Wxcommon::postXmlCurl($xml,$URL,$SSLCERT_PATH,$SSLKEY_PATH); //POST方法发送XML请求
- $result=XmlToArray($result); //把返回结果转换成数组
- WLog('WxSendRedPack_Request_Api','请求:'.array2string($order)); //记录发红包请求、返回结果日志
- WLog('WxSendRedPack_Request_Api','响应:'.array2string($result));
- return $result;
- }
- /**
- * @title: 微信支付退款
- * @desc: 描述
- * @param {string} {transaction_id} {} {支付流水号}
- * @param {float} {amount} {} {退款金额}
- * @param {float} {total} {} {订单总金额}
- * @param {string} {body} {} {退款备注}
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:20:13
- */
- public function Refund($transaction_id,$amount,$total,$body='商品已售完')
- {
- $nonce_str=GetRandStr(32); //创建随机码
- $out_refund_no=time().mt_rand(1000000,9999999); //生成退款订单号
- $URL='https://api.mch.weixin.qq.com/secapi/pay/refund'; //接口地址
- $order['appid']=$this->WxPayConfig['APPID']; //应用ID
- $order['mch_id']=$this->WxPayConfig['MCHID']; //商户号
- $order['nonce_str']=$nonce_str; //随机字符串
- $order['sign_type']='MD5'; //签名类型
- $order['refund_desc']=$body; //退款原因
- $order['transaction_id']=$transaction_id; //退款交易号
- $order['out_refund_no']=$out_refund_no; //商户退款单号
- $order['total_fee']=$total*100; //总金额
- $order['refund_fee']=$amount*100; //退款金额
- $Sign_Data=$order;
- $sign=Wxcommon::MakeSign_MD5($Sign_Data,$this->WxPayConfig['KEY']); //生成签名数据
- $order['sign']=$sign;
- $xml=Wxcommon::ToXml($order);
- $SSLCERT_PATH =$this->WxPayConfig['SSLCERT_PATH'];
- $SSLKEY_PATH =$this->WxPayConfig['SSLKEY_PATH'];
- $result=Wxcommon::postXmlCurl($xml,$URL,$SSLCERT_PATH,$SSLKEY_PATH);
- $result=XmlToArray($result);
- WLog('WxRefund_Api','请求:'.array2string($order));
- WLog('WxRefund_Api','响应:'.array2string($result));
- return $result;
- }
- /**
- * @title: 微信打款接口
- * @desc: 描述
- * @param {*} $partner_trade_no
- * @param {*} $openid
- * @param {*} $amount
- * @param {*} $body
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:21:32
- */
- public function Rransfers($partner_trade_no,$openid,$amount,$body='佣金打款')
- {
- $nonce_str=GetRandStr(32); //创建随机码
- $URL='https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers'; //接口地址
- $order['mch_appid']=$this->WxPayConfig['APPID']; //应用ID
- $order['mchid']=$this->WxPayConfig['MCHID']; //商户号
- $order['nonce_str']=$nonce_str; //随机字符串
- $order['openid']=$openid; //用户openid
- $order['check_name']='NO_CHECK'; //校验用户姓名选项
- $order['desc']=$body; //打款原因
- $order['partner_trade_no']=$partner_trade_no; //商户单号
- $order['amount']=$amount*100; //总金额
- $order['spbill_create_ip']=GetIP(); //Ip地址
- $Sign_Data=$order;
- $sign=Wxcommon::MakeSign_MD5($Sign_Data,$this->WxPayConfig['KEY']); //生成签名数据
- $order['sign']=$sign;
- $xml=Wxcommon::ToXml($order);
- $SSLCERT_PATH =$this->WxPayConfig['SSLCERT_PATH'];
- $SSLKEY_PATH =$this->WxPayConfig['SSLKEY_PATH'];
- $result=Wxcommon::postXmlCurl($xml,$URL,$SSLCERT_PATH,$SSLKEY_PATH);
- $result=XmlToArray($result);
- WLog('WxRransfers_Api','请求:'.array2string($order));
- WLog('WxRransfers_Api','响应:'.array2string($result));
- return $result;
- }
- /**
- * @title: 支付回调参数合法性校验
- * @desc: 描述
- * @param {*} $order
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:21:52
- */
- public function Callback_SignCheck($order)
- {
- $sign=$order['sign'];
- unset($order['sign']);
- $sign2=Wxcommon::MakeSign_MD5($order,$this->WxPayConfig['KEY']);
- if($sign==$sign2){
- return true;
- }else{
- return false;
- }
- }
- /**
- * @title: 组装回调数据
- * @desc: 描述
- * @param {*} $status
- * @param {*} $msg
- * @return {*}
- * @author: Rock
- * @method: POST
- * @Date: 2023-04-06 20:22:10
- */
- public function Return_Result($status,$msg)
- {
- $data['return_code']=$status;
- $data['return_msg']=$msg;
- return Wxcommon::ToXml($data);
- }
- }
- ?>
|