123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- namespace app\common\library;
- /**
- * 支付宝插件 使用需 require_once("./Plugins/AliPay/AliPay.php");
- * @author Jack_YanTC <627495692@qq.com>
- */
- class AliPay {
- private $appId;
- private $rsaPrivateKey;
- private $signType;
- private $alipayrsaPublicKey;
- private $notifyUrl;
- private $returnUrl;
- /**
- * 初始化参数
- *
- * @param array $options
- * @param $options ['appId'] 应用ID,在支付宝上获取
- * @param $options ['rsaPrivateKey'] 应用密钥,与应用公钥一组,公钥填写到支付宝
- * @param $options ['signType'] 签名方式
- * @param $options ['alipayrsaPublicKey'] 支付宝公钥,在支付宝上获取
- * @param $options ['notifyUrl'] 支付宝回调地址
- * @param $options ['returnUrl'] 用于web支付返回地址
- */
- public function __construct($options = array()) {
- $this->appId = isset ($options ['appId']) ? $options ['appId'] : config('AliPay')['appId'];
- $this->rsaPrivateKey = isset ($options ['rsaPrivateKey']) ? $options ['rsaPrivateKey'] : config('AliPay')['rsaPrivateKey'];
- $this->signType = isset ($options ['signType']) ? $options ['signType'] : config('AliPay')['signType'];
- $this->notifyUrl = isset ($options ['notifyUrl']) ? $options ['notifyUrl'] : config('AliPay')['notifyUrl'];
- $this->alipayrsaPublicKey = isset ($options ['alipayrsaPublicKey']) ? $options ['alipayrsaPublicKey'] : config('AliPay')['alipayrsaPublicKey'];
- $this->returnUrl = isset ($options ['returnUrl']) ? $options ['returnUrl'] : config('AliPay')['returnUrl'];
- }
- /**
- * 支付宝app支付
- * @param string $data 业务参数 body subject out_trade_no total_amount
- * @return $response 返回app所需字符串
- */
- public function AliPayApp($data) {
- if(empty($this->appId))
- return false;
- $aliPayPath = './Plugins/AliPay/alipay-sdk/';
- require_once($aliPayPath . "aop/AopClient.php");
- require_once($aliPayPath . 'aop/request/AlipayTradeAppPayRequest.php');
- $aop = new \AopClient;
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
- $aop->appId = $this->appId;
- $aop->rsaPrivateKey = $this->rsaPrivateKey;
- $aop->format = "json";
- $aop->charset = "UTF-8";
- $aop->signType = $this->signType;
- $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
- $request = new \AlipayTradeAppPayRequest();
- //SDK已经封装掉了公共参数,这里只需要传入业务参数
- $bizcontent = json_encode([
- 'body' => $data['body'],
- 'subject' => $data['subject'],
- 'out_trade_no' => $data['out_trade_no'],//此订单号为商户唯一订单号
- 'total_amount' => $data['total_amount'],//保留两位小数
- // 'timeout_express' => '30m',
- 'time_expire' => date('Y-m-d H:i', time() + 180),
- 'passback_params' => urlencode($data['pass_back_params']), //区分安卓ios支付
- 'product_code' => 'QUICK_MSECURITY_PAY'
- ]);
- $request->setNotifyUrl($this->notifyUrl);
- $request->setBizContent($bizcontent);
- //这里和普通的接口调用不同,使用的是sdkExecute
- $response = $aop->sdkExecute($request);
- //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
- //return htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
- //返回app所需字符串
- return $response;
- }
- /**
- * 支付宝web支付
- * @param string $data 业务参数
- * @return $result 返回form表单,插入到当前网页即跳转到支付宝
- */
- public function AliPayWeb($data) {
- if(empty($this->appId))
- return false;
- $aliPayPath = './Plugins/AliPay/alipay-sdk/';
- require_once($aliPayPath . "aop/AopClient.php");
- require_once($aliPayPath . 'aop/request/AlipayTradePagePayRequest.php');
- $aop = new \AopClient();
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
- $aop->appId = $this->appId;
- $aop->rsaPrivateKey = $this->rsaPrivateKey;
- $aop->signType = $this->signType;
- $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
- $aop->apiVersion = '1.0';
- $aop->postCharset = 'UTF-8';
- $aop->format = 'json';
- $request = new \AlipayTradePagePayRequest();
- $bizcontent = json_encode([
- 'body' => $data['body'],
- 'subject' => $data['subject'],
- 'out_trade_no' => $data['out_trade_no'],//此订单号为商户唯一订单号
- 'total_amount' => $data['total_amount'],//保留两位小数
- 'product_code' => 'FAST_INSTANT_TRADE_PAY'
- ]);
- $request->setNotifyUrl($this->notifyUrl);
- $request->setReturnUrl($this->returnUrl);
- $request->setBizContent($bizcontent);
- $result = $aop->pageExecute($request);
- //返回form提交表单
- return $result;
- }
- /**
- * 支付宝支付回调签名验证
- * @param string $data 业务参数
- * @return bool
- */
- public function AliPayNotifyCheck() {
- $aliPayPath = './Plugins/AliPay/alipay-sdk/';
- require_once($aliPayPath . "aop/AopClient.php");
- $aop = new \AopClient;
- $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
- //此处验签方式必须与下单时的签名方式一致
- $flag = $aop->rsaCheckV1($_POST, NULL, $this->signType);
- return $flag;
- }
-
- /**
- * 支付宝提现转账 需要签约 单笔转账到支付宝账户接口
- * @param array $data 业务参数
- * @param $data['out_biz_no'] 订单号 必填
- * @param $data['amount'] 提现金额 必填
- * @param $data['payee_type'] 收款方账户类型(可取值:ALIPAY_USERID<支付宝用户号>,ALIPAY_LOGONID<支付宝账号>)
- * @param $data['payee_account'] 收款账号,根据payee_type填不同的值
- * @param $data['payee_real_name'] 收款支付宝账号真实姓名 最好填上 填上会验证账号是否正确
- * @param $data['payer_show_name'] 付款方姓名 可选
- * @param $data['remark'] 转账提现备注 可选
- * @return bool
- */
- public function AliPayTransfer($data) {
- if ($data['payee_type'] === 'ALIPAY_USERID') { //验证格式是否为用户号
- if(!preg_match('/^(2088){1}[0-9]{12}$/',$data['payee_account'])) {
- //echo "失败";
- return false;
- }
- } else {
- $data['payee_type'] = 'ALIPAY_LOGONID';
- }
- if (empty($this->appId))
- return false;
- $aliPayPath = './Plugins/AliPay/alipay-sdk/';
- require_once($aliPayPath . "aop/AopClient.php");
- require_once($aliPayPath . 'aop/request/AlipayFundTransToaccountTransferRequest.php');
- $aop = new \AopClient;
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
- $aop->appId = $this->appId;
- $aop->rsaPrivateKey = $this->rsaPrivateKey;
- $aop->format = "json";
- $aop->postCharset = "UTF-8";
- $aop->signType = $this->signType;
- $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
- $request = new \AlipayFundTransToaccountTransferRequest ();
- //SDK已经封装掉了公共参数,这里只需要传入业务参数
- $bizcontent = json_encode([
- 'out_biz_no' => $data['out_biz_no'],//此订单号为商户唯一订单号
- 'payee_type' => $data['payee_type'],
- 'payee_account' => $data['payee_account'],
- 'amount' => $data['amount'],
- 'payee_real_name' => $data['payee_real_name'],
- 'payer_show_name' => $data['payer_show_name'],
- 'remark' => $data['remark'],
- ]);
- $request->setBizContent($bizcontent);
- $result = $aop->execute($request);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $resultCode = $result->$responseNode->code;
- if(!empty($resultCode)&&$resultCode == 10000){
- //echo "成功"; 提现成功
- return true;
- } else {
- //echo "失败";
- return false;
- }
- }
- /**
- * 支付宝退款 支付原路返回
- * @param array $data 业务参数
- * string $data['out_trade_no'] 订单号 必填
- * string $data['trade_no'] 支付宝交易号 不能与$data['out_trade_no']同时为空
- * string $data['refund_reason'] 退款理由
- * string $data['refund_amount'] 退款金额
- * @return boolean
- */
- public function AliPayRefund($data) {
- if (empty($this->appId))
- return false;
- $aliPayPath = './Plugins/AliPay/alipay-sdk/';
- require_once($aliPayPath . "aop/AopClient.php");
- require_once($aliPayPath . 'aop/request/AlipayTradeRefundRequest.php');
- $aop = new \AopClient;
- $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
- $aop->appId = $this->appId;
- $aop->rsaPrivateKey = $this->rsaPrivateKey;
- $aop->apiVersion = '1.0';
- $aop->format = "json";
- $aop->postCharset = "UTF-8";
- $aop->signType = $this->signType;
- $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
- //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
- $request = new \AlipayTradeRefundRequest ();
- //SDK已经封装掉了公共参数,这里只需要传入业务参数
- $bizcontent = json_encode([
- 'out_trade_no' => $data['out_trade_no'], //订单号
- 'trade_no' => $data['trade_no'], //支付宝交易号
- 'refund_reason' => $data['refund_reason'],
- 'refund_amount' => $data['refund_amount'],
- ]);
- $request->setBizContent($bizcontent);
- $result = $aop->execute($request);
- $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
- $resultCode = $result->$responseNode->code;
- if(!empty($resultCode) && $resultCode == 10000){
- //echo "成功";
- return true;
- } else {
- //echo "失败";
- return false;
- }
- }
- }
|