Alipay.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <?php
  2. /**
  3. * Alipay.php UTF-8
  4. * 支付宝支付
  5. *
  6. * @date : 2018/2/8 11:55
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : wuyonghong <wyh@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace huolib\pay\driver;
  13. use huolib\constant\PaywayConst;
  14. use huolib\pay\Driver;
  15. use huolib\status\OrderStatus;
  16. use think\Exception;
  17. use think\Loader;
  18. use think\Log;
  19. class Alipay extends Driver {
  20. private $config
  21. = [
  22. 'partner' => '',
  23. 'seller_email' => '',
  24. 'key' => '',
  25. 'sign_type' => '',
  26. 'input_charset' => 'utf-8', // 编码 (固定值不用改)
  27. 'transport' => 'http', // 协议 (固定值不用改)
  28. 'cacert' => '',
  29. 'notify_url' => '',
  30. 'return_url' => '',
  31. 'show_url' => '',
  32. 'private_key_path' => '',
  33. 'public_key_path' => '',
  34. ];
  35. /**
  36. * 构造函数
  37. */
  38. public function __construct() {
  39. if (file_exists(GLOBAL_CONF_PATH."extra/pay/alipay/config.php")) {
  40. $_config = include GLOBAL_CONF_PATH."extra/pay/alipay/config.php";
  41. } else {
  42. $_config = array();
  43. }
  44. $this->config = array_merge($this->config, $_config);
  45. $this->config['sign_type'] = strtoupper(trim('RSA'));
  46. $this->config['cacert'] = GLOBAL_CONF_PATH.'extra/pay/alipay/cacert.pem';
  47. $this->config['private_key_path'] = GLOBAL_CONF_PATH.'extra/pay/alipay/key/rsa_private_key.pem';
  48. $this->config['public_key_path'] = GLOBAL_CONF_PATH.'extra/pay/alipay/key/alipay_public_key.pem';
  49. $this->notify_url = get_val($this->config, 'notify_url', '');
  50. }
  51. /**
  52. * 设置配置
  53. *
  54. * @param array $config
  55. */
  56. public function setConf($config = []) {
  57. if (!empty($config)) {
  58. $this->config['partner'] = $config['partner'];
  59. $this->config['seller_email'] = $config['seller_email'];
  60. $this->config['key'] = $config['key'];
  61. $this->config['sign_type'] = $config['sign_type'];
  62. $this->config['private_key_path'] = $config['private_key_path'];
  63. $this->config['public_key_path'] = $config['public_key_path'];
  64. }
  65. }
  66. /**
  67. * 移动APP支付函数
  68. *
  69. * @param array $config
  70. *
  71. * @return bool| array
  72. * @throws Exception
  73. */
  74. public function clientPay($config = []) {
  75. $this->setConf($config);
  76. $_config = $this->config;
  77. if (empty($_config['key'])) {
  78. Log::write(
  79. "func=".__FUNCTION__."&class=".__CLASS__."&config=".$_config."&code=".OrderStatus::ALIPAY_CONFIG_ERROR
  80. ."&msg=".OrderStatus::getMsg(OrderStatus::ALIPAY_CONFIG_ERROR), LOG::ERROR
  81. );
  82. throw new Exception(
  83. OrderStatus::getMsg(OrderStatus::ALIPAY_CONFIG_ERROR), OrderStatus::ALIPAY_CONFIG_ERROR
  84. );
  85. }
  86. $_config['notify_url'] = $this->notify_url;
  87. $_config['return_url'] = $this->return_url;
  88. $_config['show_url'] = $this->show_url;
  89. $_data = array(
  90. "service" => "mobile.securitypay.pay",
  91. "partner" => trim($this->config['partner']),
  92. "_input_charset" => trim(strtolower($this->config['input_charset'])),
  93. "sign_type" => strtoupper(trim($this->config['sign_type'])),
  94. "notify_url" => $this->notify_url,
  95. "out_trade_no" => $this->order_id,
  96. "subject" => $this->product_name,
  97. "body" => $this->product_id,
  98. "payment_type" => "1",
  99. "seller_id" => trim($this->config['seller_email']),
  100. "total_fee" => $this->real_amount,
  101. "it_b_pay" => "30m"
  102. );
  103. Loader::import('pay.alipay.AlipaySubmit', '', '.class.php');
  104. // 建立请求,请求成功之后,会通知服务器的alipay_notify方法,客户端会通知$return_url配置的方法
  105. $_alipay_submit = new \AlipaySubmit($this->config);
  106. $_token = $_alipay_submit->buildClientRequestParaToString($_data);
  107. return $this->clientAjax(PaywayConst::PAYWAY_ALIPAY, $_token, 1, 2, 1);
  108. }
  109. /**
  110. * wap端下单
  111. *
  112. * @param array $config
  113. *
  114. * @return bool
  115. * @throws Exception
  116. */
  117. public function mobilePay($config = []) {
  118. $this->setConf($config);
  119. $_config = $this->config;
  120. if (empty($_config['key'])) {
  121. Log::write(
  122. "func=".__FUNCTION__."&class=".__CLASS__."&config=".$_config."&code=".OrderStatus::ALIPAY_CONFIG_ERROR
  123. ."&msg=".OrderStatus::getMsg(OrderStatus::ALIPAY_CONFIG_ERROR), LOG::ERROR
  124. );
  125. throw new Exception(
  126. OrderStatus::getMsg(OrderStatus::ALIPAY_CONFIG_ERROR), OrderStatus::ALIPAY_CONFIG_ERROR
  127. );
  128. }
  129. $_config['notify_url'] = $this->notify_url;
  130. $_config['return_url'] = $this->return_url;
  131. $_config['show_url'] = $this->show_url;
  132. $_data = array(
  133. "service" => "alipay.wap.create.direct.pay.by.user",
  134. "partner" => trim($_config['partner']),
  135. "_input_charset" => trim(strtolower($_config['input_charset'])),
  136. "sign_type" => strtoupper(trim($_config['sign_type'])),
  137. "notify_url" => $this->notify_url,
  138. "return_url" => $this->return_url,
  139. "show_url" => $this->show_url,
  140. "out_trade_no" => $this->order_id,
  141. "subject" => $this->product_name,
  142. "body" => $this->product_id,
  143. "payment_type" => "1",
  144. "seller_id" => trim($this->config['seller_email']),
  145. "total_fee" => $this->real_amount,
  146. "app_pay" => 'Y',
  147. "it_b_pay" => "30m"
  148. );
  149. Loader::import('pay.alipay.AlipaySubmit', '', '.class.php');
  150. // 建立请求,请求成功之后,会通知服务器的alipay_notify方法,客户端会通知$return_url配置的方法
  151. $_alipay_submit = new \AlipaySubmit($this->config);
  152. $_html_text = $_alipay_submit->buildRequestForm($_data, "get", "跳转中");
  153. header("Content-type:text/html;charset=utf-8");
  154. /* Modified by ouzhongfu BEGIN 2019/2/16 ISSUES:8103 支付宝调起方式修改 */
  155. $_pay_type = PaywayConst::PAYWAY_ALIPAYH5;
  156. $_btoken = '';
  157. if ($this->getUseProtocol()) {
  158. $_btoken = $_html_text;
  159. $_html = $_alipay_submit->alipay_gateway_new.$_alipay_submit->buildRequestParaToString($_data);
  160. $_html_result = $this->getAlipayUrl($_html);
  161. $_device_type = request()->getDeviceOsType();
  162. if ('ios' == $_device_type) {
  163. $_pay_type = PaywayConst::PAYWAY_ALIPAYXY;
  164. $_html_text = $_html_result['deeplink_ios'];
  165. } else {
  166. $_pay_type = PaywayConst::PAYWAY_ALIPAYXY;
  167. $_html_text = $_html_result['deeplink_android'];
  168. }
  169. }
  170. /* END 2019/2/16 ISSUES:8103 */
  171. return $this->clientAjax($_pay_type, $_html_text, 1, 1, 1, $_btoken);
  172. }
  173. /**
  174. * PC端下单
  175. *
  176. * @param array $config
  177. *
  178. * @return bool
  179. * @throws Exception
  180. */
  181. public function pcPay($config = []) {
  182. $this->setConf($config);
  183. $_config = $this->config;
  184. if (empty($_config['key'])) {
  185. Log::write(
  186. "func=".__FUNCTION__."&class=".__CLASS__."&config=".$_config."&code=".OrderStatus::ALIPAY_CONFIG_ERROR
  187. ."&msg=".OrderStatus::getMsg(OrderStatus::ALIPAY_CONFIG_ERROR), LOG::ERROR
  188. );
  189. throw new Exception(
  190. OrderStatus::getMsg(OrderStatus::ALIPAY_CONFIG_ERROR), OrderStatus::ALIPAY_CONFIG_ERROR
  191. );
  192. }
  193. $_config['notify_url'] = $this->notify_url;
  194. $_config['return_url'] = $this->return_url;
  195. $_config['show_url'] = $this->show_url;
  196. $_data = array(
  197. "service" => "create_direct_pay_by_user",
  198. "partner" => trim($_config['partner']),
  199. "seller_id" => trim($_config['partner']),
  200. "payment_type" => "1",
  201. "notify_url" => $this->notify_url,
  202. "return_url" => $this->return_url,
  203. "out_trade_no" => $this->order_id,
  204. "subject" => $this->product_name,
  205. "total_fee" => $this->real_amount,
  206. "show_url" => $this->show_url,
  207. "body" => $this->product_id,
  208. "it_b_pay" => "15d",
  209. "_input_charset" => trim(strtolower($_config['input_charset']))
  210. );
  211. Loader::import('pay.alipay.AlipaySubmit', '', '.class.php');
  212. //建立请求
  213. $_alipay_submit = new \AlipaySubmit($_config);
  214. $_html_text = $_alipay_submit->buildRequestForm($_data, "get", "跳转中");
  215. return $this->clientAjax(PaywayConst::PAYWAY_ALIPAYQR, $_html_text, 1, 1, 1);
  216. }
  217. /**
  218. * 异步回调函数
  219. *
  220. * @param array $config
  221. */
  222. public function notifyUrl($config = []) {
  223. $this->setConf($config);
  224. // 引入支付宝
  225. Loader::import('pay.alipay.AlipayNotify', '', '.class.php');
  226. $_ali_notify = new \AlipayNotify($this->config);
  227. // 验证支付数据
  228. $_verify_result = $_ali_notify->verifyNotify();
  229. if ($_verify_result) {
  230. /* 平台订单号 */
  231. $_out_trade_no = $_POST['out_trade_no'];
  232. /* 支付宝交易号 */
  233. $_trade_no = $_POST['trade_no'];
  234. /* 交易金额 */
  235. $_amount = $_POST['total_fee'];
  236. /* 该笔订单的备注、描述、明细等。对应请求时的body参数,原样通知回来。 */
  237. $_product_id = $_POST['body'];
  238. // 交易状态
  239. $trade_status = $_POST['trade_status'];
  240. if ($trade_status == 'TRADE_FINISHED') {
  241. } else if ($trade_status == 'TRADE_SUCCESS') {
  242. $_class = $this->order_class;
  243. $_func = $this->func;
  244. try {
  245. if (class_exists($_class)) {
  246. $_order = new $_class();
  247. $_order->$_func($_product_id, $_out_trade_no, $_trade_no, $_amount, 'alipay');
  248. } else {
  249. Log::write(
  250. "func=".__FUNCTION__."&class=".__CLASS__."&order_class=".$_class
  251. ."&func=".$_func, LOG::ERROR
  252. );
  253. }
  254. } catch (Exception $_e) {
  255. Log::write(
  256. "func=".__FUNCTION__."&class=".__CLASS__."&order_class=".$_class
  257. ."&func=".$_func."&err=".$_e->getMessage()."&errCode=".$_e->getCode(), LOG::ERROR
  258. );
  259. }
  260. }
  261. echo "success";
  262. // 下面写验证通过的逻辑 比如说更改订单状态等等 $_POST['out_trade_no'] 为订单号;
  263. } else {
  264. echo "fail";
  265. }
  266. }
  267. /**
  268. * 返回接收页面
  269. *
  270. * @param array $config
  271. *
  272. * @return bool
  273. */
  274. public function returnUrl($config = []) {
  275. $this->setConf($config);
  276. //计算得出通知验证结果
  277. // 引入支付宝
  278. Loader::import('pay.alipay.AlipayNotify', '', '.class.php');
  279. $alipayNotify = new \AlipayNotify($this->config);
  280. $verify_result = $alipayNotify->verifyReturn();
  281. if ($verify_result) { //验证成功
  282. $_status = 2;
  283. } else {
  284. $_status = 3;
  285. }
  286. return $this->clientAjax(PaywayConst::PAYWAY_ALIPAY, $_status, $_status, 1, 1);
  287. }
  288. /**
  289. * 查询订单
  290. *
  291. * @access public
  292. *
  293. * @param string $order_id 商户系统内部订单号
  294. * @param string $transaction_id 第三方支付的订单号
  295. * @param null $ext 扩展信息
  296. *
  297. * @return bool|string
  298. */
  299. public function orderQuery($order_id, $transaction_id, $ext = null) {
  300. return true;
  301. }
  302. public function getAlipayUrl($html) {
  303. $_headers = array('CLIENT-IP' => $this->ip, 'X-FORWARDED-FOR' => $this->ip);
  304. $_header_arr = array();
  305. foreach ($_headers as $_n => $_v) {
  306. $_header_arr[] = $_n.':'.$_v;
  307. }
  308. $_options = array(
  309. CURLOPT_REFERER => SDKSITE,
  310. CURLOPT_USERAGENT => 'Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1',
  311. CURLOPT_FOLLOWLOCATION => 1,
  312. CURLOPT_HTTPHEADER => $_header_arr
  313. );
  314. $_html = $this->singleGrabGetdata($html, $_options);
  315. $_p = '/{\"requestType.*?\"}/';
  316. preg_match($_p, $_html, $_match);
  317. $_result['deeplink_ios'] = '';
  318. $_result['deeplink_android'] = '';
  319. if (isset($_match[0])) {
  320. $_args = json_decode($_match[0], true);
  321. $_result['deeplink_ios'] = 'alipay://alipayclient/?'.urlencode(json_encode($_args));
  322. // $_result['deeplink_android'] = 'alipay://alipayclient/?'.urlencode($_args['dataString']);
  323. $_result['deeplink_android'] = 'alipays://platformapi/startApp?appId=20000125&orderSuffix='.urlencode(
  324. $_args['dataString']
  325. );
  326. }
  327. return $_result;
  328. }
  329. /**获取支付宝吊起地址
  330. *
  331. * @param $url
  332. * @param null $opts
  333. *
  334. * @return bool|mixed
  335. */
  336. public function singleGrabGetdata($url, $opts = null) {
  337. if (empty($url)) {
  338. return false;
  339. }
  340. $ch = curl_init();
  341. $options = array(
  342. CURLOPT_URL => $url,
  343. CURLOPT_USERAGENT => "spider",
  344. CURLOPT_RETURNTRANSFER => true,
  345. CURLOPT_SSL_VERIFYPEER => false,
  346. CURLOPT_SSL_VERIFYHOST => false,
  347. );
  348. if (!is_null($opts)) {
  349. foreach ($opts as $k => $v) {
  350. $options[$k] = $v;
  351. }
  352. unset($k, $v);
  353. }
  354. curl_setopt_array($ch, $options);
  355. $tmpResult = curl_exec($ch);
  356. $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
  357. if ($httpCode == 200) {
  358. curl_close($ch);
  359. return $tmpResult;
  360. }
  361. return false;
  362. }
  363. }