Alipay.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php
  2. namespace app\common\library;
  3. /**
  4. * 支付宝插件 使用需 require_once("./Plugins/AliPay/AliPay.php");
  5. * @author Jack_YanTC <627495692@qq.com>
  6. */
  7. class AliPay {
  8. private $appId;
  9. private $rsaPrivateKey;
  10. private $signType;
  11. private $alipayrsaPublicKey;
  12. private $notifyUrl;
  13. private $returnUrl;
  14. /**
  15. * 初始化参数
  16. *
  17. * @param array $options
  18. * @param $options ['appId'] 应用ID,在支付宝上获取
  19. * @param $options ['rsaPrivateKey'] 应用密钥,与应用公钥一组,公钥填写到支付宝
  20. * @param $options ['signType'] 签名方式
  21. * @param $options ['alipayrsaPublicKey'] 支付宝公钥,在支付宝上获取
  22. * @param $options ['notifyUrl'] 支付宝回调地址
  23. * @param $options ['returnUrl'] 用于web支付返回地址
  24. */
  25. public function __construct($options = array()) {
  26. $this->appId = isset ($options ['appId']) ? $options ['appId'] : config('AliPay')['appId'];
  27. $this->rsaPrivateKey = isset ($options ['rsaPrivateKey']) ? $options ['rsaPrivateKey'] : config('AliPay')['rsaPrivateKey'];
  28. $this->signType = isset ($options ['signType']) ? $options ['signType'] : config('AliPay')['signType'];
  29. $this->notifyUrl = isset ($options ['notifyUrl']) ? $options ['notifyUrl'] : config('AliPay')['notifyUrl'];
  30. $this->alipayrsaPublicKey = isset ($options ['alipayrsaPublicKey']) ? $options ['alipayrsaPublicKey'] : config('AliPay')['alipayrsaPublicKey'];
  31. $this->returnUrl = isset ($options ['returnUrl']) ? $options ['returnUrl'] : config('AliPay')['returnUrl'];
  32. }
  33. /**
  34. * 支付宝app支付
  35. * @param string $data 业务参数 body subject out_trade_no total_amount
  36. * @return $response 返回app所需字符串
  37. */
  38. public function AliPayApp($data) {
  39. if(empty($this->appId))
  40. return false;
  41. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  42. require_once($aliPayPath . "aop/AopClient.php");
  43. require_once($aliPayPath . 'aop/request/AlipayTradeAppPayRequest.php');
  44. $aop = new \AopClient;
  45. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  46. $aop->appId = $this->appId;
  47. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  48. $aop->format = "json";
  49. $aop->charset = "UTF-8";
  50. $aop->signType = $this->signType;
  51. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  52. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  53. $request = new \AlipayTradeAppPayRequest();
  54. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  55. $bizcontent = json_encode([
  56. 'body' => $data['body'],
  57. 'subject' => $data['subject'],
  58. 'out_trade_no' => $data['out_trade_no'],//此订单号为商户唯一订单号
  59. 'total_amount' => $data['total_amount'],//保留两位小数
  60. // 'timeout_express' => '30m',
  61. 'time_expire' => date('Y-m-d H:i', time() + 180),
  62. 'passback_params' => urlencode($data['pass_back_params']), //区分安卓ios支付
  63. 'product_code' => 'QUICK_MSECURITY_PAY'
  64. ]);
  65. $request->setNotifyUrl($this->notifyUrl);
  66. $request->setBizContent($bizcontent);
  67. //这里和普通的接口调用不同,使用的是sdkExecute
  68. $response = $aop->sdkExecute($request);
  69. //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
  70. //return htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
  71. //返回app所需字符串
  72. return $response;
  73. }
  74. /**
  75. * 支付宝web支付
  76. * @param string $data 业务参数
  77. * @return $result 返回form表单,插入到当前网页即跳转到支付宝
  78. */
  79. public function AliPayWeb($data) {
  80. if(empty($this->appId))
  81. return false;
  82. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  83. require_once($aliPayPath . "aop/AopClient.php");
  84. require_once($aliPayPath . 'aop/request/AlipayTradePagePayRequest.php');
  85. $aop = new \AopClient();
  86. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  87. $aop->appId = $this->appId;
  88. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  89. $aop->signType = $this->signType;
  90. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  91. $aop->apiVersion = '1.0';
  92. $aop->postCharset = 'UTF-8';
  93. $aop->format = 'json';
  94. $request = new \AlipayTradePagePayRequest();
  95. $bizcontent = json_encode([
  96. 'body' => $data['body'],
  97. 'subject' => $data['subject'],
  98. 'out_trade_no' => $data['out_trade_no'],//此订单号为商户唯一订单号
  99. 'total_amount' => $data['total_amount'],//保留两位小数
  100. 'product_code' => 'FAST_INSTANT_TRADE_PAY'
  101. ]);
  102. $request->setNotifyUrl($this->notifyUrl);
  103. $request->setReturnUrl($this->returnUrl);
  104. $request->setBizContent($bizcontent);
  105. $result = $aop->pageExecute($request);
  106. //返回form提交表单
  107. return $result;
  108. }
  109. /**
  110. * 支付宝支付回调签名验证
  111. * @param string $data 业务参数
  112. * @return bool
  113. */
  114. public function AliPayNotifyCheck() {
  115. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  116. require_once($aliPayPath . "aop/AopClient.php");
  117. $aop = new \AopClient;
  118. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  119. //此处验签方式必须与下单时的签名方式一致
  120. $flag = $aop->rsaCheckV1($_POST, NULL, $this->signType);
  121. return $flag;
  122. }
  123. /**
  124. * 支付宝提现转账 需要签约 单笔转账到支付宝账户接口
  125. * @param array $data 业务参数
  126. * @param $data['out_biz_no'] 订单号 必填
  127. * @param $data['amount'] 提现金额 必填
  128. * @param $data['payee_type'] 收款方账户类型(可取值:ALIPAY_USERID<支付宝用户号>,ALIPAY_LOGONID<支付宝账号>)
  129. * @param $data['payee_account'] 收款账号,根据payee_type填不同的值
  130. * @param $data['payee_real_name'] 收款支付宝账号真实姓名 最好填上 填上会验证账号是否正确
  131. * @param $data['payer_show_name'] 付款方姓名 可选
  132. * @param $data['remark'] 转账提现备注 可选
  133. * @return bool
  134. */
  135. public function AliPayTransfer($data) {
  136. if ($data['payee_type'] === 'ALIPAY_USERID') { //验证格式是否为用户号
  137. if(!preg_match('/^(2088){1}[0-9]{12}$/',$data['payee_account'])) {
  138. //echo "失败";
  139. return false;
  140. }
  141. } else {
  142. $data['payee_type'] = 'ALIPAY_LOGONID';
  143. }
  144. if (empty($this->appId))
  145. return false;
  146. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  147. require_once($aliPayPath . "aop/AopClient.php");
  148. require_once($aliPayPath . 'aop/request/AlipayFundTransToaccountTransferRequest.php');
  149. $aop = new \AopClient;
  150. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  151. $aop->appId = $this->appId;
  152. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  153. $aop->format = "json";
  154. $aop->postCharset = "UTF-8";
  155. $aop->signType = $this->signType;
  156. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  157. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  158. $request = new \AlipayFundTransToaccountTransferRequest ();
  159. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  160. $bizcontent = json_encode([
  161. 'out_biz_no' => $data['out_biz_no'],//此订单号为商户唯一订单号
  162. 'payee_type' => $data['payee_type'],
  163. 'payee_account' => $data['payee_account'],
  164. 'amount' => $data['amount'],
  165. 'payee_real_name' => $data['payee_real_name'],
  166. 'payer_show_name' => $data['payer_show_name'],
  167. 'remark' => $data['remark'],
  168. ]);
  169. $request->setBizContent($bizcontent);
  170. $result = $aop->execute($request);
  171. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  172. $resultCode = $result->$responseNode->code;
  173. if(!empty($resultCode)&&$resultCode == 10000){
  174. //echo "成功"; 提现成功
  175. return true;
  176. } else {
  177. //echo "失败";
  178. return false;
  179. }
  180. }
  181. /**
  182. * 支付宝退款 支付原路返回
  183. * @param array $data 业务参数
  184. * string $data['out_trade_no'] 订单号 必填
  185. * string $data['trade_no'] 支付宝交易号 不能与$data['out_trade_no']同时为空
  186. * string $data['refund_reason'] 退款理由
  187. * string $data['refund_amount'] 退款金额
  188. * @return boolean
  189. */
  190. public function AliPayRefund($data) {
  191. if (empty($this->appId))
  192. return false;
  193. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  194. require_once($aliPayPath . "aop/AopClient.php");
  195. require_once($aliPayPath . 'aop/request/AlipayTradeRefundRequest.php');
  196. $aop = new \AopClient;
  197. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  198. $aop->appId = $this->appId;
  199. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  200. $aop->apiVersion = '1.0';
  201. $aop->format = "json";
  202. $aop->postCharset = "UTF-8";
  203. $aop->signType = $this->signType;
  204. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  205. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  206. $request = new \AlipayTradeRefundRequest ();
  207. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  208. $bizcontent = json_encode([
  209. 'out_trade_no' => $data['out_trade_no'], //订单号
  210. 'trade_no' => $data['trade_no'], //支付宝交易号
  211. 'refund_reason' => $data['refund_reason'],
  212. 'refund_amount' => $data['refund_amount'],
  213. ]);
  214. $request->setBizContent($bizcontent);
  215. $result = $aop->execute($request);
  216. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  217. $resultCode = $result->$responseNode->code;
  218. if(!empty($resultCode) && $resultCode == 10000){
  219. //echo "成功";
  220. return true;
  221. } else {
  222. //echo "失败";
  223. return false;
  224. }
  225. }
  226. }