失眠网,内容丰富有趣,生活中的好帮手!
失眠网 > 支付宝扫码支付php demo

支付宝扫码支付php demo

时间:2023-08-01 16:15:55

相关推荐

支付宝扫码支付php demo

支付宝扫码支付php demo

网上有个大神写了个案例,扫码成功后没有,做通知的部分。这里给说下思路

直接上代码,TP5,写的有点乱,讲究看吧。

Alipayserver 是原本demo中摘出来的请找扫码付款部分,大神地址

/alipay/demo.html

里面有用的支付宝的SDK,算是拼凑的吧,能完成任务就好,不喜勿喷!

<?php namespace app\api\controller; use app\common\controller\Api; use app\common\library\Ems; use app\common\library\Sms; use fast\Random; use think\Validate; use think\Db; use think\Config; use think\Cache; use app\api\controller\Alipayserver; use app\api\controller\Drivercaultorder; require_once ROOT_PATH."/extend/alipay/aop/AopClient.php"; require_once ROOT_PATH."/extend/alipay/aop/request/AlipayTradePayRequest.php"; require_once ROOT_PATH."/extend/phpqrcode/phpqrcode.php"; /** * 支付宝支付 * Class Pay * @package app\api\controller * @author wx:lynntim */ class F2fpay extends Api { public $appid =''; // 账户中心->密钥管理->开放平台密钥,填写添加了电脑网站支付的应用的APPID public $notifyUrl =''; //付款成功后的异步回调地址 //应用私钥 private $rsaPrivateKey=''; //应用公钥 private $publickey=''; //支付宝公钥 private $alipaypubkey=''; public function _initialize() { $this->notifyUrl =Config::get('WEBURL').'/api/F2fpay/alipaynotify'; $this->rsaPrivateKey =Config::get('facealipay')['rsaPrivateKey']; $this->appid =Config::get('facealipay')['appId']; $this->alipaypubkey=Config::get('facealipay')['alipayrsaPublicKey']; } public function newpay($ordersn,$paymoney,$paystr) { /*** 请填写以下配置信息 ***/ $outTradeNo =$ordersn; //你自己的商品订单号,不能重复 $payAmount = $paymoney; //付款金额,单位:元 $orderName =$paystr; //订单标题 $signType = 'RSA2'; //签名算法类型,支持RSA2和RSA,推荐使用RSA2 //商户私钥,填写对应签名算法类型的私钥,如何生成密钥参考:https://docs./291/105971和https://docs./200/105310 /*** 配置结束 ***/ $aliPay = new Alipayserver; $aliPay->setAppid($this->appid); $aliPay->setNotifyUrl($this->notifyUrl); $aliPay->setRsaPrivateKey($this->rsaPrivateKey); $aliPay->setTotalFee($payAmount); $aliPay->setOutTradeNo($outTradeNo); $aliPay->setOrderName($orderName); $result = $aliPay->doPay(); $result = $result['alipay_trade_precreate_response']; if($result['code'] && $result['code']=='10000'){ //生成二维码 $pic=$this->code($result['qr_code']); $update['alipaypic']=$pic; $update['alipre_time']=time(); $update['pre_time']=0; Db::table("ims_freight_orderchild")->where(array("order_id"=>$order_detail['order_id']))->update($update); return array('status'=>1,'msg'=>'成功','pic'=>$pic); }else{ return array('status'=>0,'msg'=>$result['msg'].' : '.$result['sub_msg']); } } public function code($url = "") { $qrcode = new \QRcode(); // $qrimage = new \QRimage(); $value = $url; //二维码内容 $errorCorrectionLevel = 'H'; //容错级别 $matrixPointSize = 6; //生成图片大小 $path=ROOT_PATH . 'public' . DS . 'uploads/'.date('Ymd'); if(!is_dir($path)) { mkdir($path); } $filename='/'.time().rand(1000,9999).'.png'; $filepath=$path.'/'.$filename; $img=Config::get('WEBURL').'/uploads/'.date('Ymd').$filename; $qrcode::png($value,$filepath , $errorCorrectionLevel, $matrixPointSize, 2); // $object->png($url, false, $errorCorrectionLevel, $matrixPointSize, 2); //这里就是把生成的图片流从缓冲区保存到内存对象上,使用base64_encode变成编码字符串,通过json返回给页面。 return $img; // return json($data); } /** * 支付回调 */ public function alipaynotify() { $paths=dirname(dirname(dirname(dirname(__FILE__)))); $str=file_get_contents('php://input'); file_put_contents($paths.'/public/text.txt',"\n\n支付后传递".date('Y-m-d H:i:s')."\n".print_r($this->request->request(),true),FILE_APPEND); $post=$this->request->request(); $aop = new \AopClient(); $aop->gatewayUrl = Config::get('facealipay')['gatewayUrl']; $aop->appId =$this->appId; $aop->rsaPrivateKey = $this->rsaPrivateKey; $aop->format = Config::get('facealipay')['format']; $aop->charset = Config::get('facealipay')['charset']; $aop->signType =Config::get('facealipay')['signType']; $aop->alipayrsaPublicKey = $this->alipaypubkey; unset($post['s']); $flag = $aop->rsaCheckV1($post,NULL,"RSA2"); //验签 if($flag){ $ss=1; } else { $ss=0; } $order_sn = $post['out_trade_no']; $trade_status = $post['trade_status']; $amount=$post['total_amount']; // 支付宝交易号 $trade_no =$post['trade_no']; if ($trade_status == 'TRADE_FINISHED' || $trade_status == 'TRADE_SUCCESS') { $orderinfo=Db::table('table')->where(array('childordersn'=>$order_sn))->find(); $order_amount=$orderinfo['paymoney']; if($orderinfo['uid'] == 13 ) $order_amount=0.01; if ($order_amount == $amount) { //更新订单状态,自身逻辑,不做赘述 } echo 'success';exit; } else { echo 'fail';exit; } } /** * 支付宝支付 * @param $body 名称 * @param $total_amount 价格 * @param $product_code 订单号 * @param $notify_url 回调地址 * @return string */ public function alipay($body, $total_amount, $product_code, $notify_url) { $aop = new \AopClient(); $aop->gatewayUrl = Config::get('alipay')['gatewayUrl']; $aop->appId = Config::get('alipay')['appId']; $aop->rsaPrivateKey = Config::get('alipay')['rsaPrivateKey']; $aop->format = Config::get('alipay')['format']; $aop->charset = Config::get('alipay')['charset']; $aop->signType = Config::get('alipay')['signType']; $aop->alipayrsaPublicKey = Config::get('alipay')['alipayrsaPublicKey']; $request = new \AlipayTradePayRequest(); $arr['body'] = $body; $arr['subject'] = $body; $arr['out_trade_no'] = $product_code; $arr['timeout_express'] = '100m'; $arr['total_amount'] = floatval($total_amount); $arr['product_code'] = 'QUICK_MSECURITY_PAY'; $json = json_encode($arr); if(!$notify_url) $notify_url=Config::get('WEBURL').'/api/Native/alipaynotify'; $request->setNotifyUrl($notify_url); $request->setBizContent($json); $response = $aop->sdkExecute($request); $pic=$this->code($response ); return $pic; } }

如果觉得《支付宝扫码支付php demo》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。