Wechat.php 7.4 KB

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