PHP实现微信小程序支付

news/2024/7/20 4:11:08 标签: 小程序, php, 微信支付, 后端, js

实际应用项目:http://github.crmeb.net/u/long

一、前端代码:

1.先写一个可以点击支付的按钮

<view class="chongzhi_button" bindtap="pay">支付</view>

2.开始js代码

pay: function(){
    //下订单并唤起微信支付
    var price = '0.01'; //支付金额
    var openid = ''; //这里传用户openid,怎么获取用户的openid这里就不详细的说了
    var that = this;
    wx.request({
      url: '这里写后端接口地址',
      data: {
        price: price,
		openid: openid
      },
      method: "POST",
      headers: {
        'Content-Type': 'application/json'
      },
      success: function (res) {
        //客户单发起微信请求
        wx.requestPayment({
            'appId': '这里写小程序的appId',
            'timeStamp': res.data.timeStamp,
            'nonceStr': res.data.nonceStr,
            'package': res.data.package,
            'signType': 'MD5',
            'paySign': res.data.paySign,
            'success': function (res) {
              wx.showToast({
                title: '支付成功',
                duration: 1200
              })
              console.log('支付成功');
              setTimeout(function () {
                wx.redirectTo({
                  url: '../pay/payok?paymoney=' + that.data.paymoney + '&body=' + that.data.contents
                })
              }, 1000);
              //这里完成跳转
            },
            'fail': function (res) {
              if (res.errMsg == "requestPayment:fail cancel") {
                wx.showToast({
                  title: '支付取消',
                  duration: 1200
                })
              } else {
                wx.showToast({
                  title: '支付失败',
                  image: '../../images/error.png',
                  duration: 1200
                })
              }
              //支付失败
              console.log(res);
            },
            'complete': function (res) { }
          })
      }
    })

二、然后开始后端代码(这里用PHP代码作为示例)

代码开始

php">/**
	 * 小程序支付
	 */
	public function Pay(){
	    $fee = 0.01;//举例充值0.01
	    $openid = '填写openid';
	    $appid = '小程序AddId';//这里写小程序AddId
	    $body = '标题'; //这个自己写,微信订单里面显示的是商品名称
	    $mch_id = '你的商户号';
	    $nonce_str = $this->nonce_str();//随机字符串
	    $notify_url = '填写你的回调地址';
	    $out_trade_no = $this->order_number($openid);//商户订单号
	    $spbill_create_ip = '服务器Ip地址';
	    $total_fee =    $fee*100;//因为充值金额最小是1 而且单位为分 如果是充值1元所以这里需要*100
	    $trade_type = 'JSAPI';//交易类型 默认
	    //这里是按照顺序的 因为下面的签名是按照顺序 排序错误 肯定出错
	    $post['appid'] = $appid;
	    $post['body'] = $body;
	    $post['mch_id'] = $mch_id;
	    $post['nonce_str'] = $nonce_str; //随机字符串
	    $post['notify_url'] = $notify_url;
	    $post['openid'] = $openid;
	    $post['out_trade_no'] = $out_trade_no;
	    $post['spbill_create_ip'] = $spbill_create_ip; //终端的ip
	    $post['total_fee'] = $total_fee; //总金额 最低为一块钱 必须是整数
	    $post['trade_type'] = $trade_type;
	    $sign = $this->sign($post); //签名
	    $post_xml = '<xml>
	           <appid>'.$appid.'</appid>
	           <body>'.$body.'</body>
	           <mch_id>'.$mch_id.'</mch_id>
	           <nonce_str>'.$nonce_str.'</nonce_str>
	           <notify_url>'.$notify_url.'</notify_url>
	           <openid>'.$openid.'</openid>
	           <out_trade_no>'.$out_trade_no.'</out_trade_no>
	           <spbill_create_ip>'.$spbill_create_ip.'</spbill_create_ip>
	           <total_fee>'.$total_fee.'</total_fee>
	           <trade_type>'.$trade_type.'</trade_type>
	           <sign>'.$sign.'</sign>
	        </xml> ';
	    //统一接口prepay_id
	    $url = 'https://api.mch.weixin.qq***/pay/unifiedorder';
	    $xml = $this->http_request($url,$post_xml);
	    $array = $this->xml($xml);//全要大写
	    if($array['RETURN_CODE'] == 'SUCCESS' && $array['RESULT_CODE'] == 'SUCCESS'){
	        $time = time();
	        $tmp='';//临时数组用于签名
	        $tmp['appId'] = $appid;
	        $tmp['nonceStr'] = $nonce_str;
	        $tmp['package'] = 'prepay_id='.$array['PREPAY_ID'];
	        $tmp['signType'] = 'MD5';
	        $tmp['timeStamp'] = "$time";
	        $data['state'] = 1;
	        $data['timeStamp'] = "$time";//时间戳
	        $data['nonceStr'] = $nonce_str;//随机字符串
	        $data['signType'] = 'MD5';//签名算法,暂支持 MD5
	        $data['package'] = 'prepay_id='.$array['PREPAY_ID'];//统一接口返回的 prepay_id 参数值,提交格式如:prepay_id=*
	        $data['paySign'] = $this->sign($tmp);//签名,具体签名方案参见微信公众号支付帮助文档;
	        $data['out_trade_no'] = $out_trade_no;

	    }else{
	        $data['state'] = 0;
	        $data['text'] = "错误";
	        $data['RETURN_CODE'] = $array['RETURN_CODE'];
	        $data['RETURN_MSG'] = $array['RETURN_MSG'];
	    }
	    echo json_encode($data);
	}

	//随机32位字符串
	private function nonce_str(){
	    $result = '';
	    $str = 'QWERTYUIOPASDFGHJKLZXVBNMqwertyuioplkjhgfdsamnbvcxz';
	    for ($i=0;$i<32;$i++){
	        $result .= $str[rand(0,48)];
	    }
	    return $result;
	}

	//生成订单号
	private function order_number($openid){
	    //date('Ymd',time()).time().rand(10,99);//18位
	    return md5($openid.time().rand(10,99));//32位
	}

	//签名 $data要先排好顺序
	private function sign($data){
	    $stringA = '';
	    foreach ($data as $key=>$value){
	        if(!$value) continue;
	        if($stringA) $stringA .= '&'.$key."=".$value;
	        else $stringA = $key."=".$value;
	    }
		$wx_key = '支付秘钥';
		$stringSignTemp = $stringA.'&key='.$wx_key;//申请支付后有给予一个商户账号和密码,登陆后自己设置key
		return strtoupper(md5($stringSignTemp));
	}

	//curl请求啊
	function http_request($url,$data = null,$headers=array()){
		$curl = curl_init();
		if(count($headers)>= 1){
			curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
		}
		curl_setopt($curl, CURLOPT_URL, $url);
		curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
		if(!empty($data)){
			curl_setopt($curl, CURLOPT_POST, 1);
			curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
		}
		curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
		$output = curl_exec($curl);
		curl_close($curl);
		return $output;
	}

	//获取xml
	private function xml($xml){
		$p = xml_parser_create();
		xml_parse_into_struct($p, $xml, $vals, $index);
		xml_parser_free($p);  $data = "";
		foreach ($index as $key=>$value){
			if($key == 'xml' || $key == 'XML') continue;
			$tag = $vals[$value[0]]['tag'];
			$value = $vals[$value[0]]['value'];
			$data[$tag] = $value;
		}
		return $data;
	}

 三、测试运行


http://www.niftyadmin.cn/n/908752.html

相关文章

1-5-43:质因数分解

总时间限制: 1000ms内存限制: 65536kB描述已知正整数 n 是两个不同的质数的乘积&#xff0c;试求出较大的那个质数。 输入输入只有一行&#xff0c;包含一个正整数 n。对于60%的数据&#xff0c;6 ≤ n ≤ 1000。对于100%的数据&#xff0c;6 ≤ n ≤ 2*10^9。输出输出只有一行…

微信小程序生成带参数的二维码以及小程序码

实际应用项目&#xff1a;http://github.crmeb.net/u/long 微信小程序生成带参数的二维码 官方共给了三个接口调用&#xff0c;大家可以根据自己的实际情况来使用&#xff0c;我这里使用的是接口B和接口C。官方文档地址 业务需求&#xff1a; 扫描二维码进入指定商品页面&…

安卓开发中使用Genymotion模拟器

在安卓开发中&#xff0c;运行和调试自己所写的安卓程序需要用到模拟器 在一般情况下 是直接在这创建一个模拟器&#xff0c;但是这种自带的模拟器运行效率不佳&#xff0c;而且启动时间漫长 所以&#xff0c;我们可以换一款安卓模拟器 Genymotion安卓模拟器。详情百度 官网地址…

PHP微信开发---小程序登录

实际应用项目&#xff1a;http://github.crmeb.net/u/long 微信小程序登录以及获取openid&#xff08;用户唯一标识&#xff09;和session_key 一、前端登录代码&#xff1a; //登录获取codelogin:function(){wx.login({success:function(res){console.log(res.code)//发送请…

网络获取的XML的Pull解析

<?xml version"1.0" encoding"utf-8" ?> - <students> - <student xuehao"1233"> <name>张三</name> <address>北京市昌平区</address> - <call> <phone>010-37464567</phone&g…

深入浅出了解CORS原理与实践

这里给大家推荐一款免费迭代 二开便捷的商城项目&#xff1a;源码直通车>>> 先从CQRS说起&#xff0c;CQRS的全称是Command Query Responsibility Segregation&#xff0c;翻译成中文叫作命令查询职责分离。从字面上就能看出&#xff0c;这个模式要求开发者按照方法的…

解决MySQL登录ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using passwor)问题

实际应用项目&#xff1a;http://github.crmeb.net/u/long 解决MySQL登录ERROR 1045 (28000): Access denied for user rootlocalhost (using passwor)问题 找到Mysql配置文件 这个操作&#xff0c;在不同平台下对应的不同的操作文件 mac系统中配置文件是mysql安装目录suppor…

5-Zend Studio配置

0-将文件编码设置成utf-8 Window>Preferences>General>Content Types>Text Default encoding:utf-8 1-Zend Studio常用快捷键 智能提示&#xff1a;Alt/ 复制当前行&#xff1a;CtrlAlt向下键 删除当前行&#xff1a;CtrlD 块注释&#xff1a;CtrlShift/ 块取消&am…