电脑网站支付封装。以下是alipay.php的内容

<?php
/**
* @user : wy
* @date : 2018年7月23日
* @desc : 支付宝支付对接
**/
namespace Alipay;
use think\Exception;class Alipay {private $_config = [];public function __construct(){require_once 'config.php';$this->_config = $config;//注册自动加载方法spl_autoload_register([__CLASS__,'loadFile']);}/*** @param unknown $className* @return boolean* @desc:自动加载类文件*/private function loadFile($className){$fileName = $className.'.php';$currentPath = dirname(__FILE__).'/';$pagepayPath = $currentPath.'./pagepay/';$buildermodelPath = $currentPath.'pagepay/buildermodel/';$servicePath = $currentPath.'pagepay/service/';if(file_exists($pagepayPath.$fileName)){require_once $pagepayPath.$fileName;}elseif (file_exists($buildermodelPath.$fileName)){require_once $buildermodelPath.$fileName;}elseif (file_exists($servicePath.$fileName)){require_once $servicePath.$fileName;}else{return false;} return true;}/*** @param unknown $data* 参数名|是否必填|描述* out_trade_no|是|支付单号* subject|是|订单名称* total|是|支付金额* body|否|商品描述* * @desc:*/public function unifiedOrder($data){$this->setLog($data,'undifiedOrder-param:');//商户订单号,商户网站订单系统中唯一支付单号,必填$outTradeNo = trim($data['out_trade_no']);//订单名称,必填$subject = trim($data['subject']);//付款金额,必填$totalAmount = trim($data['total']);//商品描述,可空$body = isset($data['body'])?trim($data['body']):'';//构造参数$payRequestBuilder = new \AlipayTradePagePayContentBuilder();$payRequestBuilder->setBody($body);$payRequestBuilder->setSubject($subject);$payRequestBuilder->setTotalAmount($totalAmount);$payRequestBuilder->setOutTradeNo($outTradeNo);$aop = new \AlipayTradeService($this->_config);/*** pagePay 电脑网站支付请求* @param $builder 业务参数,使用buildmodel中的对象生成。* @param $return_url 同步跳转地址,公网可以访问* @param $notify_url 异步通知地址,公网可以访问* @return $response 支付宝返回的信息*/$response = $aop->pagePay($payRequestBuilder,$this->_config['return_url'],$this->_config['notify_url']);}/*** @param unknown $data* @param boolean $sign 是否验签* @throws Exception* @return * @desc:支付宝回调数据检测*/public function notify($data,$sign = false){try {if($sign){//验证签名$alipay = new \AlipayTradeService($this->_config);$result = $alipay->check($data);if(!$result) throw new Exception('验签失败');}//验证接口调用的app_id是否一致$appid = $data['app_id'];if($appid != $this->_config['app_id']){throw new Exception('app_id不一致');}//订单查询支付结果$queryData = ['transaction_id'=>$data['trade_no'],'out_trade_no'=>$data['out_trade_no']];$result = $this->orderQuery($queryData);if(!$result['state']) throw new Exception('订单查询结果:'.$result['msg']);if($result['data']['total_amount'] != $data['total_amount']) throw new Exception('回调金额与订单查询金额不等');return callback(true);} catch (Exception $e) {return callback(false,$e->getMessage());}}/*** 订单查询-支付宝* @param unknown $data*  参数名|是否必填|描述* transaction_id|二选一|支付宝交易号* out_trade_no|二选一|商户支付单号* @desc:*/public function orderQuery($data){try {$this->setLog($data,'orderQuery-param:');if(empty($data)) throw new Exception('参数为空');//构造参数$RequestBuilder = new \AlipayTradeQueryContentBuilder();if(isset($data['transaction_id']) && !empty($data['transaction_id'])){//支付宝交易号$RequestBuilder->setTradeNo($data['transaction_id']);}elseif(isset($data['out_trade_no']) && !empty($data['out_trade_no'])){$RequestBuilder->setOutTradeNo($out_trade_no);}else{throw new Exception('交易单号与支付单号不能同时为空');}$aop = new \AlipayTradeService($this->_config);/*** alipay.trade.query (统一收单线下交易查询)* @param $builder 业务参数,使用buildmodel中的对象生成。* @return $response 支付宝返回的信息*/$response = $aop->Query($RequestBuilder);$response = json_decode(json_encode($response),true);$this->setLog($response,'orderQuery-response:');if($response['code'] != '10000'){throw new Exception($response['msg']);}return callback(true,'',$response);} catch (Exception $e) {$this->setLog($e->getMessage(),'orderQuery-error:');return callback(false,$e->getMessage());}}/*** @param unknown $data*  参数名|是否必填|描述* transaction_id|二选一|支付宝交易号* out_trade_no|二选一|商户支付单号* refund_fee|是|退款总额,单位元* out_refund_no|是|平台退款单号,* @return 退款返回结果{"code": "10000","msg": "Success","buyer_logon_id": "nqy***@sandbox.com","buyer_user_id": "2088102175437002","fund_change": "N","gmt_refund_pay": "2018-07-24 16:22:23","out_trade_no": "610585760482976360","refund_fee": "30.00","send_back_fee": "0.00","trade_no": "2018072421001004000200631548"}* @desc:退款*/public function refund($data){try {$this->setLog($data,'refund-param:');//参数构造$RequestBuilder = new \AlipayTradeRefundContentBuilder();if(isset($data['transaction_id']) && !empty($data['transaction_id'])){$RequestBuilder->setTradeNo($data['transaction_id']);}elseif(isset($data['out_trade_no']) && !empty($data['out_trade_no'])){$RequestBuilder->setOutTradeNo($data['out_trade_no']);}else{throw new Exception('支付单号和支付宝交易号不能同时为空');}//退款总额,单位元$RequestBuilder->setRefundAmount($data['refund_fee']);//退款单号$RequestBuilder->setOutRequestNo($data['out_refund_no']);$aop = new \AlipayTradeService($this->_config);$response = $aop->Refund($RequestBuilder);$this->setLog($response,'refund-response:');$response = json_decode(json_encode($response),true);if($response['code'] != '10000'){throw new Exception($response['msg']);}return callback(true,'退款申请成功',$response);} catch (Exception $e) {$this->setLog($e->getMessage(),'refund-error:');return callback(false,$e->getMessage());}}/*** @param unknown $data* 参数|是否必填|描述* transaction_id|二选一|支付宝交易号* out_trade_no|二选一|商户支付单号* out_refund_no|是|平台退款单号,* @return* 返回参数{"code": "10000","msg": "Success","out_request_no": "2018072421001004000200631548","out_trade_no": "610585760482976360","refund_amount": "30.00","total_amount": "30.00","trade_no": "2018072421001004000200631548"}* @desc:退款查询*/public function refundQuery($data){try {$this->setLog($data,'refundQuery-param:');$RequestBuilder = new \AlipayTradeFastpayRefundQueryContentBuilder();if(isset($data['transaction_id']) && !empty($data['transaction_id'])){$RequestBuilder->setTradeNo($data['transaction_id']);}elseif(isset($data['out_trade_no']) && !empty($data['out_trade_no'])){$RequestBuilder->setOutTradeNo($data['out_trade_no']);}else{throw new Exception('支付单号和支付宝交易号不能同时为空');}$RequestBuilder->setOutRequestNo($data['out_refund_no']);$aop = new \AlipayTradeService($this->_config);$response = $aop->refundQuery($RequestBuilder);$this->setLog($response,'refundQuery-response:');$response = json_decode(json_encode($response),true);if($response['alipay_trade_fastpay_refund_query_response']['code'] != '10000'){throw new Exception($response['alipay_trade_fastpay_refund_query_response']['msg']);}return callback(true,'',$response['alipay_trade_fastpay_refund_query_response']);} catch (Exception $e) {$this->setLog($e->getMessage(),'refundQuery-error:');return callback(false,$e->getMessage());}}/*** @param unknown $content* @param string $prefix* @desc:记录日志*/public function setLog($contents,$prefix = ""){$path = RUNTIME_PATH.'/log/alipay/';setLog($contents,$prefix,$path);}
}

以下是notify.php中的异步通知处理

/**** @desc:支付宝支付结果同步回调*/public function alipayReturn(){$param = $this->request->param();$result = $this->dealNotify($param,false,1);if($result['state']){//支付成功$this->redirect(url('index/buy/payok',['pay_sn'=>$param['out_trade_no']]));}else{//支付失败$this->redirect(url(''));}}/*** @param unknown $data* @param boolean $sign 是否验签* @param boolean $type 1-同步处理 2-异步处理* @desc:处理支付宝同步通知和异步通知数据*/private function dealNotify($data,$sign = false,$type = '2'){try {$typeDesc = ($type == 1)?'return':'notify';$prefix = 'alipay-'.$typeDesc;$alipay = new Alipay();$alipay->setLog($data,$prefix."-param:");//1-alipay数据验证$result = $alipay->notify($data,$sign);if(!$result['state']) throw new Exception($result['msg']);//2-检测支付金额是否一致$pay = new Pay();$result = $pay->checkPayTotal($data['out_trade_no'], $data['total_amount']);$alipay->setLog('账单实际金额:'.$result['data']['total'].",实际支付金额:".$data['total_amount']);if(!$result['state']) throw new Exception($result['msg']);//修改订单相关数据$this->purOrderUpdate($data['out_trade_no'], $data['trade_no'],$data['total_amount']);$alipay->setLog('success',$prefix."-end:");return callback(true);} catch (Exception $e) {$alipay->setLog($e->getMessage(),$prefix."-end-error:");return callback(false,$e->getMessage());}}/*** * @desc:支付宝支付结果异步回调(包括部分退款异步通知。全额退款没有异步通知)*/public function alipayNotify(){try {if(!$this->request->isPost()) throw new Exception('非法请求');$param = $this->request->param();if(isset($param['refund_fee'])){$alipay = new Alipay();$alipay->setLog($param,'refund-notify:param:');//退款状态修改$this->_updateRefund($param['out_biz_no'],1);$alipay->setLog('success','refund-notify-end:');}else{//支付结果异步通知$result = $this->dealNotify($param);if(!$result['state']) throw new Exception($result['msg']);}return 'success';} catch (Exception $e) {$alipay->setLog($e->getMessage(),'notify-end-error:');return 'fail';}}

注意:申请退款后,部分退款才会有异步通知,全额退款不会触发退款结果的异步通知。退款结果的异步通知路径跟支付结果异步回调地址一致。退款回调参数中多refund_fee这个参数。退款回调参数如下:

{"gmt_create": "2018-07-26 09:45:05","charset": "UTF-8","gmt_payment": "2018-07-26 09:45:24","notify_time": "2018-07-26 09:47:37","subject": "","gmt_refund": "2018-07-26 09:47:36.849","sign": "drGhgTe9Ly60hN0fgXlrcu+xSH5+cx3ZJsNDKyCFDsRhHhD2eqANbaqXKgPMOBdUon3aYWa7Akv55lel2AZWSPDpJsDeG04V2Xd7Dr4GB\/dtIDAcrZ6Bb7a3+HZFnc8q+VxKZAJmEMKgMNX86Evv55FxtsGByqi53UASa4OQ2\/yhayjPBlcujUXhzLBb1sVDttGS5UZx7pDGYPlOpUaLnymxAcbPCHcOZY1Gt0MjYd4m\/AUKs1gByyaJdrX34++gi7chlSUruSFzwlRmVyI2eLEdb23tISvOox21wCMq6YdBYcUNlmZ2Mt6itaEItGWx8D9\/VJYdj1bLjnCSKn8f1A==","out_biz_no": "R201807260946373238851","buyer_id": "2088102175437002","version": "1.0","notify_id": "00a13b3a2640169502675254b25572ag02","notify_type": "trade_status_sync","out_trade_no": "","total_amount": "340.00","trade_status": "TRADE_SUCCESS","refund_fee": "240.00","trade_no": "2018072621001004000200641757","auth_app_id": "","app_id": "","sign_type": "RSA2","seller_id": ""
}

支付宝电脑网站版支付相关推荐

  1. django手机访问_Django对接支付宝电脑网站、App支付步骤详解!这个框架是真强!...

    一.前言 本文主要针对 Python for Django 在对接支付宝电脑网站.手机网站.App支付过程中具体实现步骤进行详解: 相信大家支付功能也写了不少,但时间一长,再次用到的时候有些细节难免会 ...

  2. 支付宝开放平台开发助手_支付宝:如何创建和接入支付宝电脑网站支付-新手必备...

    1.登录支付宝 1.打开支付宝官网,以"我是支付宝商家"用户登录 https://www.alipay.com/ 2.进入"产品中心",选择并申请支付类型 2. ...

  3. php网页跳转支付宝app,php 做支付宝电脑网站和app支付

    首先支付宝的文档真是看得头晕眼花,摸不着北的感觉,网上百度PHP支付宝一堆都是花里胡哨的,看得一脸懵逼.所以自己整理了一下,其实也没那么复杂直接上代码: php 电脑支付的方法: /** * 支付宝支 ...

  4. Spring Boot集成支付宝电脑网站支付功能

    Spring Boot集成支付宝电脑网站支付功能 接入准备 登录 创建应用 添加能力 生成私钥与公钥 开发设置 沙箱环境 示例Demo的使用与学习 下载Demo 启动项目 参数配置 执行测试 Spri ...

  5. php 支付宝电脑支付宝,PHP语言学习之php 做支付宝电脑网站和app支付

    本文主要向大家介绍了PHP语言学习之php 做支付宝电脑网站和app支付,通过具体的内容向大家展示,希望对大家学习php语言有所帮助. 首先支付宝的文档真是看得头晕眼花,摸不着北的感觉,网上百度PHP ...

  6. php 做支付宝电脑网站和app支付

    首先支付宝的文档真是看得头晕眼花,摸不着北的感觉,网上百度PHP支付宝一堆都是花里胡哨的,看得一脸懵逼.所以自己整理了一下,其实也没那么复杂直接上代码: php 电脑支付的方法: 1 2 3 4 5 ...

  7. Laravel 集成支付宝电脑网站支付

    前期准备: 1.下载SDK,里面还有Demo,可以参照Demo里面的内容快速接入 支付宝电脑网站支付SDK&Demo 2.把下载的zip文件解压,放到项目目录里,这里作者放在app文件夹里,方 ...

  8. 支付宝电脑网站支付 demo 启动过程

    接支付的这个需求,想了很久,看了官网总是没有头绪,依旧不知道怎么做,于是把demo下载下来跑一下,由于太菜,弄了一天. 准备工作: 支付宝开发平台登录后注册获取APPID,支付宝公钥,私钥 (http ...

  9. 接入支付宝电脑网站支付实现JAVA版

    简介 首先要说明的是个人感觉接入支付宝比微信简单多了,很轻松的,所以同学们不要紧张~ 当然还是老规矩啦,上来肯定的贴上官网地址,因为我这些服务天天在更新,而我的文章是教大家方法,而让你不是照葫芦画瓢 ...

最新文章

  1. 组策略轻松实现软件发布,Active Directory系列之二十二
  2. VS集成opencv编译C++项目遇到的问题
  3. Building System之 get_abs_build_var() get_build_var()
  4. 前端javascript实现二进制读写操作
  5. Android 5.1 Lollipop的Zygote分析——上篇
  6. 计算机组成原理CF和ZF,2017年广东工业大学计算机学院832计算机组成原理考研仿真模拟题...
  7. 03 jquery easyui 之 easyLoader 加载器
  8. 小程序 公众号/h5相互跳转-webview
  9. 根据注释生成项目文档
  10. 好用的对象转xml、xml转对象工具类-支持集合嵌套转换(Java实现)
  11. 【Java并发编程】—–“J.U.C”:ArrayBlockingQueue
  12. layer normalization 缺点_【收藏贴】不同种类质谱仪优缺点分析对比!
  13. vue实时显示当前时间
  14. python海龟隐藏_Python海龟绘图——常用方法指令
  15. 特长生模拟——买装备
  16. 我心中的超短系统之人气、情绪、大局观、仓控
  17. Android怎么制作圆角矩形,如何在Android UI中绘制圆角矩形?
  18. install diagnostic_updater
  19. 《所谓情商高就是会说话》
  20. 计算机主板供电,如何查看电脑主板是几项供电的?电脑主板供电相数基础知识科普...

热门文章

  1. 模板模式详解、模板模式怎么用、模板模式模板代码
  2. validate.js
  3. Illustrator CS5扭曲功能初学者必读(2)——宽度工具
  4. C语言-文件读写操作
  5. 场景应用:Redis使用setnx命令实现分布式锁
  6. google code
  7. 【C++】指针深度解析
  8. 智能音箱的五大核心技术
  9. nginx防御简单CC攻击的方法
  10. KVM虚拟化技术的-虚拟机配置文件