Wechat.php 5.4 KB

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