Wechat.php 7.6 KB

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