E企盈小程序营销工具技术服务商 热线:4006-838-530

PHP小程序支付

E企盈小程序直播系统

PHP 小程序支付根据code使用PHP 调起小程序支付(基于TP 5.0)?php  public function xcxpay()  {      $appid = “************”;      $mch_id = “*************”;      $code = input(“post.code”);      $price = 1;      // 根据code获取openid      $url = “https://api.weixin.qq.com/sns/jscode2session?appid=” . $appid . “&secret=” . $mch_id . “&js_code=” . $code . “&grant_type=authorization_code”;      $infos = json_decode(file_get_contents($url));      $openid = $infos[‘openid’];      // 调支付      $url = “https://api.mch.weixin.qq.com/pay/unifiedorder”;      $data = array();      $data[‘appid’] = $appid;      $data[‘mch_id’] = $mch_id;      $data[‘nonce_str’] = $this-getRandCode(32);      $data[‘body’] = “商城消费”;      $data[‘out_trade_no’] = uniqid();      $data[‘total_fee’] = $price * 100;      $data[‘spbill_create_ip’] = $_SERVER[‘REMOTE_ADDR’];      $data[‘notify_url’] = “回调地址”;      $data[‘trade_type’] = “JSAPI”;      $data[‘openid’] = $openid;      // 生成sign      ksort($data);      $string = ”;      foreach ($data as $key = $v) {        if (empty($v)) {          continue;        }        $string .= “{$key}={$v}&”;      }      $string .= “key=” . config(“wx.key”);      $sign = strtoupper(md5($string));      $data[‘sign’] = $sign;      // 转xml      $xml = “xml”;      foreach ($data as $key = $val) {        $xml .= “” . $key . “” . $val . “/” . $key . “”;      }      $xml .= “/xml”;      // 发送请求      $res = $this-payCurl($url, ‘post’, ‘xml’, $xml);      if ($res[‘result_code’] == “FAIL”) {        return returnJson(0, “网络错误”, $res);      }      $result = array();      $result[‘appId’] = $res[‘appid’];      $result[‘timeStamp’] = (string)time();      $result[‘nonceStr’] = $res[‘nonce_str’];      $result[‘package’] = “prepay_id=” . $res[‘prepay_id’];      $result[‘signType’] = “MD5”;      ksort($result);      $str = “”;      foreach ($result as $key = $v) {        $str .= “{$key}={$v}&”;      }      $str .= “key=” . config(“wx.key”);      $result[‘paySign’] = strtoupper(md5($str));      return returnJson(1, “返回数据”, $result);  }    // 取随机字符串  public function getRandCode($num = 16)  {     $array = array(‘A’, ‘B’, ‘C’, ‘D’, ‘E’, ‘F’, ‘G’, ‘H’, ‘I’, ‘J’, ‘K’, ‘L’, ‘M’, ‘N’, ‘O’, ‘P’, ‘Q’, ‘R’, ‘S’, ‘T’, ‘U’, ‘V’, ‘W’, ‘X’, ‘Y’, ‘Z’, ‘a’, ‘b’, ‘c’, ‘d’, ‘e’, ‘f’, ‘g’, ‘h’, ‘i’, ‘j’, ‘k’, ‘l’, ‘m’, ‘n’, ‘o’, ‘p’, ‘q’, ‘r’, ‘s’, ‘t’, ‘u’, ‘v’, ‘w’, ‘x’, ‘y’, ‘z’, ‘0’, ‘1’, ‘2’, ‘3’, ‘4’, ‘5’, ‘6’, ‘7’, ‘8’, ‘9’);      $tmpstr = ”;      $max = count($array);      for ($i = 1; $i = $num; $i++) {        $key = rand(0, $max – 1);        $tmpstr .= $array[$key];      }      return $tmpstr;  }    // 支付请求  public function payCurl($url, $type = ‘get’, $res = ‘json’, $arr = ”)  {      //1,初始化      $ch = curl_init();      //2,设置参数      curl_setopt($ch, CURLOPT_URL, $url);      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);      if ($type == ‘post’) {        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);//严格校验        curl_setopt($ch, CURLOPT_POST, 1);        curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);      }      //3,调用接口      $exec = curl_exec($ch);      //4,关闭      curl_close($ch);      if ($res == ‘json’) {        //成功时会返回 0  所以说下面的if判断不成立        if (curl_errno($ch)) {          return curl_errno($ch);        } else {          return json_decode($exec, true);        }      } elseif ($res == ‘xml’) {        $object = simplexml_load_string($exec, ‘SimpleXMLElement’, LIBXML_NOCDATA);        $json = json_encode($object);        return json_decode($json, true);      } else {        return $exec;      }  }    // 返回值 returnJson方法  function returnJson($code = ”, $msg = ”, $data = [])  {    $returnArr[‘code’] = $code;    $returnArr[‘msg’] = $msg;    $returnArr[‘data’] = $data;    return json($returnArr);  }

赞(0) 打赏
未经允许不得转载:E企盈小程序开发-热线:4006-838-530 » PHP小程序支付
分享到: 更多 (0)
E企盈小程序直播营销卖货系统
E企盈直播平台营销卖货系统

评论 抢沙发

E企盈小程序开发

联系我们联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏