Wechat.php 7.2 KB

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