对于每个刚开始工作的新手来说,无论支付宝支付还是微信支付都是跑不掉的一个小门槛。
在加上本人比较技术比较渣(比较懒导致的),不太喜欢引用那么大的SDK,于是就简单集成了一下支付宝的支付。
但也只是只有支付而已哦,其他的一些功能还是自己用自己在集成吧,嘻嘻(#^.^#)还是懒。
废话不多说了,直接上代码:
/** * 支付宝支付 */ class Alipay { private $config =[ "app_id" => "************", //支付宝APPid "rsaPrivateKey" => "************", //支付宝私钥 "rsaPublicKey" => "************", //支付宝公钥 "charset" => "utf-8", "sign_type" => "RSA2", "version" => "1.0", ]; private $submitUrl = "https://openapi.alipay.com/gateway.do" ;//提交地址 public $calltype = "app";//pc电脑端 app 手机和app端公用 public $submit_auto=true; public function index($data) { $calltype=$this->calltype; return $this->$calltype($data); } public function app($data) { $return_url=$data['return_url']; $notify_url=$data['notify_url']; $subject=$data['subject']; $order_sn=$data['order_sn']; $total=$data['total']; $biz_content=[ 'body'=>'', 'subject'=>$subject, 'out_trade_no'=>$order_sn, 'timeout_express'=>'30m', 'total_amount'=>$total, 'product_code'=>'QUICK_MSECURITY_PAY', ]; $postdata=[ 'app_id'=>$this->config['app_id'], 'method'=>'alipay.trade.app.pay', 'format'=>'json', 'charset'=>$this->config['charset'], 'sign_type'=>$this->config['sign_type'], 'sign'=>'', 'timestamp'=>date('Y-m-d H:i:s'), 'version'=>'1.0', 'notify_url'=>$notify_url, 'biz_content'=>json_encode($biz_content), ]; $signdata=$this->alisign($postdata); $str=$this->keyandvalStr($signdata,1); //返回链接不能带官方https链接 return $str; } public function pc($data) { $return_url=$data['return_url']; $notify_url=$data['notify_url']; $biz_content=[ "total_amount"=>$data['total'], "product_code"=>"FAST_INSTANT_TRADE_PAY", "subject"=>$data['subject'], "out_trade_no"=>$data['order_sn'], "goods_type"=>1,//商品类型 0虚拟商品 1实物商品 ]; $post=[ 'app_id'=>$this->config['app_id'], 'method'=>'alipay.trade.page.pay', 'format'=>'JSON', 'return_url'=>$return_url, 'charset'=>$this->config['charset'], 'sign_type'=>$this->config['sign_type'], // 'sign'=>$sign, 'timestamp'=>date('Y-m-d H:i:s'), 'version'=>$this->config['version'], 'notify_url'=>$notify_url, 'biz_content'=>json_encode($biz_content), ]; $signdata=$this->alisign($post); $html=$this->formsubmithtml($this->submitUrl,$signdata); return $html; } public function mobile($data) { $return_url=$data['return_url']; $notify_url=$data['notify_url']; $biz_content=[ "total_amount"=>$data['total'], "product_code"=>"QUICK_WAP_WAY", "subject"=>$data['subject'], "out_trade_no"=>$data['order_sn'], "goods_type"=>1,//商品类型 0虚拟商品 1实物商品 ]; $post=[ 'app_id'=>$this->config['app_id'], 'method'=>'alipay.trade.wap.pay', 'format'=>'JSON', 'return_url'=>$return_url, 'charset'=>$this->config['charset'], 'sign_type'=>$this->config['sign_type'], // 'sign'=>$sign, 'timestamp'=>date('Y-m-d H:i:s'), 'version'=>$this->config['version'], 'notify_url'=>$notify_url, 'biz_content'=>json_encode($biz_content), ]; $signdata=$this->alisign($post); $str=$this->keyandvalStr($signdata,1); $url="https://openapi.alipay.com/gateway.do?".$str; return $url; } public function alisign($data) { $rsaPrivateKey=$this->config['rsaPrivateKey']; $buff = ""; ksort($data); $buff=$this->keyandvalStr($data); $str = chunk_split($rsaPrivateKey, 64, " "); $res = "-----BEGIN RSA PRIVATE KEY----- $str-----END RSA PRIVATE KEY----- "; $sign=$this->ras2Sign($buff,$res); $data['sign']=$sign; return $data; } function keyandvalStr($data,$flg=0) { $buff=""; foreach ($data as $k => $v) { if($flg!=0 && $v != "" && !is_array($v)) { $v=urlencode($v); $buff .= $k . "=" . $v . "&"; }else{ if($k != "sign" && $v != "" && !is_array($v)){ $buff .= $k . "=" . $v . "&"; } } } $buff = trim($buff, "&"); return $buff; } function ras2Sign($buff,$res) { $sign=''; openssl_sign($buff, $sign, $res, OPENSSL_ALGO_SHA256); $sign = base64_encode($sign); return $sign; } function formsubmithtml($action,$data) { $sHtml = "<form id='alipaysubmit' name='alipaysubmit' action='".$action."' method='POST'>"; foreach ($data as $key => $value) { $val = str_replace("'","'",$value); $sHtml.= "<input type='hidden' name='".$key."' value='".$val."'/>"; } $sHtml = $sHtml."<input type='submit' class='paysubmit' value='ok' ></form>"; if($this->submit_auto==true) { $sHtml = $sHtml."<script>document.forms['alipaysubmit'].submit();</script>"; } return $sHtml; } }
调用方法:
use payAlipay; $callD=[ 'return_url' => '支付完成跳转路径', 'notify_url' => '回调路径', 'total' => '支付总额', 'subject' => '支付说明', 'order_sn' => '生成订单号', ]; $url=$this->Alipay($callD); public function Alipay($data,$flg=true) { $pay = new Alipay(); $pay->calltype='app'; $pay->submit_auto=$flg; $pay=$pay->index($data); return $pay; }
以上即是本次分享的内容。
感谢您的观看。
2019年05月10日