AliPay.php 16 KB

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