Wechat.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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. $this->log('支付回调-初始化', file_get_contents("php://input"));
  26. $callback = new Callback();
  27. $result = $callback->Handle($this->config, false);
  28. }
  29. /**
  30. * 获取统一下单的基本信息
  31. */
  32. public function order($account_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
  33. {
  34. $trade_type = $this->getType($type);
  35. $order_id = $this->createOrder($uid, $username, $account_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
  36. $tools = new \JsApiPay($this->config);
  37. $openid = $openid ? $openid : $tools->GetOpenid();
  38. $input = new \WxPayUnifiedOrder();
  39. $input->SetBody($name);
  40. $input->SetAttach($name);
  41. $input->SetOut_trade_no($order_id);
  42. $input->SetTotal_fee($cash * 100);
  43. $input->SetTime_start(date("YmdHis"));
  44. $input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut()));
  45. //$input->SetGoods_tag($name);
  46. $input->SetNotify_url($this->config->GetNotifyUrl());
  47. $input->SetTrade_type($trade_type);
  48. $input->SetProduct_id($product_id);
  49. $input->SetOpenid($openid);
  50. if ($type == 1) {
  51. $order = \WxPayApi::unifiedOrder($this->config, $input);
  52. # 下单信息
  53. $order['time'] = '' . time() . '';
  54. $order['order_id'] = $order_id;
  55. $order['sign_type'] = $this->config->GetSignType();
  56. unset($order['mch_id']);
  57. $this->updateOrderParam($order_id, $order);
  58. return $order;
  59. } else {
  60. # 下单信息
  61. $this->updateOrderParam($order_id, $input);
  62. return $input;
  63. }
  64. }
  65. /**
  66. * 获取二维码支付
  67. */
  68. public function qrcode($order, $refer)
  69. {
  70. $notify = new \NativePay();
  71. $result = $notify->GetPayUrl($order);
  72. $url = $result['code_url'];
  73. return $url;
  74. }
  75. /**
  76. * 获取小程序支付
  77. */
  78. public function applet($order)
  79. {
  80. if (isset($order['prepay_id'])) {
  81. $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();
  82. if($order['sign_type'] == "MD5"){
  83. $string = md5($string);
  84. } else {
  85. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  86. }
  87. $order['sign'] = $string;
  88. }
  89. return $order;
  90. }
  91. /**
  92. * 获取页面支付
  93. */
  94. public function page($order, $refer)
  95. {
  96. $refer = urldecode($refer);
  97. $tools = new \JsApiPay($this->config);
  98. $info = $tools->GetJsApiParameters($order);
  99. $html = '<script type="text/javascript">
  100. function jsApiCall()
  101. {
  102. WeixinJSBridge.invoke(
  103. "getBrandWCPayRequest",
  104. '.$info.',
  105. function(res){
  106. //WeixinJSBridge.log(res.err_msg);
  107. if(res.err_msg == "get_brand_wcpay_request:ok")
  108. {
  109. location.href = "'.$refer.'";
  110. } else {
  111. alert(res.err_code+res.err_desc+res.err_msg);
  112. }
  113. }
  114. );
  115. }
  116. function callpay()
  117. {
  118. if (typeof WeixinJSBridge == "undefined"){
  119. if( document.addEventListener ){
  120. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  121. }else if (document.attachEvent){
  122. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  123. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  124. }
  125. }else{
  126. jsApiCall();
  127. }
  128. }
  129. callpay();
  130. </script>';
  131. return $html;
  132. }
  133. private function getType($type)
  134. {
  135. switch ($type) {
  136. case 1:
  137. $type = 'JSAPI';
  138. break;
  139. case 2:
  140. $type = 'NATIVE';
  141. break;
  142. }
  143. return $type;
  144. }
  145. }
  146. class Callback extends \WxPayNotify
  147. {
  148. public function NotifyProcess($objData, $config, &$msg)
  149. {
  150. $data = $objData->GetValues();
  151. $obj = Dever::load('pay/lib/core');
  152. $obj->log('支付回调-获取数据', $data);
  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. /*
  186. if(!$this->Queryorder($data["transaction_id"])){
  187. $msg = '订单查询失败';
  188. $callback($msg);
  189. return false;
  190. }
  191. */
  192. # 处理业务逻辑
  193. $callback();
  194. return true;
  195. }
  196. }