Wechat.php 6.8 KB

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