php实现支付宝当面付(扫码支付)功能
程序员文章站
2022-10-06 21:04:44
网上的很多php支付宝支付接入教程都颇为复杂,且需要配置和引入较多的文件,本人通过整理后给出一个单文件版的,希望可以给各位想接入支付宝支付的带来些许帮助和借鉴意义。
扫码...
网上的很多php支付宝支付接入教程都颇为复杂,且需要配置和引入较多的文件,本人通过整理后给出一个单文件版的,希望可以给各位想接入支付宝支付的带来些许帮助和借鉴意义。
扫码支付,指用户打开支付宝钱包中的“扫一扫”功能,扫描商家展示在某收银场景下的二维码并进行支付的模式。该模式适用于线下实体店支付、面对面支付等场景。
运行以下php文件代码即可生成一张付款二维码图片,使用支付宝扫一扫即可付款。
一个php文件搞定
一个php文件搞定微信支付系列
环境依赖
php5.0以上,且需要开启curl服务、ssl服务。
注意事项
1.文件开头的配置信息必须完善
2.商户私钥需填写对应签名算法类型的私钥,如何生成密钥参考:
2.1 生成rsa密钥
2.2
代码
<?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='miiepaibaakcaqea1mv+oy6mvgfxpm0mkpjt+fdzgmpovvmx2wf3gjwqpehbeup9jlxhvs32fz1ixi1e7wugq5tvxn28p8190kpon/c/g5t2caksuvemvf7ujn/n3z1hfmdt3omvcd14k05lgcfyz7z4c+a7zjf5bpcb6oshjjumbcy3hibuwzx/1j8agsod9lly1ofqxlj98k5zryihk900gmqs/wj3a1fc09dln9fuhbuyjtphaml+4w+sdkdzxpktxdfrmci7m7rnewg25xtst5z49ofpe84alxm7+oc9jyviptge00womsgtakn039uct/59bup6plko08rv85uxbqzgtcyahnhlfqidaqabaoibaqcbupm58s+j8kgb8ty5yiqrpoeaj+o2h4txn7a02/sfpqvntci0w3tpt5twsihulo+evytxjcitun7df2sp5pygzted5njlrtnu4zvhj+thjf8greriu9b4oxi/wrzjlrxzi+urei40ok+fwi0xgxdcdroy/eniedjfv8zpaqsuxg7vdwzijq/8d3mi31owv30kr9jfed15dbingjgsqr+qwrab4pbsmcw8hl6pylzopi1ygcefjrrnbemg40zt0oupsexqigamfvgqxtl5xo3dfezighdfwysbkz2m8ubae+r6lcndxi+o2hw4tncc1tdenmtjw7+h9s5aef5a8uwbaogbapxclwphuhcylixuz0d1soolzs9wk7kz1yswnzqrpegn+fos5/ji93yylge+kl31twbngqlawknwmx3qtzmkvtovmy8jevxbscsefm81q0wg/35e1skktxl66rqb2y0xfldcf3f9s8zieclqkywnshh0nqzrefixmmasj+3n2vhdaogbanftykzyrbs4ii/zcjmbyguyiknfnmrd+ta6ckogzqshfwxjcaz1rf4/xcqvac9nxuzjr/72qkn9z07uh6qszcqlzdrkiikak2uvqfdb+0abmk/tghxummdvmkyj2jezxg2rkg0kmg4qykkg/5tgg1on/2geznvpu8jpsfr1pdyhaogbanr8pctkc6fdfwp1c3qrtmry7zhc6rb4d4pjq5ump5+eypaizqi2f/dfd1qfuis3eurxygmqztoddyptdzvp/impnfs+pnbfryd0hfmrekquhivyzxogqknnsgbv5iyekctjaii9fxzinakzzei7+0a+jqud1kn3gogp50sze2ltaogaram5xpaa8rz6dgocfi9nn4/ch5fdzpfvhkdjmopv+lkinktw/tz+kiclalasdsfzt+ray9aje3nvuhtzox807swivr1xr3eplaced+0xrn3ajb34dzasku87wzw+cjdtmjfzgoofbsygji+op/wmop6jo/ybbwox88tcjrozsgecgyat8phhiypt5y/5pdb8edvd3xnes1fbkfzffsoaodsrkeokgrskl+9m3rcgx+s9dscyoh0ur3bftmhtioohc5qytt+bhmhip5mas4di4u/joqcwqbuyruggvk5it+6bfgat+jeb7ztautggptvfq3klbv0nz+xqyehvlnojnhypqg=='; $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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。