PHP开发支付宝PC扫码支付/支付宝当面付开发流程
对于支付宝支付的开发,尽管官网文档描述的已经比较清楚了,但还是有像我这样的小白仍然还是不会。。。。今天好好的摸索了一天,在这里分享记录了一下。
首先我们要拿到企业的支付宝开放平台账号(我做的是企业,个人的我不清楚能不能做),登录进去后创建一个应用(如果之前没有创建的话);
然后我们就能看到你新创建的应用了,但现在是用不了的,需要完善信息提交审核,通过之后才能使用,点击查看进入应用
上传应用图标,配置授权回调地址和应用公钥,这里的回调地址一定是公网可以访问得到的,不是你本地的测试地址!
不会生成秘钥的话就点击上面 如何生成秘钥?
填完了这些信息后就可以提交审核了,审核需要一点时间。这时我们可以先写我们的逻辑代码然后利用支付宝沙箱进行测试。
开发可以下载支付宝官方的SDK参考,也可以自行百度大神们整理的代码,这里小编直接贴出来:
- <?php
- header('Content-type:text/html; Charset=utf-8');
- $appid = 'xxxxx'; //https://open.alipay.com 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID
- $notifyUrl = 'http://www.xxx.com/alipay/notify.php'; //付款成功后的异步回调地址
- $outTradeNo = uniqid(); //你自己的商品订单号
- $payAmount = 0.01; //付款金额,单位:元
- $orderName = '支付测试'; //订单标题
- $signType = 'RSA2'; //签名算法类型,支持RSA2和RSA,推荐使用RSA2
- //商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs.open.alipay.com/291/105971和https://docs.open.alipay.com/200/105310
- $saPrivateKey='这里填你的商户私钥';
- $aliPay = new AlipayService($appid,$returnUrl,$notifyUrl,$saPrivateKey);
- $result = $aliPay->doPay($payAmount,$outTradeNo,$orderName,$returnUrl,$notifyUrl);
- $result = $result['alipay_trade_precreate_response'];
- if($result['code'] && $result['code']=='10000'){
- //生成二维码
- $url = 'http://pan.baidu.com/share/qrcode?w=300&h=300&url='.$result['qr_code'];
- echo "<img src='{$url}' style='width:300px;'><br>";
- echo '二维码内容:'.$result['qr_code'];
- }else{
- echo $result['msg'].' : '.$result['sub_msg'];
- }
- class AlipayService
- {
- protected $appId;
- protected $returnUrl;
- protected $notifyUrl;
- //私钥文件路径
- protected $rsaPrivateKeyFilePath;
- //私钥值
- protected $rsaPrivateKey;
- public function __construct($appid, $returnUrl, $notifyUrl,$saPrivateKey)
- {
- $this->appId = $appid;
- $this->returnUrl = $returnUrl;
- $this->notifyUrl = $notifyUrl;
- $this->charset = 'utf8';
- $this->rsaPrivateKey=$saPrivateKey;
- }
- /**
- * 发起订单
- * @param float $totalFee 收款总费用 单位元
- * @param string $outTradeNo 唯一的订单号
- * @param string $orderName 订单名称
- * @param string $notifyUrl 支付结果通知url 不要有问号
- * @param string $timestamp 订单发起时间
- * @return array
- */
- public function doPay($totalFee, $outTradeNo, $orderName, $returnUrl,$notifyUrl)
- {
- //请求参数
- $requestConfigs = array(
- 'out_trade_no'=>$outTradeNo,
- 'total_amount'=>$totalFee, //单位 元
- 'subject'=>$orderName, //订单标题
- );
- $commonConfigs = array(
- //公共参数
- 'app_id' => $this->appId,
- 'method' => 'alipay.trade.precreate', //接口名称
- 'format' => 'JSON',
- 'charset'=>$this->charset,
- 'sign_type'=>'RSA2',
- 'timestamp'=>date('Y-m-d H:i:s'),
- 'version'=>'1.0',
- 'notify_url' => $notifyUrl,
- 'biz_content'=>json_encode($requestConfigs),
- );
- $commonConfigs["sign"] = $this->generateSign($commonConfigs, $commonConfigs['sign_type']);
- $result = $this->curlPost('https://openapi.alipay.com/gateway.do',$commonConfigs);
- return json_decode($result,true);
- }
- public function generateSign($params, $signType = "RSA") {
- return $this->sign($this->getSignContent($params), $signType);
- }
- protected function sign($data, $signType = "RSA") {
- $priKey=$this->rsaPrivateKey;
- $res = "-----BEGIN RSA PRIVATE KEY-----\n" .
- wordwrap($priKey, 64, "\n", true) .
- "\n-----END RSA PRIVATE KEY-----";
- ($res) or die('您使用的私钥格式错误,请检查RSA私钥配置');
- if ("RSA2" == $signType) {
- openssl_sign($data, $sign, $res, version_compare(PHP_VERSION,'5.4.0', '<') ? SHA256 : OPENSSL_ALGO_SHA256); //OPENSSL_ALGO_SHA256是php5.4.8以上版本才支持
- } else {
- openssl_sign($data, $sign, $res);
- }
- $sign = base64_encode($sign);
- return $sign;
- }
- /**
- * 校验$value是否非空
- * if not set ,return true;
- * if is null , return true;
- **/
- protected function checkEmpty($value) {
- if (!isset($value))
- return true;
- if ($value === null)
- return true;
- if (trim($value) === "")
- return true;
- return false;
- }
- public function getSignContent($params) {
- ksort($params);
- $stringToBeSigned = "";
- $i = 0;
- foreach ($params as $k => $v) {
- if (false === $this->checkEmpty($v) && "@" != substr($v, 0, 1)) {
- // 转换成目标字符集
- $v = $this->characet($v, $this->charset);
- if ($i == 0) {
- $stringToBeSigned .= "$k" . "=" . "$v";
- } else {
- $stringToBeSigned .= "&" . "$k" . "=" . "$v";
- }
- $i++;
- }
- }
- unset ($k, $v);
- return $stringToBeSigned;
- }
- /**
- * 转换字符集编码
- * @param $data
- * @param $targetCharset
- * @return string
- */
- function characet($data, $targetCharset) {
- if (!empty($data)) {
- $fileType = $this->charset;
- if (strcasecmp($fileType, $targetCharset) != 0) {
- $data = mb_convert_encoding($data, $targetCharset, $fileType);
- //$data = iconv($fileType, $targetCharset.'//IGNORE', $data);
- }
- }
- return $data;
- }
- public function curlPost($url = '', $postData = '', $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;
- }
- }
之后我们可以利用支付宝的沙箱环境进行测试我们的代码是否ok,在开发者中心找到沙箱环境。
这里他会给你分配好appid这些信息,公钥可以用你的应用的公钥(我是这样用的,不知道有没问题),然后你就可以拿这appid,公钥秘钥等去进行测试了(沙箱的网关地址和正式环境的网关地址是不同的!!!这里特别注意!!!),以下是我获取到的二维码信息,通过第三方工具可以生成二维码(比如jquery的qrcode)
然后我们就在页面将返回的支付码供用户扫码支付了。
这里是不能直接用你的支付宝去扫的,不然的话会提示二维码已失效,请刷新的字样。沙箱环境的需要下载沙箱版的支付宝进行扫码支付,下载地址https://openhome.alipay.com/platform/appDaily.htm,目前只有安卓版,用安卓手机下载就好,安装好了直接登录,不是使用你自己的支付宝账号去登录,沙箱账号在
用买家账号登录进去扫码支付就好了,等待支付成功我们的任务就完成啦,等待应用审核通过把正式的appid等(一定要记得替换网关地址)替换掉就可以使用啦。
这里只是实现了扫码,没有实现验签,验签是灰常重要的,不过这块比较简单,按照支付宝官方的文档操作就行了。
本文网址:https://www.zztuku.com/index.php/detail-7913.html
站长图库 - PHP开发支付宝PC扫码支付/支付宝当面付开发流程
申明:如有侵犯,请 联系我们 删除。
您还没有登录,请 登录 后发表评论!
提示:请勿发布广告垃圾评论,否则封号处理!!