Wechat.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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->channel_id = $config['channel_id'];
  10. $this->system_source = $config['system_source'];
  11. $this->config = new \WxPayConfig();
  12. # 通知接口
  13. $config['notify'] = $this->url($config['type'], $config['id']);
  14. # 证书
  15. $config['ssl'] = array
  16. (
  17. 'cert' => Dever::local($config['file_cert']),
  18. 'key' => Dever::local($config['file_key']),
  19. );
  20. $config['ip'] = (isset($config['ip']) && $config['ip']) ? $config['ip'] : Dever::ip();
  21. $this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout'], $config['ip']);
  22. }
  23. /**
  24. * 通知
  25. */
  26. public function notify()
  27. {
  28. $this->log('支付回调-初始化', file_get_contents("php://input"));
  29. $callback = new Callback();
  30. $result = $callback->Handle($this->config, false);
  31. }
  32. # 查询订单
  33. public function search($order_id)
  34. {
  35. $info = Dever::db('pay/order')->one(array('order_id' => $order_id, 'status' => 2));
  36. if ($info) {
  37. $this->updateOrder($info['order_id'], 1);
  38. return $info;
  39. } else {
  40. $input = new \WxPayOrderQuery();
  41. $input->SetOut_trade_no($order_id);
  42. $result = \WxPayApi::orderQuery($this->config, $input);
  43. if (isset($result['transaction_id']) && isset($result['out_trade_no']) && isset($result['trade_state_desc'])) {
  44. $this->updateOrder($result['out_trade_no'], $result['cash_fee']);
  45. }
  46. return $result;
  47. }
  48. }
  49. # 退款
  50. public function refundByOrder($order_id)
  51. {
  52. $info = Dever::db('pay/order')->one(array('order_id' => $order_id));
  53. if ($info && ($info['status'] == 1 || $info['status'] == 2 || $info['status'] == 5)) {
  54. $this->refund($info['order_id'], $info['cash']);
  55. Dever::db('pay/order')->update(array('where_id' => $info['id'], 'status' => 5));
  56. return $result;
  57. }
  58. return false;
  59. }
  60. # 退款
  61. public function refund($order_id, $cash)
  62. {
  63. $out_trade_no = $order_id;
  64. $cash = $cash * 100;
  65. $total_fee = $cash;
  66. $refund_fee = $cash;
  67. $input = new \WxPayRefund();
  68. $input->SetOut_trade_no($out_trade_no);
  69. $input->SetTotal_fee($total_fee);
  70. $input->SetRefund_fee($refund_fee);
  71. $input->SetOut_refund_no($out_trade_no . '_' . time());
  72. $input->SetOp_user_id($this->config->GetMerchantId());
  73. $result = \WxPayApi::refund($this->config, $input);
  74. return $result;
  75. }
  76. /**
  77. * 获取统一下单的基本信息
  78. */
  79. public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false)
  80. {
  81. $trade_type = $this->getType($type);
  82. $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id);
  83. $tools = new \JsApiPay($this->config);
  84. $input = new \WxPayUnifiedOrder();
  85. $input->SetBody($name);
  86. $input->SetAttach($name);
  87. $input->SetOut_trade_no($order_id);
  88. $input->SetTotal_fee($cash * 100);
  89. $input->SetTime_start(date("YmdHis"));
  90. $input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut()));
  91. //$input->SetGoods_tag($name);
  92. $input->SetNotify_url($this->config->GetNotifyUrl());
  93. $input->SetTrade_type($trade_type);
  94. $input->SetProduct_id($product_id);
  95. if ($type != 4 && $openid != -1) {
  96. if (!$openid && Dever::project('passport')) {
  97. $where = array();
  98. $where['uid'] = $uid;
  99. $where['system_source'] = $this->system_source;
  100. $where['system_id'] = $this->channel_id;
  101. $wechat = Dever::db('passport/wechat')->one($where);
  102. if ($wechat) {
  103. $openid = $wechat['openid'];
  104. }
  105. }
  106. $openid = $openid ? $openid : $tools->GetOpenid();
  107. $input->SetOpenid($openid);
  108. }
  109. if ($type == 2) {
  110. # 下单信息
  111. $this->updateOrderParam($order_id, $input);
  112. return $input;
  113. } else {
  114. $order = \WxPayApi::unifiedOrder($this->config, $input);
  115. # 下单信息
  116. $order['time'] = '' . time() . '';
  117. $order['order_id'] = $order_id;
  118. $order['sign_type'] = $this->config->GetSignType();
  119. unset($order['mch_id']);
  120. $this->updateOrderParam($order_id, $order);
  121. return $order;
  122. }
  123. }
  124. /**
  125. * 获取二维码支付
  126. */
  127. public function qrcode($order, $refer)
  128. {
  129. $notify = new \NativePay();
  130. $result = $notify->GetPayUrl($order);
  131. $url = $result['code_url'];
  132. return $url;
  133. }
  134. /**
  135. * 获取小程序支付
  136. */
  137. public function applet($order)
  138. {
  139. if (isset($order['prepay_id'])) {
  140. $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();
  141. $this->log('支付签名-applet', $string);
  142. if($order['sign_type'] == "MD5"){
  143. $string = md5($string);
  144. } else {
  145. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  146. }
  147. $order['sign'] = $string;
  148. }
  149. return $order;
  150. }
  151. /**
  152. * 获取app支付
  153. */
  154. public function app($order)
  155. {
  156. if (isset($order['prepay_id'])) {
  157. $order['partnerid'] = $this->config->GetMerchantId();
  158. $order['package_string'] = 'Sign=WXPay';
  159. $string = array();
  160. $string['appid'] = $this->config->GetAppId();
  161. $string['partnerid'] = $order['partnerid'];
  162. $string['prepayid'] = $order['prepay_id'];
  163. $string['package'] = $order['package_string'];
  164. $string['noncestr'] = $order['nonce_str'];
  165. $string['timestamp'] = $order['time'];
  166. ksort($string);
  167. $string = str_replace('%3D', '=', http_build_query($string));
  168. $string .= '&key=' . $this->config->GetKey();
  169. $this->log('支付签名-app', $string);
  170. if($order['sign_type'] == "MD5"){
  171. $string = md5($string);
  172. } else {
  173. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  174. }
  175. $order['sign'] = $string;
  176. }
  177. return $order;
  178. }
  179. /**
  180. * 获取页面支付
  181. */
  182. public function page($order, $refer)
  183. {
  184. if (isset($order['mweb_url'])) {
  185. $url = $order['mweb_url'] . '&redirect_url=' . $refer;
  186. if (!$url) {
  187. $location = 'window.open("' . $url . '")';
  188. } else {
  189. $location = 'location.href="' . $url . '"';
  190. }
  191. return '<script type="text/javascript">'.$location.'</script>';
  192. }
  193. $refer = urldecode($refer);
  194. if (strstr($refer, 'callback.')) {
  195. $callback = str_replace('callback.', '', $refer);
  196. $callback = $callback . '()';
  197. } else {
  198. $callback = 'location.href = "'.$refer.'";';
  199. }
  200. $tools = new \JsApiPay($this->config);
  201. $info = $tools->GetJsApiParameters($order);
  202. $html = '<script type="text/javascript">
  203. function jsApiCall()
  204. {
  205. WeixinJSBridge.invoke(
  206. "getBrandWCPayRequest",
  207. '.$info.',
  208. function(res){
  209. //WeixinJSBridge.log(res.err_msg);
  210. if(res.err_msg == "get_brand_wcpay_request:ok")
  211. {
  212. '.$callback.'
  213. } else {
  214. alert("支付失败");
  215. //alert(res.err_code+res.err_desc+res.err_msg);
  216. }
  217. }
  218. );
  219. }
  220. function callpay()
  221. {
  222. if (typeof WeixinJSBridge == "undefined"){
  223. if( document.addEventListener ){
  224. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  225. }else if (document.attachEvent){
  226. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  227. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  228. }
  229. }else{
  230. jsApiCall();
  231. }
  232. }
  233. callpay();
  234. </script>';
  235. return $html;
  236. }
  237. private function getType($type)
  238. {
  239. switch ($type) {
  240. case 1:
  241. $type = 'JSAPI';
  242. break;
  243. case 2:
  244. $type = 'NATIVE';
  245. break;
  246. case 3:
  247. $type = 'APP';
  248. break;
  249. case 4:
  250. $type = 'MWEB';
  251. break;
  252. }
  253. return $type;
  254. }
  255. }
  256. class Callback extends \WxPayNotify
  257. {
  258. public function NotifyProcess($objData, $config, &$msg)
  259. {
  260. $data = $objData->GetValues();
  261. $obj = Dever::load('pay/lib/core');
  262. $obj->log('支付回调-获取数据', $data);
  263. $callback = function($msg = '') use ($obj, $data) {
  264. if ($msg) {
  265. $msg = $data['transaction_id'] . ':' . $msg;
  266. }
  267. $obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);
  268. };
  269. if(!array_key_exists("transaction_id", $data)){
  270. $msg = '输入参数不正确';
  271. $callback($msg);
  272. return false;
  273. }
  274. # 参数校验
  275. if(!array_key_exists("return_code", $data)
  276. ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
  277. $msg = $data['return_code'] . '(异常)';
  278. $callback($msg);
  279. return false;
  280. }
  281. # 进行签名验证
  282. try {
  283. $checkResult = $objData->CheckSign($config);
  284. if($checkResult == false){
  285. $msg = '签名错误';
  286. $callback($msg);
  287. return false;
  288. }
  289. } catch(Exception $e) {
  290. $msg = '签名异常';
  291. $callback($msg);
  292. return false;
  293. }
  294. # 查询订单,判断订单真实性
  295. /*
  296. if(!$this->Queryorder($data["transaction_id"])){
  297. $msg = '订单查询失败';
  298. $callback($msg);
  299. return false;
  300. }
  301. */
  302. # 处理业务逻辑
  303. $callback();
  304. return true;
  305. }
  306. }