php支付宝系列之电脑网站支付
程序员文章站
2022-12-28 23:13:23
网上的很多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 $returnurl = 'http://www.xxx.com/alipay/return.php'; //付款成功后的同步回调地址 $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/1j8agsod9llyxofqxlj98k5zryihk900gmqs/wj3a1fc09dln9fuhbuyjtphaml+4w+sdkdzxpktxdfrmci7m7rnewg25xtst5z49ofpe84alxm7+oc9jyviptge00womsgtakn039uct/59bup6plko08rv85uxbqzgtcyahnhlfqidaqabaoibaqcbup258s+j8kgb8ty5yiqrpoeaj+o2h4txn7a02/sfpqvntci0wstpt5twsihulo+evytxjcitun7df2sp5pygzted5njlrtnu4zvhj+thjf8greriu9b4oxi/wrzjlrxzi+urei40ok+fwi0xgxdcdroy/eniedjfv8zpaqsuxg7vdwzijq/8d3mi31owv30kr9jfed15dbingjgsqr+qwrab4pbsmcw8hl6pylzopi1ygcefjrrnbefg40zt0oupsexqigamfvgqxtl5xo3dfezighdfwysbkz2m8ubae+r6lcndxi+o2hw4tncc1tdenmtjw7+h9s5aef5a8uwbaogbapxclwphuhcylixuz0d1soolzs9wk7kz1yswnzqrpegn+fos5/ji93yylge+kl31twbngqlawknwmx3qtzmkvtovmy8jevxbscsefm81q0wg/35e1skktxl66rqb2y0xfldcf3f9s8zieclqkywnshh0nqzrefixmmasj+3n2vhdaogbanftykzyrbs4ii/zcjmbyguyiknfnmrd+ta6ckogzqshfwxjcaz1rf4/xcqvac9nxuzjr/72qkn9z07uh6qszcqlzdrkiikak2uvqfdb+0abmk/tghxummdvmkyj2jezxg2rkg0kmg4qykkg/5tgg1on/0geznvpu8jpsfr1pdyhaogbanr8pctkc6fdfwp1c3qrtmry7zhc6rb4d4pjq5ump5+eypaizqi2f/dfd1qfuis3eurxygmqztoddyptdzvp/impnfs+pnbfryd0hfmrekquhivyzxogqknnsgbv5iy3kctjaii9fxzinakzzei7+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); $shtml = $alipay->dopay($payamount,$outtradeno,$ordername,$returnurl,$notifyurl); echo $shtml; class alipayservice { protected $appid; protected $returnurl; protected $notifyurl; protected $charset; //私钥值 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, 'product_code'=>'fast_instant_trade_pay', 'total_amount'=>$totalfee, //单位 元 'subject'=>$ordername, //订单标题 ); $commonconfigs = array( //公共参数 'app_id' => $this->appid, 'method' => 'alipay.trade.page.pay', //接口名称 'format' => 'json', 'return_url' => $returnurl, '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']); return $this->buildrequestform($commonconfigs); } /** * 建立请求,以表单html形式构造(默认) * @param $para_temp 请求参数数组 * @return 提交表单html文本 */ protected function buildrequestform($para_temp) { $shtml = "<form id='alipaysubmit' name='alipaysubmit' action='https://openapi.alipay.com/gateway.do?charset=".$this->charset."' method='post'>"; while (list ($key, $val) = each ($para_temp)) { if (false === $this->checkempty($val)) { $val = str_replace("'","'",$val); $shtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; } } //submit按钮控件请不要含有name属性 $shtml = $shtml."<input type='submit' value='ok' style='display:none;''></form>"; $shtml = $shtml."<script>document.forms['alipaysubmit'].submit();</script>"; return $shtml; } 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; } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。