Wxpay.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. <?php
  2. /**
  3. * Wxpay.php UTF-8
  4. *
  5. *
  6. * @date : 2018/5/3 23:07
  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 think\Exception;
  16. use think\Log;
  17. class Wxpay extends Driver {
  18. /**
  19. * 构造函数
  20. *
  21. */
  22. public function __construct() {
  23. }
  24. /**
  25. * 移动APP支付函数
  26. * https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=8_5
  27. *
  28. * @return mixed
  29. */
  30. public function clientPay() {
  31. $_trade_type = 'APP';
  32. $_class = new WxNotifyCallBack();
  33. /* https://pay.weixin.qq.com/wiki/doc/api/app/app.php?chapter=9_1 */
  34. try {
  35. $_token = $_class->unifiedOrder(
  36. $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
  37. $this->notify_url, $_trade_type
  38. );
  39. return $this->clientAjax(PaywayConst::PAYWAY_WXPAY, $_token, 1, 2, 1);
  40. } catch (\WxPayException $e) {
  41. return false;
  42. }
  43. }
  44. /**
  45. * 小程序支付
  46. * https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=7_10&index=1
  47. *
  48. * @param array $config 配置
  49. *
  50. */
  51. public function mpPay($config = []) {
  52. $_trade_type = 'MPAPI';
  53. $_class = new WxNotifyCallBack();
  54. if (!empty($config)) {
  55. $_class->setConf($config);
  56. }
  57. /* https://pay.weixin.qq.com/wiki/doc/api/wxa/wxa_api.php?chapter=9_1 */
  58. // try {
  59. $_token = $_class->unifiedOrder(
  60. $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
  61. $this->notify_url, $_trade_type, $this->open_id
  62. );
  63. return $this->clientAjax(PaywayConst::PAYWAY_WXPAYMP, $_token, 1, 2, 1);
  64. // } catch (\WxPayException $e) {
  65. //// return false;
  66. //// }
  67. }
  68. /**
  69. * wap H5网页支付
  70. * https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=15_4
  71. *
  72. * @param array $config
  73. *
  74. * @return array|bool
  75. */
  76. public function mobilePay($config = []) {
  77. $_trade_type = 'MWEB';
  78. $_class = new WxNotifyCallBack();
  79. if (!empty($config)) {
  80. $_class->setConf($config);
  81. }
  82. try {
  83. /* https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_1 */
  84. $_token = $_class->unifiedOrder(
  85. $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_name,
  86. $this->notify_url, $_trade_type
  87. );
  88. if (!empty($_token) && isset($_token['return_code']) && 'SUCCESS' == $_token['return_code']
  89. && !empty($_token['mweb_url'])
  90. ) {
  91. if (isset($_SERVER['HTTP_REFERER']) && strstr($_SERVER['HTTP_REFERER'], DOCDOMAIN) !== false) {
  92. $_pay_url = $_token['mweb_url'];
  93. $_ch = curl_init();
  94. curl_setopt($_ch, CURLOPT_URL, $_pay_url);
  95. if (strpos($_pay_url, 'https') === 0) {
  96. curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, false);
  97. curl_setopt($_ch, CURLOPT_SSL_VERIFYHOST, 2);
  98. }
  99. $_headers['CLIENT-IP'] = $_SERVER['REMOTE_ADDR'];
  100. $_headers['X-FORWARDED-FOR'] = $_SERVER['REMOTE_ADDR'];
  101. $_header_arr = array();
  102. foreach ($_headers as $n => $v) {
  103. $_header_arr[] = $n.':'.$v;
  104. }
  105. curl_setopt($_ch, CURLOPT_RETURNTRANSFER, 1);
  106. curl_setopt($_ch, CURLOPT_HTTPHEADER, $_header_arr); //构造IP
  107. curl_setopt($_ch, CURLOPT_CONNECTTIMEOUT, 5); // 连接超时(秒)
  108. //微信那边填的微信授权域
  109. curl_setopt($_ch, CURLOPT_REFERER, H5MSITE);
  110. curl_setopt($_ch, CURLOPT_TIMEOUT, 5); // 执行超时(秒)
  111. $_out_put = curl_exec($_ch);
  112. if ($_out_put === false) {
  113. echo curl_error($_ch);
  114. die;
  115. }
  116. curl_close($_ch);
  117. //匹配出支付链接
  118. preg_match('/weixin(.*)"/', $_out_put, $_match);
  119. if (!isset($_match[1])) {
  120. throw new Exception('无法获取支付链接:'.$_out_put);
  121. }
  122. $_client_pay_url = 'weixin'.$_match[1];
  123. $_url = $_client_pay_url;
  124. } else {
  125. $_url = $_token['mweb_url'].'&redirect_url='.urlencode($this->return_url);
  126. }
  127. /*备用调起支付url*/
  128. $_spare_url = $_token['mweb_url'].'&redirect_url='.urlencode($this->return_url);
  129. return $this->clientAjax(PaywayConst::PAYWAY_WXPAYH5, $_url, 1, 1, 1, $_spare_url);
  130. }
  131. return false;
  132. } catch (\WxPayException $e) {
  133. return false;
  134. }
  135. }
  136. /**
  137. * PC端支付
  138. * 返回二维码下单地址
  139. * https://pay.weixin.qq.com/wiki/doc/api/native.php?chapter=6_5
  140. *
  141. * @param array $config
  142. *
  143. * @return bool
  144. */
  145. public function pcPay($config = []) {
  146. $_trade_type = 'NATIVE';
  147. $_class = new WxNotifyCallBack();
  148. if (!empty($config)) {
  149. $_class->setConf($config);
  150. }
  151. try {
  152. $_token = $_class->unifiedOrder(
  153. $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
  154. $this->notify_url, $_trade_type
  155. );
  156. if (!empty($_token) && isset($_token['code_url'])) {
  157. $_code_url = $_token['code_url'];
  158. return $this->clientAjax(PaywayConst::PAYWAY_WXPAYQR, $_code_url, 1, 1, 1);
  159. }
  160. } catch (\WxPayException $e) {
  161. return false;
  162. }
  163. return false;
  164. }
  165. /**
  166. * 微信内调用微信公众号支付
  167. * https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=7_4
  168. *
  169. * @param array $config 配置
  170. *
  171. * @return bool
  172. */
  173. public function jsPay($config = []) {
  174. $_trade_type = 'JSAPI';
  175. $_class = new WxNotifyCallBack();
  176. if (!empty($config)) {
  177. $_class->setConf($config);
  178. }
  179. try {
  180. /* https://pay.weixin.qq.com/wiki/doc/api/jsapi.php?chapter=9_1 */
  181. $_token = $_class->unifiedOrder(
  182. $this->order_id, $this->real_amount, $this->ip, $this->product_id, $this->product_desc,
  183. $this->notify_url, $_trade_type, $this->open_id
  184. );
  185. if (!empty($_token)) {
  186. return $this->clientAjax(PaywayConst::PAYWAY_WXPAYJS, $_token, 1, 1, 1);
  187. }
  188. } catch (\WxPayException $e) {
  189. return false;
  190. }
  191. return false;
  192. }
  193. /**
  194. * 钱包充值回调函数
  195. */
  196. public function walletNotify() {
  197. }
  198. /**
  199. * 游戏币充值回调
  200. */
  201. public function gmNotify() {
  202. }
  203. /*
  204. * 异步回调函数
  205. */
  206. public function notifyUrl($config = []) {
  207. $notify = new WxNotifyCallBack();
  208. if (!empty($config)) {
  209. $notify->setConf($config);
  210. }
  211. $notify->selfHandle(false, $this->order_class, $this->func);
  212. }
  213. /*
  214. * 返回接收页面
  215. */
  216. public function returnUrl($order_id = '') {
  217. $_status = 1;
  218. return $this->clientAjax($this->payway, '', $_status, 1, 1);
  219. }
  220. /**
  221. * 查询订单
  222. * https://pay.weixin.qq.com/wiki/doc/api/H5.php?chapter=9_2&index=2
  223. *
  224. * @access public
  225. *
  226. * @param string $order_id 商户系统内部订单号
  227. * @param string $transaction_id 第三方支付的订单号
  228. * @param null $ext 扩展信息
  229. *
  230. * @return bool|string
  231. */
  232. public function orderQuery($order_id, $transaction_id, $ext = null) {
  233. $_config = get_val($ext, 'config', []);
  234. $_class = new WxNotifyCallBack();
  235. if (!empty($_config)) {
  236. $_class->setConf($_config);
  237. }
  238. $_rs = $_class->orderQuery($order_id, $transaction_id);
  239. return $_rs;
  240. }
  241. }