AliPay.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. <?php
  2. /**
  3. * 支付宝插件 使用方法请查看同文件夹下的demo
  4. * 目前已经支持电脑网站支付,手机APP支付,支付回调校验,用户提现等功能,如需拓展请联系作者
  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. * C('ALI_PAY') Api Config中的配置
  18. * @param array $options
  19. * @param $options ['appId'] 应用ID,在支付宝上获取
  20. * @param $options ['rsaPrivateKey'] 应用密钥,与应用公钥一组,公钥填写到支付宝
  21. * @param $options ['signType'] 签名方式
  22. * @param $options ['alipayrsaPublicKey'] 支付宝公钥,在支付宝上获取
  23. * @param $options ['notifyUrl'] 支付宝回调地址
  24. * @param $options ['returnUrl'] 用于web支付返回地址
  25. */
  26. public function __construct($options = null) {
  27. $epayConfig = get_addon_config('epay');
  28. $alipayConfig = isset($epayConfig['alipay']) ? $epayConfig['alipay'] : [];
  29. $this->appId = isset ($options['appId']) ? $options['appId'] : $alipayConfig['app_id'];
  30. $this->rsaPrivateKey = isset ($options['private_key_path']) ? $options['private_key_path'] : $alipayConfig['private_key'];
  31. $this->signType = isset ($options['sign_type']) ? $options['sign_type'] : 'RSA2';
  32. $this->notifyUrl = isset ($options['notify_url']) ? $options['notify_url'] : $alipayConfig['notify_url'];
  33. $this->alipayrsaPublicKey = isset ($options['ali_public_key_path']) ? $options['ali_public_key_path'] : $alipayConfig['ali_public_key'];
  34. // $this->returnUrl = isset ($options ['returnUrl']) ? $options ['returnUrl'] : C('ALI_PAY')['returnUrl'];
  35. }
  36. /**
  37. * 支付宝app支付 需要签约 APP支付
  38. * @param string $data 业务参数 body subject out_trade_no total_amount
  39. * @param string $data['out_trade_no'] 订单号 必填
  40. * @param string $data['total_amount'] 订单金额 必填
  41. * @param string $data['subject'] 订单标题 必填
  42. * @param string $data['body'] 订单详情 可选
  43. * @return $response 返回app所需字符串
  44. */
  45. public function AliPayApp($data) {
  46. if (empty($this->appId))
  47. return false;
  48. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  49. require_once($aliPayPath . "aop/AopClient.php");
  50. require_once($aliPayPath . 'aop/request/AlipayTradeAppPayRequest.php');
  51. $aop = new \AopClient;
  52. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  53. $aop->appId = $this->appId;
  54. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  55. $aop->format = "json";
  56. $aop->charset = "UTF-8";
  57. $aop->signType = $this->signType;
  58. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  59. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  60. $request = new \AlipayTradeAppPayRequest();
  61. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  62. $bizcontent = json_encode([
  63. 'body' => $data['body'],
  64. 'subject' => $data['subject'],
  65. 'out_trade_no' => $data['out_trade_no'],//此订单号为商户唯一订单号
  66. 'total_amount' => $data['total_amount'],//保留两位小数
  67. 'timeout_express' => '30m',
  68. 'passback_params' => urlencode($data['pass_back_params']), //区分安卓ios支付
  69. 'product_code' => 'QUICK_MSECURITY_PAY'
  70. ]);
  71. $request->setNotifyUrl($this->notifyUrl);
  72. $request->setBizContent($bizcontent);
  73. //这里和普通的接口调用不同,使用的是sdkExecute
  74. $response = $aop->sdkExecute($request);
  75. //htmlspecialchars是为了输出到页面时防止被浏览器将关键参数html转义,实际打印到日志以及http传输不会有这个问题
  76. //return htmlspecialchars($response);//就是orderString 可以直接给客户端请求,无需再做处理。
  77. //返回app所需字符串
  78. return $response;
  79. }
  80. /**
  81. * 支付宝web支付 需要签约 电脑网站支付
  82. * @param string $data 业务参数
  83. * @param string $data['out_trade_no'] 订单号 必填
  84. * @param string $data['total_amount'] 订单金额 必填
  85. * @param string $data['subject'] 订单标题 必填
  86. * @param string $data['body'] 订单详情 可选
  87. * @return $result 返回form表单,插入到当前网页即跳转到支付宝付款界面
  88. */
  89. public function AliPayWeb($data) {
  90. if (empty($this->appId))
  91. return false;
  92. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  93. require_once($aliPayPath . "aop/AopClient.php");
  94. require_once($aliPayPath . 'aop/request/AlipayTradePagePayRequest.php');
  95. $aop = new \AopClient();
  96. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  97. $aop->appId = $this->appId;
  98. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  99. $aop->signType = $this->signType;
  100. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  101. $aop->apiVersion = '1.0';
  102. $aop->postCharset = 'UTF-8';
  103. $aop->format = 'json';
  104. $request = new \AlipayTradePagePayRequest();
  105. $bizcontent = json_encode([
  106. 'body' => $data['body'],
  107. 'subject' => $data['subject'],
  108. 'out_trade_no' => $data['out_trade_no'],//此订单号为商户唯一订单号
  109. 'total_amount' => $data['total_amount'],//保留两位小数
  110. 'product_code' => 'FAST_INSTANT_TRADE_PAY'
  111. ]);
  112. $request->setNotifyUrl($this->notifyUrl);
  113. $request->setReturnUrl($this->returnUrl);
  114. $request->setBizContent($bizcontent);
  115. $result = $aop->pageExecute($request);
  116. //返回form提交表单
  117. return $result;
  118. }
  119. /**
  120. * 支付宝MobileWeb支付 需要签约 手机网站支付
  121. * @param string $data 业务参数
  122. * @param string $data['out_trade_no'] 订单号 必填
  123. * @param string $data['total_amount'] 订单金额 必填
  124. * @param string $data['subject'] 订单标题 必填
  125. * @param string $data['body'] 订单详情 可选
  126. * @return $result 返回form表单,插入到当前网页即跳转到支付宝付款界面
  127. */
  128. public function AliPayMobileWeb($data) {
  129. if (empty($this->appId))
  130. return false;
  131. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  132. require_once($aliPayPath . "aop/AopClient.php");
  133. require_once($aliPayPath . 'aop/request/AlipayTradeWapPayRequest.php');
  134. $aop = new \AopClient();
  135. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  136. $aop->appId = $this->appId;
  137. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  138. $aop->signType = $this->signType;
  139. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  140. $aop->apiVersion = '1.0';
  141. $aop->postCharset = 'UTF-8';
  142. $aop->format = 'json';
  143. $request = new \AlipayTradeWapPayRequest ();
  144. $bizcontent = json_encode([
  145. 'body' => $data['body'],
  146. 'subject' => $data['subject'],
  147. 'out_trade_no' => $data['out_trade_no'],//此订单号为商户唯一订单号
  148. 'total_amount' => $data['total_amount'],//保留两位小数
  149. 'timeout_express' => '90m',
  150. 'product_code' => 'QUICK_WAP_WAY'
  151. ]);
  152. $request->setNotifyUrl($this->notifyUrl);
  153. $request->setReturnUrl($this->returnUrl);
  154. $request->setBizContent($bizcontent);
  155. $result = $aop->pageExecute($request);
  156. //返回form提交表单
  157. return $result;
  158. }
  159. /**
  160. * 支付宝支付回调签名验证
  161. * @param string $data 业务参数
  162. * @return bool
  163. */
  164. public function AliPayNotifyCheck() {
  165. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  166. require_once($aliPayPath . "aop/AopClient.php");
  167. $aop = new \AopClient;
  168. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  169. //此处验签方式必须与下单时的签名方式一致
  170. $flag = $aop->rsaCheckV1($_POST, NULL, $this->signType);
  171. return $flag;
  172. }
  173. /**
  174. * 支付宝提现转账 需要签约 单笔转账到支付宝账户接口
  175. * @param string $data 业务参数
  176. * @param $data['out_biz_no'] 订单号 必填
  177. * @param $data['amount'] 提现金额 必填 金额不小于0.1元,单日转出累计额度为100万元,转账给个人支付宝账户,单笔最高5万元;转账给企业支付宝账户,单笔最高10万元。
  178. * @param $data['payee_account'] 收款支付宝账号 必填
  179. * @param $data['payee_real_name'] 收款支付宝账号真实姓名 最好填上 填上会验证账号是否正确
  180. * @param $data['payer_show_name'] 付款方姓名 可选
  181. * @param $data['remark'] 转账提现备注 可选
  182. * @return bool
  183. */
  184. public function AliPayWithdrawOld($data) {
  185. if (empty($this->appId))
  186. return false;
  187. if ($data['amount']<0.1)
  188. return false;
  189. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  190. require_once($aliPayPath . "aop/AopClient.php");
  191. require_once($aliPayPath . 'aop/request/AlipayFundTransToaccountTransferRequest.php');
  192. $aop = new \AopClient;
  193. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  194. $aop->appId = $this->appId;
  195. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  196. $aop->format = "json";
  197. $aop->charset = "UTF-8";
  198. $aop->signType = $this->signType;
  199. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  200. //实例化具体API对应的request类,类名称和接口名称对应,当前调用接口名称:alipay.trade.app.pay
  201. $request = new \AlipayFundTransToaccountTransferRequest ();
  202. //SDK已经封装掉了公共参数,这里只需要传入业务参数
  203. $bizcontent = json_encode([
  204. 'out_biz_no' => $data['out_biz_no'],//此订单号为商户唯一订单号
  205. 'payee_type' => 'ALIPAY_LOGONID',//默认登录账号,后期可拓展
  206. 'payee_account' => $data['payee_account'],
  207. 'amount' => $data['amount'],
  208. 'payee_real_name' => $data['payee_real_name'],
  209. 'payer_show_name' => $data['payer_show_name'],
  210. 'remark' => $data['remark'],
  211. ]);
  212. $request->setBizContent($bizcontent);
  213. $result = $aop->execute($request);
  214. $responseNode = str_replace(".", "_", $request->getApiMethodName()) . "_response";
  215. $resultCode = $result->$responseNode->code;
  216. if(!empty($resultCode)&&$resultCode == 10000){
  217. //echo "成功"; 提现成功
  218. return true;
  219. } else {
  220. //echo "失败";
  221. return false;
  222. }
  223. }
  224. /**
  225. * 支付宝提现转账 需要签约 单笔转账到支付宝账户接口(新版2022-11-21)
  226. * @return bool
  227. */
  228. public function AliPayWithdraw($data) {
  229. if (empty($this->appId))
  230. return false;
  231. if ($data['trans_amount'] < 0.1)
  232. return false;
  233. $aliPayPath = '../extend/AliPay/aop/';
  234. require_once $aliPayPath . 'AopClient.php';
  235. require_once $aliPayPath . 'AopCertClient.php';
  236. require_once $aliPayPath . 'AopCertification.php';
  237. require_once $aliPayPath . 'AlipayConfig.php';
  238. require_once $aliPayPath . 'request/AlipayFundTransUniTransferRequest.php';
  239. $privateKey = $this->rsaPrivateKey;//"<-- 请填写您的应用私钥,例如:MIIEvQIBADANB ... ... -->";
  240. $alipayConfig = new AlipayConfig();
  241. $alipayConfig->setPrivateKey($privateKey);
  242. $alipayConfig->setServerUrl("https://openapi.alipay.com/gateway.do");
  243. $alipayConfig->setAppId($this->appId); //"<-- 请填写您的AppId,例如:2019091767145019 -->"
  244. $alipayConfig->setCharset("UTF-8");
  245. $alipayConfig->setSignType("RSA2");
  246. $alipayConfig->setEncryptKey("");
  247. $alipayConfig->setFormat("json");
  248. $alipayConfig->setAppCertPath('../addons/epay/certs/appCertPublicKey.crt');//"<-- 请填写您的应用公钥证书文件路径,例如:/foo/appCertPublicKey_2019051064521003.crt -->"
  249. $alipayConfig->setAlipayPublicCertPath('../addons/epay/certs/alipayCertPublicKey.crt');//"<-- 请填写您的支付宝公钥证书文件路径,例如:/foo/alipayCertPublicKey_RSA2.crt -->"
  250. $alipayConfig->setRootCertPath('../addons/epay/certs/alipayRootCert.crt');//"<-- 请填写您的支付宝根证书文件路径,例如:/foo/alipayRootCert.crt -->"
  251. $alipayClient = new AopCertClient($alipayConfig);
  252. $alipayClient->isCheckAlipayPublicCert = true;
  253. $request = new AlipayFundTransUniTransferRequest();
  254. $data = json_encode($data);
  255. $request->setBizContent($data);
  256. $responseResult = $alipayClient->execute($request);
  257. $responseApiName = str_replace(".","_",$request->getApiMethodName())."_response";
  258. $responsecode = $responseResult->$responseApiName->code;
  259. $result = [
  260. 'code' => $responsecode,
  261. 'sub_msg' => isset($responseResult->$responseApiName->sub_msg) ? $responseResult->$responseApiName->sub_msg : '',
  262. ];
  263. return $result;
  264. /*if(!empty($responsecode)&&$responsecode==10000){
  265. // echo("调用成功");
  266. return $responseResult->$responseApiName;
  267. } else{
  268. // echo("调用失败");
  269. return $responseResult->$responseApiName;
  270. }*/
  271. }
  272. /**
  273. * 支付宝身份认证初始化服务
  274. */
  275. public function userCertifyInitialize($data) {
  276. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  277. require_once($aliPayPath . "aop/AopClient.php");
  278. require_once($aliPayPath . 'aop/request/AlipayUserCertifyOpenInitializeRequest.php');
  279. $aop = new \AopClient;
  280. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  281. $aop->appId = $this->appId;
  282. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  283. $aop->format = "json";
  284. $aop->postCharset = "UTF-8";
  285. $aop->signType = $this->signType;
  286. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  287. $request = new \AlipayUserCertifyOpenInitializeRequest();
  288. $request->setBizContent(json_encode($data));
  289. $result = $aop->execute ($request);
  290. $result = json_decode(json_encode($result), true);
  291. return $result['alipay_user_certify_open_initialize_response'];
  292. }
  293. /**
  294. * 支付宝身份认证开始认证
  295. */
  296. public function userCertify($data) {
  297. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  298. require_once($aliPayPath . "aop/AopClient.php");
  299. require_once($aliPayPath . 'aop/request/AlipayUserCertifyOpenCertifyRequest.php');
  300. $aop = new \AopClient;
  301. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  302. $aop->appId = $this->appId;
  303. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  304. $aop->format = "json";
  305. $aop->postCharset = "UTF-8";
  306. $aop->signType = $this->signType;
  307. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  308. $request = new \AlipayUserCertifyOpenCertifyRequest();
  309. $request->setBizContent(json_encode($data));
  310. $result = $aop->pageExecute ($request, 'GET');
  311. return $result;
  312. // $result = json_decode($result, true); p($result);die;
  313. // return $result['alipay_user_certify_open_certify_response'];
  314. }
  315. /**
  316. * 支付宝身份认证状态查询
  317. */
  318. public function userCertifyState($data) {
  319. $aliPayPath = './Plugins/AliPay/alipay-sdk/';
  320. require_once($aliPayPath . "aop/AopClient.php");
  321. require_once($aliPayPath . 'aop/request/AlipayUserCertifyOpenQueryRequest.php');
  322. $aop = new \AopClient;
  323. $aop->gatewayUrl = "https://openapi.alipay.com/gateway.do";
  324. $aop->appId = $this->appId;
  325. $aop->rsaPrivateKey = $this->rsaPrivateKey;
  326. $aop->format = "json";
  327. $aop->postCharset = "UTF-8";
  328. $aop->signType = $this->signType;
  329. $aop->alipayrsaPublicKey = $this->alipayrsaPublicKey;
  330. $request = new \AlipayUserCertifyOpenQueryRequest();
  331. $request->setBizContent(json_encode($data));
  332. $result = $aop->execute ($request);
  333. $result = json_decode(json_encode($result), true);
  334. return $result['alipay_user_certify_open_query_response'];
  335. }
  336. }