Wechat.php 6.6 KB

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