Wechat.php 7.5 KB

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