Wechat.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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']);
  12. # 证书
  13. $config['ssl'] = array
  14. (
  15. 'cert' => $config['file_cert'],
  16. 'key' => $config['file_key'],
  17. );
  18. $this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout']);
  19. }
  20. /**
  21. * 通知
  22. */
  23. public function notify()
  24. {
  25. $callback = new Callback();
  26. $result = $callback->Handle($this->config, false);
  27. }
  28. /**
  29. * 获取统一下单的基本信息
  30. */
  31. public function order($account_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1)
  32. {
  33. $trade_type = $this->getType($type);
  34. $order_id = $this->createOrder($uid, $username, $account_id, $product_id, $name, $cash, $this->config->GetType());
  35. $tools = new \JsApiPay($this->config);
  36. $openid = $openid ? $openid : $tools->GetOpenid();
  37. $input = new \WxPayUnifiedOrder();
  38. $input->SetBody($name);
  39. $input->SetAttach($name);
  40. $input->SetOut_trade_no($order_id);
  41. $input->SetTotal_fee($cash);
  42. $input->SetTime_start(date("YmdHis"));
  43. $input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut()));
  44. //$input->SetGoods_tag($name);
  45. $input->SetNotify_url($this->config->GetNotifyUrl());
  46. $input->SetTrade_type($trade_type);
  47. $input->SetProduct_id($product_id);
  48. $input->SetOpenid($openid);
  49. if ($type == 1) {
  50. $order = \WxPayApi::unifiedOrder($this->config, $input);
  51. # 下单信息
  52. $this->updateOrderParam($order_id, $order);
  53. $order['time'] = time();
  54. return array($order_id, $order);
  55. } else {
  56. # 下单信息
  57. $this->updateOrderParam($order_id, $input);
  58. return array($order_id, $input);
  59. }
  60. }
  61. /**
  62. * 获取二维码支付
  63. */
  64. public function qrcode($order, $refer)
  65. {
  66. $notify = new \NativePay();
  67. $result = $notify->GetPayUrl($order[1]);
  68. $url = $result['code_url'];
  69. return $url;
  70. }
  71. /**
  72. * 获取页面支付
  73. */
  74. public function page($order, $refer)
  75. {
  76. $refer = urldecode($refer);
  77. $tools = new \JsApiPay($this->config);
  78. $info = $tools->GetJsApiParameters($order[1]);
  79. $html = '<script type="text/javascript">
  80. function jsApiCall()
  81. {
  82. WeixinJSBridge.invoke(
  83. "getBrandWCPayRequest",
  84. '.$info.',
  85. function(res){
  86. //WeixinJSBridge.log(res.err_msg);
  87. if(res.err_msg == "get_brand_wcpay_request:ok")
  88. {
  89. location.href = "'.$refer.'";
  90. } else {
  91. alert(res.err_code+res.err_desc+res.err_msg);
  92. }
  93. }
  94. );
  95. }
  96. function callpay()
  97. {
  98. if (typeof WeixinJSBridge == "undefined"){
  99. if( document.addEventListener ){
  100. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  101. }else if (document.attachEvent){
  102. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  103. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  104. }
  105. }else{
  106. jsApiCall();
  107. }
  108. }
  109. callpay();
  110. </script>';
  111. return $html;
  112. }
  113. private function getType($type)
  114. {
  115. switch ($type) {
  116. case 1:
  117. $type = 'JSAPI';
  118. break;
  119. case 2:
  120. $type = 'NATIVE';
  121. break;
  122. }
  123. return $type;
  124. }
  125. }
  126. class Callback extends \WxPayNotify
  127. {
  128. public function NotifyProcess($objData, $config, &$msg)
  129. {
  130. $data = $objData->GetValues();
  131. $obj = Dever::load('pay/lib/wechat');
  132. $callback = function($msg = '') use ($obj, $data) {
  133. if ($msg) {
  134. $msg = $data['transaction_id'] . ':' . $msg;
  135. }
  136. $obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);
  137. };
  138. # 参数校验
  139. if(!array_key_exists("return_code", $data)
  140. ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
  141. $msg = '异常';
  142. return false;
  143. }
  144. if(!array_key_exists("transaction_id", $data)){
  145. $msg = '输入参数不正确';
  146. return false;
  147. }
  148. # 进行签名验证
  149. try {
  150. $checkResult = $objData->CheckSign($config);
  151. if($checkResult == false){
  152. $msg = '签名错误';
  153. $callback($msg);
  154. return false;
  155. }
  156. } catch(Exception $e) {
  157. $msg = '签名异常';
  158. $callback($msg);
  159. return false;
  160. }
  161. # 查询订单,判断订单真实性
  162. if(!$this->Queryorder($data["transaction_id"])){
  163. $msg = '订单查询失败';
  164. $callback($msg);
  165. return false;
  166. }
  167. # 处理业务逻辑
  168. $callback();
  169. return true;
  170. }
  171. }