Wechat.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. 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 = http_build_query($string);
  112. if($order['sign_type'] == "MD5"){
  113. $string = md5($string);
  114. } else {
  115. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  116. }
  117. $order['sign'] = $string;
  118. }
  119. return $order;
  120. }
  121. /**
  122. * 获取页面支付
  123. */
  124. public function page($order, $refer)
  125. {
  126. $refer = urldecode($refer);
  127. $tools = new \JsApiPay($this->config);
  128. $info = $tools->GetJsApiParameters($order);
  129. $html = '<script type="text/javascript">
  130. function jsApiCall()
  131. {
  132. WeixinJSBridge.invoke(
  133. "getBrandWCPayRequest",
  134. '.$info.',
  135. function(res){
  136. //WeixinJSBridge.log(res.err_msg);
  137. if(res.err_msg == "get_brand_wcpay_request:ok")
  138. {
  139. location.href = "'.$refer.'";
  140. } else {
  141. alert("支付失败");
  142. //alert(res.err_code+res.err_desc+res.err_msg);
  143. }
  144. }
  145. );
  146. }
  147. function callpay()
  148. {
  149. if (typeof WeixinJSBridge == "undefined"){
  150. if( document.addEventListener ){
  151. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  152. }else if (document.attachEvent){
  153. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  154. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  155. }
  156. }else{
  157. jsApiCall();
  158. }
  159. }
  160. callpay();
  161. </script>';
  162. return $html;
  163. }
  164. private function getType($type)
  165. {
  166. switch ($type) {
  167. case 1:
  168. $type = 'JSAPI';
  169. break;
  170. case 2:
  171. $type = 'NATIVE';
  172. break;
  173. case 3:
  174. $type = 'APP';
  175. break;
  176. }
  177. return $type;
  178. }
  179. }
  180. class Callback extends \WxPayNotify
  181. {
  182. public function NotifyProcess($objData, $config, &$msg)
  183. {
  184. $data = $objData->GetValues();
  185. $obj = Dever::load('pay/lib/core');
  186. $obj->log('支付回调-获取数据', $data);
  187. $callback = function($msg = '') use ($obj, $data) {
  188. if ($msg) {
  189. $msg = $data['transaction_id'] . ':' . $msg;
  190. }
  191. $obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);
  192. };
  193. if(!array_key_exists("transaction_id", $data)){
  194. $msg = '输入参数不正确';
  195. $callback($msg);
  196. return false;
  197. }
  198. # 参数校验
  199. if(!array_key_exists("return_code", $data)
  200. ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
  201. $msg = $data['return_code'] . '(异常)';
  202. $callback($msg);
  203. return false;
  204. }
  205. # 进行签名验证
  206. try {
  207. $checkResult = $objData->CheckSign($config);
  208. if($checkResult == false){
  209. $msg = '签名错误';
  210. $callback($msg);
  211. return false;
  212. }
  213. } catch(Exception $e) {
  214. $msg = '签名异常';
  215. $callback($msg);
  216. return false;
  217. }
  218. # 查询订单,判断订单真实性
  219. /*
  220. if(!$this->Queryorder($data["transaction_id"])){
  221. $msg = '订单查询失败';
  222. $callback($msg);
  223. return false;
  224. }
  225. */
  226. # 处理业务逻辑
  227. $callback();
  228. return true;
  229. }
  230. }