Wechat.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php namespace Pay\Lib;
  2. use Dever;
  3. Dever::apply('sdk/wechat', 'pay');
  4. class Wechat extends Core
  5. {
  6. public function __construct($config)
  7. {
  8. $project = Dever::project('pay');
  9. $this->config = new \WxPayConfig();
  10. # 通知接口
  11. $config['notify'] = $this->url($config['type'], $config['id']);
  12. # 证书
  13. $config['ssl'] = array
  14. (
  15. 'cert' => $config['file_cert'],
  16. 'key' => $config['file_key'],
  17. );
  18. $config['ip'] = (isset($config['ip']) && $config['ip']) ? $config['ip'] : Dever::ip();
  19. $this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout'], $config['ip']);
  20. }
  21. /**
  22. * 通知
  23. */
  24. public function notify()
  25. {
  26. $this->log('支付回调-初始化', file_get_contents("php://input"));
  27. $callback = new Callback();
  28. $result = $callback->Handle($this->config, false);
  29. }
  30. # 查询订单
  31. public function search($order_id)
  32. {
  33. $input = new \WxPayOrderQuery();
  34. $input->SetOut_trade_no($order_id);
  35. $result = \WxPayApi::orderQuery($this->config, $input);
  36. if (isset($result['transaction_id']) && isset($result['out_trade_no']) && isset($result['trade_state_desc'])) {
  37. if ($msg) {
  38. $msg = $result['transaction_id'] . ':' . $msg;
  39. }
  40. $this->updateOrder($result['out_trade_no'], $result['cash_fee'], $msg);
  41. }
  42. return $result;
  43. }
  44. /**
  45. * 获取统一下单的基本信息
  46. */
  47. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
  48. {
  49. $trade_type = $this->getType($type);
  50. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
  51. $tools = new \JsApiPay($this->config);
  52. $input = new \WxPayUnifiedOrder();
  53. $input->SetBody($name);
  54. $input->SetAttach($name);
  55. $input->SetOut_trade_no($order_id);
  56. $input->SetTotal_fee($cash * 100);
  57. $input->SetTime_start(date("YmdHis"));
  58. $input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut()));
  59. //$input->SetGoods_tag($name);
  60. $input->SetNotify_url($this->config->GetNotifyUrl());
  61. $input->SetTrade_type($trade_type);
  62. $input->SetProduct_id($product_id);
  63. if ($type != 4 && $openid != -1) {
  64. $openid = $openid ? $openid : $tools->GetOpenid();
  65. $input->SetOpenid($openid);
  66. }
  67. if ($type == 2) {
  68. # 下单信息
  69. $this->updateOrderParam($order_id, $input);
  70. return $input;
  71. } else {
  72. $order = \WxPayApi::unifiedOrder($this->config, $input);
  73. # 下单信息
  74. $order['time'] = '' . time() . '';
  75. $order['order_id'] = $order_id;
  76. $order['sign_type'] = $this->config->GetSignType();
  77. unset($order['mch_id']);
  78. $this->updateOrderParam($order_id, $order);
  79. return $order;
  80. }
  81. }
  82. /**
  83. * 获取二维码支付
  84. */
  85. public function qrcode($order, $refer)
  86. {
  87. $notify = new \NativePay();
  88. $result = $notify->GetPayUrl($order);
  89. $url = $result['code_url'];
  90. return $url;
  91. }
  92. /**
  93. * 获取小程序支付
  94. */
  95. public function applet($order)
  96. {
  97. if (isset($order['prepay_id'])) {
  98. $string = 'appId='.$this->config->GetAppId().'&nonceStr='.$order['nonce_str'].'&package=prepay_id='.$order['prepay_id'].'&signType='.$order['sign_type'].'&timeStamp='.$order['time'].'&key='.$this->config->GetKey();
  99. $this->log('支付签名-applet', $string);
  100. if($order['sign_type'] == "MD5"){
  101. $string = md5($string);
  102. } else {
  103. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  104. }
  105. $order['sign'] = $string;
  106. }
  107. return $order;
  108. }
  109. /**
  110. * 获取app支付
  111. */
  112. public function app($order)
  113. {
  114. if (isset($order['prepay_id'])) {
  115. $order['partnerid'] = $this->config->GetMerchantId();
  116. $order['package_string'] = 'Sign=WXPay';
  117. $string = array();
  118. $string['appid'] = $this->config->GetAppId();
  119. $string['partnerid'] = $order['partnerid'];
  120. $string['prepayid'] = $order['prepay_id'];
  121. $string['package'] = $order['package_string'];
  122. $string['noncestr'] = $order['nonce_str'];
  123. $string['timestamp'] = $order['time'];
  124. ksort($string);
  125. $string = str_replace('%3D', '=', http_build_query($string));
  126. $string .= '&key=' . $this->config->GetKey();
  127. $this->log('支付签名-app', $string);
  128. if($order['sign_type'] == "MD5"){
  129. $string = md5($string);
  130. } else {
  131. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  132. }
  133. $order['sign'] = $string;
  134. }
  135. return $order;
  136. }
  137. /**
  138. * 获取页面支付
  139. */
  140. public function page($order, $refer)
  141. {
  142. if (isset($order['mweb_url'])) {
  143. $url = $order['mweb_url'] . '&redirect_url=' . $refer;
  144. if (!$url) {
  145. $location = 'window.open("' . $url . '")';
  146. } else {
  147. $location = 'location.href="' . $url . '"';
  148. }
  149. return '<script type="text/javascript">'.$location.'</script>';
  150. }
  151. $refer = urldecode($refer);
  152. $tools = new \JsApiPay($this->config);
  153. $info = $tools->GetJsApiParameters($order);
  154. $html = '<script type="text/javascript">
  155. function jsApiCall()
  156. {
  157. WeixinJSBridge.invoke(
  158. "getBrandWCPayRequest",
  159. '.$info.',
  160. function(res){
  161. //WeixinJSBridge.log(res.err_msg);
  162. if(res.err_msg == "get_brand_wcpay_request:ok")
  163. {
  164. location.href = "'.$refer.'";
  165. } else {
  166. alert("支付失败");
  167. //alert(res.err_code+res.err_desc+res.err_msg);
  168. }
  169. }
  170. );
  171. }
  172. function callpay()
  173. {
  174. if (typeof WeixinJSBridge == "undefined"){
  175. if( document.addEventListener ){
  176. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  177. }else if (document.attachEvent){
  178. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  179. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  180. }
  181. }else{
  182. jsApiCall();
  183. }
  184. }
  185. callpay();
  186. </script>';
  187. return $html;
  188. }
  189. private function getType($type)
  190. {
  191. switch ($type) {
  192. case 1:
  193. $type = 'JSAPI';
  194. break;
  195. case 2:
  196. $type = 'NATIVE';
  197. break;
  198. case 3:
  199. $type = 'APP';
  200. break;
  201. case 4:
  202. $type = 'MWEB';
  203. break;
  204. }
  205. return $type;
  206. }
  207. }
  208. class Callback extends \WxPayNotify
  209. {
  210. public function NotifyProcess($objData, $config, &$msg)
  211. {
  212. $data = $objData->GetValues();
  213. $obj = Dever::load('pay/lib/core');
  214. $obj->log('支付回调-获取数据', $data);
  215. $callback = function($msg = '') use ($obj, $data) {
  216. if ($msg) {
  217. $msg = $data['transaction_id'] . ':' . $msg;
  218. }
  219. $obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);
  220. };
  221. if(!array_key_exists("transaction_id", $data)){
  222. $msg = '输入参数不正确';
  223. $callback($msg);
  224. return false;
  225. }
  226. # 参数校验
  227. if(!array_key_exists("return_code", $data)
  228. ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
  229. $msg = $data['return_code'] . '(异常)';
  230. $callback($msg);
  231. return false;
  232. }
  233. # 进行签名验证
  234. try {
  235. $checkResult = $objData->CheckSign($config);
  236. if($checkResult == false){
  237. $msg = '签名错误';
  238. $callback($msg);
  239. return false;
  240. }
  241. } catch(Exception $e) {
  242. $msg = '签名异常';
  243. $callback($msg);
  244. return false;
  245. }
  246. # 查询订单,判断订单真实性
  247. /*
  248. if(!$this->Queryorder($data["transaction_id"])){
  249. $msg = '订单查询失败';
  250. $callback($msg);
  251. return false;
  252. }
  253. */
  254. # 处理业务逻辑
  255. $callback();
  256. return true;
  257. }
  258. }