>  기사  >  백엔드 개발  >  PHP는 WeChat 기본 결제(스캔코드 결제) 기능을 구현합니다.

PHP는 WeChat 기본 결제(스캔코드 결제) 기능을 구현합니다.

不言
不言원래의
2018-05-31 16:36:362104검색

이 기사에서는 주로 WeChat 기본 결제 및 스캔 코드 결제 기능을 구현하기 위한 PHP를 소개합니다. 관심 있는 친구가 참조할 수 있습니다.

인터넷에 있는 많은 PHP WeChat 스캔 코드 결제 액세스 튜토리얼은 상당히 복잡하고 많은 구성과 파일 소개가 필요합니다. 정리한 후 코드가 200줄에 불과한 단일 파일 버전을 제공했습니다. WeChat 스캔 코드 결제에 액세스하려는 모든 사람에게 도움과 참고가 되기를 바랍니다.

결제 QR코드 사진을 얻으려면 이 파일을 직접 실행하세요.

참고 사항:

1. 이 파일은 결제 승인 디렉토리에 있어야 하며 WeChat Pay 판매자 플랫폼->제품 센터->개발 구성에서 설정할 수 있습니다.
2. 서명 오류가 표시되면 WeChat 결제 서명 확인 도구를 통해 확인할 수 있습니다: WeChat 공개 플랫폼 결제 인터페이스 디버깅 도구

코드는 다음과 같습니다.

<?php
header(&#39;Content-type:text/html; Charset=utf-8&#39;);
$mchid = &#39;xxxxx&#39;;     //微信支付商户号 PartnerID 通过微信支付商户资料审核后邮件发送
$appid = &#39;xxxxx&#39;; //公众号APPID 通过微信支付商户资料审核后邮件发送
$apiKey = &#39;xxxxx&#39;;  //https://pay.weixin.qq.com 帐户设置-安全设置-API安全-API密钥-设置API密钥
$wxPay = new WxpayService($mchid,$appid,$apiKey);
$outTradeNo = uniqid();   //你自己的商品订单号
$payAmount = 0.01;     //付款金额,单位:元
$orderName = &#39;支付测试&#39;;  //订单标题
$notifyUrl = &#39;https://www.xxx.com/wx/&#39;;   //付款成功后的回调地址(不要有问号)
$payTime = time();   //付款时间
$arr = $wxPay->createJsBizPackage($payAmount,$outTradeNo,$orderName,$notifyUrl,$payTime);
//生成二维码
$url = &#39;http://qr.liantu.com/api.php?text=&#39;.$arr[&#39;code_url&#39;];
echo "<img src=&#39;{$url}&#39; style=&#39;width:300px;&#39;>";

class WxpayService
{
  protected $mchid;
  protected $appid;
  protected $apiKey;

  public function __construct($mchid, $appid, $key)
  {
    $this->mchid = $mchid;
    $this->appid = $appid;
    $this->apiKey = $key;
  }

  /**
   * 发起订单
   * @param float $totalFee 收款总费用 单位元
   * @param string $outTradeNo 唯一的订单号
   * @param string $orderName 订单名称
   * @param string $notifyUrl 支付结果通知url 不要有问号
   * @param string $timestamp 订单发起时间
   * @return array
   */
  public function createJsBizPackage($totalFee, $outTradeNo, $orderName, $notifyUrl, $timestamp)
  {
    $config = array(
      &#39;mch_id&#39; => $this->mchid,
      &#39;appid&#39; => $this->appid,
      &#39;key&#39; => $this->apiKey,
    );
    $orderName = iconv(&#39;GBK&#39;,&#39;UTF-8&#39;,$orderName);
    $unified = array(
      &#39;appid&#39; => $config[&#39;appid&#39;],
      &#39;attach&#39; => &#39;pay&#39;,       //商家数据包,原样返回,如果填写中文,请注意转换为utf-8
      &#39;body&#39; => $orderName,
      &#39;mch_id&#39; => $config[&#39;mch_id&#39;],
      &#39;nonce_str&#39; => self::createNonceStr(),
      &#39;notify_url&#39; => $notifyUrl,
      &#39;out_trade_no&#39; => $outTradeNo,
      &#39;spbill_create_ip&#39; => &#39;127.0.0.1&#39;,
      &#39;total_fee&#39; => intval($totalFee * 100),    //单位 转为分
      &#39;trade_type&#39; => &#39;NATIVE&#39;,
    );
    $unified[&#39;sign&#39;] = self::getSign($unified, $config[&#39;key&#39;]);
    $responseXml = self::curlPost(&#39;https://api.mch.weixin.qq.com/pay/unifiedorder&#39;, self::arrayToXml($unified));
    $unifiedOrder = simplexml_load_string($responseXml, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
    if ($unifiedOrder === false) {
      die(&#39;parse xml error&#39;);
    }
    if ($unifiedOrder->return_code != &#39;SUCCESS&#39;) {
      die($unifiedOrder->return_msg);
    }
    if ($unifiedOrder->result_code != &#39;SUCCESS&#39;) {
      die($unifiedOrder->err_code);
    }
    $codeUrl = (array)($unifiedOrder->code_url);
    if(!$codeUrl[0]) exit(&#39;get code_url error&#39;);
    $arr = array(
      "appId" => $config[&#39;appid&#39;],
      "timeStamp" => $timestamp,
      "nonceStr" => self::createNonceStr(),
      "package" => "prepay_id=" . $unifiedOrder->prepay_id,
      "signType" => &#39;MD5&#39;,
      "code_url" => $codeUrl[0],
    );
    $arr[&#39;paySign&#39;] = self::getSign($arr, $config[&#39;key&#39;]);
    return $arr;
  }


  public function notify()
  {
    $config = array(
      &#39;mch_id&#39; => $this->mchid,
      &#39;appid&#39; => $this->appid,
      &#39;key&#39; => $this->apiKey,
    );
    $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

    $postObj = simplexml_load_string($postStr, &#39;SimpleXMLElement&#39;, LIBXML_NOCDATA);
    if ($postObj === false) {
      die(&#39;parse xml error&#39;);
    }
    if ($postObj->return_code != &#39;SUCCESS&#39;) {
      die($postObj->return_msg);
    }
    if ($postObj->result_code != &#39;SUCCESS&#39;) {
      die($postObj->err_code);
    }
    $arr = (array)$postObj;
    unset($arr[&#39;sign&#39;]);
    if (self::getSign($arr, $config[&#39;key&#39;]) == $postObj->sign) {
      echo &#39;<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg></xml>&#39;;
      return $postObj;
    }
  }

  /**
   * curl get
   *
   * @param string $url
   * @param array $options
   * @return mixed
   */
  public static function curlGet($url = &#39;&#39;, $options = array())
  {
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30);
    if (!empty($options)) {
      curl_setopt_array($ch, $options);
    }
    //https请求 不验证证书和host
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
  }

  public static function curlPost($url = &#39;&#39;, $postData = &#39;&#39;, $options = array())
  {
    if (is_array($postData)) {
      $postData = http_build_query($postData);
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
    curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
    if (!empty($options)) {
      curl_setopt_array($ch, $options);
    }
    //https请求 不验证证书和host
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    $data = curl_exec($ch);
    curl_close($ch);
    return $data;
  }

  public static function createNonceStr($length = 16)
  {
    $chars = &#39;abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789&#39;;
    $str = &#39;&#39;;
    for ($i = 0; $i < $length; $i++) {
      $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
    }
    return $str;
  }

  public static function arrayToXml($arr)
  {
    $xml = "<xml>";
    foreach ($arr as $key => $val) {
      if (is_numeric($val)) {
        $xml .= "<" . $key . ">" . $val . "</" . $key . ">";
      } else
        $xml .= "<" . $key . "><![CDATA[" . $val . "]]></" . $key . ">";
    }
    $xml .= "</xml>";
    return $xml;
  }
  /**
   * 获取签名
   */
  public static function getSign($params, $key)
  {
    ksort($params, SORT_STRING);
    $unSignParaString = self::formatQueryParaMap($params, false);
    $signStr = strtoupper(md5($unSignParaString . "&key=" . $key));
    return $signStr;
  }

  protected static function formatQueryParaMap($paraMap, $urlEncode = false)
  {
    $buff = "";
    ksort($paraMap);
    foreach ($paraMap as $k => $v) {
      if (null != $v && "null" != $v) {
        if ($urlEncode) {
          $v = urlencode($v);
        }
        $buff .= $k . "=" . $v . "&";
      }
    }
    $reqPar = &#39;&#39;;
    if (strlen($buff) > 0) {
      $reqPar = substr($buff, 0, strlen($buff) - 1);
    }
    return $reqPar;
  }
}

위는 전체 내용입니다. 이 기사를 읽어주셔서 감사합니다. 자세한 내용은 PHP 중국어 웹사이트를 참고하세요!

관련 추천:

php는 WeChat 결제의 현금 빨간 봉투를 실현합니다

php WeChat 결제의 공식 계정 결제 기능

위 내용은 PHP는 WeChat 기본 결제(스캔코드 결제) 기능을 구현합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.