PHP开发支付宝PC扫码支付/支付宝当面付开发流程

 5823

对于支付宝支付的开发,尽管官网文档描述的已经比较清楚了,但还是有像我这样的小白仍然还是不会。。。。今天好好的摸索了一天,在这里分享记录了一下。

首先我们要拿到企业的支付宝开放平台账号(我做的是企业,个人的我不清楚能不能做),登录进去后创建一个应用(如果之前没有创建的话);


5f3a1785d914d.jpg


然后我们就能看到你新创建的应用了,但现在是用不了的,需要完善信息提交审核,通过之后才能使用,点击查看进入应用

上传应用图标,配置授权回调地址和应用公钥,这里的回调地址一定是公网可以访问得到的,不是你本地的测试地址!


5f3a17cc46283.jpg

不会生成秘钥的话就点击上面 如何生成秘钥  

填完了这些信息后就可以提交审核了,审核需要一点时间。这时我们可以先写我们的逻辑代码然后利用支付宝沙箱进行测试。

开发可以下载支付宝官方的SDK参考,也可以自行百度大神们整理的代码,这里小编直接贴出来:

  1. <?php
  2. header('Content-type:text/html; Charset=utf-8');
  3. $appid = 'xxxxx'; //https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID
  4. $notifyUrl = 'http://www.xxx.com/alipay/notify.php';   //付款成功后的异步回调地址
  5. $outTradeNo = uniqid();   //你自己的商品订单号
  6. $payAmount = 0.01;     //付款金额,单位:元
  7. $orderName = '支付测试';  //订单标题
  8. $signType = 'RSA2';    //签名算法类型,支持RSA2和RSA,推荐使用RSA2
  9. //商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
  10. $saPrivateKey='这里填你的商户私钥';
  11. $aliPay = new AlipayService($appid,$returnUrl,$notifyUrl,$saPrivateKey);
  12. $result = $aliPay->doPay($payAmount,$outTradeNo,$orderName,$returnUrl,$notifyUrl);
  13. $result = $result['alipay_trade_precreate_response'];
  14. if($result['code'] && $result['code']=='10000'){
  15.     //生成二维码
  16.     $url = 'http://pan.baidu.com/share/qrcode?w=300&h=300&url='.$result['qr_code'];
  17.     echo "<img src='{$url}' style='width:300px;'><br>";
  18.     echo '二维码内容:'.$result['qr_code'];
  19. }else{
  20.     echo $result['msg'].' : '.$result['sub_msg'];
  21. }
  22. class AlipayService
  23. {
  24.     protected $appId;
  25.     protected $returnUrl;
  26.     protected $notifyUrl;
  27.     //私钥文件路径
  28.     protected $rsaPrivateKeyFilePath;
  29.     //私钥值
  30.     protected $rsaPrivateKey;
  31.     public function __construct($appid, $returnUrl, $notifyUrl,$saPrivateKey)
  32.     {
  33.         $this->appId = $appid;
  34.         $this->returnUrl = $returnUrl;
  35.         $this->notifyUrl = $notifyUrl;
  36.         $this->charset = 'utf8';
  37.         $this->rsaPrivateKey=$saPrivateKey;
  38.     }
  39.     /**
  40.      * 发起订单
  41.      * @param float $totalFee 收款总费用 单位元
  42.      * @param string $outTradeNo 唯一的订单号
  43.      * @param string $orderName 订单名称
  44.      * @param string $notifyUrl 支付结果通知url 不要有问号
  45.      * @param string $timestamp 订单发起时间
  46.      * @return array
  47.      */
  48.     public function doPay($totalFee, $outTradeNo, $orderName, $returnUrl,$notifyUrl)
  49.     {
  50.         //请求参数
  51.         $requestConfigs = array(
  52.             'out_trade_no'=>$outTradeNo,
  53.             'total_amount'=>$totalFee, //单位 元
  54.             'subject'=>$orderName, //订单标题
  55.         );
  56.         $commonConfigs = array(
  57.             //公共参数
  58.             'app_id' => $this->appId,
  59.             'method' => 'alipay.trade.precreate',       //接口名称
  60.             'format' => 'JSON',
  61.             'charset'=>$this->charset,
  62.             'sign_type'=>'RSA2',
  63.             'timestamp'=>date('Y-m-d H:i:s'),
  64.             'version'=>'1.0',
  65.             'notify_url' => $notifyUrl,
  66.             'biz_content'=>json_encode($requestConfigs),
  67.         );
  68.         $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
  69.         $result = $this->curlPost('https://openapi.alipay.com/gateway.do',$commonConfigs);
  70.         return json_decode($result,true);
  71.     }
  72.     public function generateSign($params, $signType = "RSA") {
  73.         return $this->sign($this->getSignContent($params), $signType);
  74.     }
  75.     protected function sign($data, $signType = "RSA") {
  76.         $priKey=$this->rsaPrivateKey;
  77.         $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
  78.         wordwrap($priKey, 64, "\n", true) .
  79.         "\n-----END RSA PRIVATE KEY-----";
  80.         ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
  81.         if ("RSA2" == $signType) {
  82.             openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
  83.         } else {
  84.             openssl_sign($data, $sign, $res);
  85.         }
  86.         $sign = base64_encode($sign);
  87.         return $sign;
  88.     }
  89.     /**
  90.      * 校验$value是否非空
  91.      * if not set ,return true;
  92.      *  if is null , return true;
  93.      **/
  94.     protected function checkEmpty($value) {
  95.         if (!isset($value))
  96.             return true;
  97.         if ($value === null)
  98.             return true;
  99.         if (trim($value) === "")
  100.             return true;
  101.         return false;
  102.     }
  103.     public function getSignContent($params) {
  104.         ksort($params);
  105.         $stringToBeSigned = "";
  106.         $i = 0;
  107.         foreach ($params as $k => $v) {
  108.             if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
  109.                 // 转换成目标字符集
  110.                 $v = $this->characet($v, $this->charset);
  111.                 if ($i == 0) {
  112.                     $stringToBeSigned .= "$k" . "=" . "$v";
  113.                 } else {
  114.                     $stringToBeSigned .= "&" . "$k" . "=" . "$v";
  115.                 }
  116.                 $i++;
  117.             }
  118.         }
  119.         unset ($k, $v);
  120.         return $stringToBeSigned;
  121.     }
  122.     /**
  123.      * 转换字符集编码
  124.      * @param $data
  125.      * @param $targetCharset
  126.      * @return string
  127.      */
  128.     function characet($data, $targetCharset) {
  129.         if (!empty($data)) {
  130.             $fileType = $this->charset;
  131.             if (strcasecmp($fileType, $targetCharset) != 0) {
  132.                 $data = mb_convert_encoding($data, $targetCharset, $fileType);
  133.                 //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
  134.             }
  135.         }
  136.         return $data;
  137.     }
  138.     public function curlPost($url = '', $postData = '', $options = array())
  139.     {
  140.         if (is_array($postData)) {
  141.             $postData = http_build_query($postData);
  142.         }
  143.         $ch = curl_init();
  144.         curl_setopt($ch, CURLOPT_URL, $url);
  145.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  146.         curl_setopt($ch, CURLOPT_POST, 1);
  147.         curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
  148.         curl_setopt($ch, CURLOPT_TIMEOUT, 30); //设置cURL允许执行的最长秒数
  149.         if (!empty($options)) {
  150.             curl_setopt_array($ch, $options);
  151.         }
  152.         //https请求 不验证证书和host
  153.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
  154.         curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
  155.         $data = curl_exec($ch);
  156.         curl_close($ch);
  157.         return $data;
  158.     }
  159. }

之后我们可以利用支付宝的沙箱环境进行测试我们的代码是否ok,在开发者中心找到沙箱环境。


5f3a19907248b.jpg


这里他会给你分配好appid这些信息,公钥可以用你的应用的公钥(我是这样用的,不知道有没问题),然后你就可以拿这appid,公钥秘钥等去进行测试了(沙箱的网关地址和正式环境的网关地址是不同的!!!这里特别注意!!!),以下是我获取到的二维码信息,通过第三方工具可以生成二维码(比如jquery的qrcode)


5f3a19c2c48f1.jpg


然后我们就在页面将返回的支付码供用户扫码支付了。


5f3a1a09a72e8.jpg


这里是不能直接用你的支付宝去扫的,不然的话会提示二维码已失效,请刷新的字样。沙箱环境的需要下载沙箱版的支付宝进行扫码支付,下载地址https://openhome.alipay.com/platform/appDaily.htm,目前只有安卓版,用安卓手机下载就好,安装好了直接登录,不是使用你自己的支付宝账号去登录,沙箱账号在


5f3a1a480f58b.jpg


用买家账号登录进去扫码支付就好了,等待支付成功我们的任务就完成啦,等待应用审核通过把正式的appid等(一定要记得替换网关地址)替换掉就可以使用啦。

这里只是实现了扫码,没有实现验签,验签是灰常重要的,不过这块比较简单,按照支付宝官方的文档操作就行了。




本文网址:https://www.zztuku.com/index.php/detail-7913.html
站长图库 - PHP开发支付宝PC扫码支付/支付宝当面付开发流程
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐