Wechat.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. $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. $order['time'] = '' . time() . '';
  53. $order['order_id'] = $order_id;
  54. $order['sign_type'] = $this->config->GetSignType();
  55. unset($order['mch_id']);
  56. $this->updateOrderParam($order_id, $order);
  57. return $order;
  58. } else {
  59. # 下单信息
  60. $this->updateOrderParam($order_id, $input);
  61. return $input;
  62. }
  63. }
  64. /**
  65. * 获取二维码支付
  66. */
  67. public function qrcode($order, $refer)
  68. {
  69. $notify = new \NativePay();
  70. $result = $notify->GetPayUrl($order);
  71. $url = $result['code_url'];
  72. return $url;
  73. }
  74. /**
  75. * 获取小程序支付
  76. */
  77. public function applet($order)
  78. {
  79. if (isset($order['prepay_id'])) {
  80. $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();
  81. if($order['sign_type'] == "MD5"){
  82. $string = md5($string);
  83. } else {
  84. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  85. }
  86. $order['sign'] = $string;
  87. }
  88. return $order;
  89. }
  90. /**
  91. * 获取页面支付
  92. */
  93. public function page($order, $refer)
  94. {
  95. $refer = urldecode($refer);
  96. $tools = new \JsApiPay($this->config);
  97. $info = $tools->GetJsApiParameters($order);
  98. $html = '<script type="text/javascript">
  99. function jsApiCall()
  100. {
  101. WeixinJSBridge.invoke(
  102. "getBrandWCPayRequest",
  103. '.$info.',
  104. function(res){
  105. //WeixinJSBridge.log(res.err_msg);
  106. if(res.err_msg == "get_brand_wcpay_request:ok")
  107. {
  108. location.href = "'.$refer.'";
  109. } else {
  110. alert(res.err_code+res.err_desc+res.err_msg);
  111. }
  112. }
  113. );
  114. }
  115. function callpay()
  116. {
  117. if (typeof WeixinJSBridge == "undefined"){
  118. if( document.addEventListener ){
  119. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  120. }else if (document.attachEvent){
  121. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  122. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  123. }
  124. }else{
  125. jsApiCall();
  126. }
  127. }
  128. callpay();
  129. </script>';
  130. return $html;
  131. }
  132. private function getType($type)
  133. {
  134. switch ($type) {
  135. case 1:
  136. $type = 'JSAPI';
  137. break;
  138. case 2:
  139. $type = 'NATIVE';
  140. break;
  141. }
  142. return $type;
  143. }
  144. }
  145. class Callback extends \WxPayNotify
  146. {
  147. public function NotifyProcess($objData, $config, &$msg)
  148. {
  149. $data = $objData->GetValues();
  150. $obj = Dever::load('pay/lib/core');
  151. $callback = function($msg = '') use ($obj, $data) {
  152. if ($msg) {
  153. $msg = $data['transaction_id'] . ':' . $msg;
  154. }
  155. $obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);
  156. };
  157. if(!array_key_exists("transaction_id", $data)){
  158. $msg = '输入参数不正确';
  159. $callback($msg);
  160. return false;
  161. }
  162. # 参数校验
  163. if(!array_key_exists("return_code", $data)
  164. ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
  165. $msg = $data['return_code'] . '(异常)';
  166. $callback($msg);
  167. return false;
  168. }
  169. # 进行签名验证
  170. try {
  171. $checkResult = $objData->CheckSign($config);
  172. if($checkResult == false){
  173. $msg = '签名错误';
  174. $callback($msg);
  175. return false;
  176. }
  177. } catch(Exception $e) {
  178. $msg = '签名异常';
  179. $callback($msg);
  180. return false;
  181. }
  182. # 查询订单,判断订单真实性
  183. if(!$this->Queryorder($data["transaction_id"])){
  184. $msg = '订单查询失败';
  185. $callback($msg);
  186. return false;
  187. }
  188. # 处理业务逻辑
  189. $callback();
  190. return true;
  191. }
  192. }