Wechat.php 7.0 KB

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