Wechat.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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'] = 'http://trade.5dev.cn/pay/pay/notify.php';
  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. $receipt = $_REQUEST;
  26. if ($receipt == null) {
  27. $receipt = file_get_contents("php://input");
  28. }
  29. if ($receipt == null) {
  30. $receipt = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : $_POST;
  31. }
  32. $this->log('支付回调-初始化', $receipt);
  33. $callback = new Callback();
  34. $result = $callback->Handle($this->config, false);
  35. }
  36. /**
  37. * 获取统一下单的基本信息
  38. */
  39. public function order($account_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1)
  40. {
  41. $trade_type = $this->getType($type);
  42. $order_id = $this->createOrder($uid, $username, $account_id, $product_id, $name, $cash, $this->config->GetType());
  43. $tools = new \JsApiPay($this->config);
  44. $openid = $openid ? $openid : $tools->GetOpenid();
  45. $input = new \WxPayUnifiedOrder();
  46. $input->SetBody($name);
  47. $input->SetAttach($name);
  48. $input->SetOut_trade_no($order_id);
  49. $input->SetTotal_fee($cash);
  50. $input->SetTime_start(date("YmdHis"));
  51. $input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut()));
  52. //$input->SetGoods_tag($name);
  53. $input->SetNotify_url($this->config->GetNotifyUrl());
  54. $input->SetTrade_type($trade_type);
  55. $input->SetProduct_id($product_id);
  56. $input->SetOpenid($openid);
  57. if ($type == 1) {
  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. } else {
  67. # 下单信息
  68. $this->updateOrderParam($order_id, $input);
  69. return $input;
  70. }
  71. }
  72. /**
  73. * 获取二维码支付
  74. */
  75. public function qrcode($order, $refer)
  76. {
  77. $notify = new \NativePay();
  78. $result = $notify->GetPayUrl($order);
  79. $url = $result['code_url'];
  80. return $url;
  81. }
  82. /**
  83. * 获取小程序支付
  84. */
  85. public function applet($order)
  86. {
  87. if (isset($order['prepay_id'])) {
  88. $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();
  89. if($order['sign_type'] == "MD5"){
  90. $string = md5($string);
  91. } else {
  92. $string = hash_hmac("sha256", $string, $this->config->GetKey());
  93. }
  94. $order['sign'] = $string;
  95. }
  96. return $order;
  97. }
  98. /**
  99. * 获取页面支付
  100. */
  101. public function page($order, $refer)
  102. {
  103. $refer = urldecode($refer);
  104. $tools = new \JsApiPay($this->config);
  105. $info = $tools->GetJsApiParameters($order);
  106. $html = '<script type="text/javascript">
  107. function jsApiCall()
  108. {
  109. WeixinJSBridge.invoke(
  110. "getBrandWCPayRequest",
  111. '.$info.',
  112. function(res){
  113. //WeixinJSBridge.log(res.err_msg);
  114. if(res.err_msg == "get_brand_wcpay_request:ok")
  115. {
  116. location.href = "'.$refer.'";
  117. } else {
  118. alert(res.err_code+res.err_desc+res.err_msg);
  119. }
  120. }
  121. );
  122. }
  123. function callpay()
  124. {
  125. if (typeof WeixinJSBridge == "undefined"){
  126. if( document.addEventListener ){
  127. document.addEventListener("WeixinJSBridgeReady", jsApiCall, false);
  128. }else if (document.attachEvent){
  129. document.attachEvent("WeixinJSBridgeReady", jsApiCall);
  130. document.attachEvent("onWeixinJSBridgeReady", jsApiCall);
  131. }
  132. }else{
  133. jsApiCall();
  134. }
  135. }
  136. callpay();
  137. </script>';
  138. return $html;
  139. }
  140. private function getType($type)
  141. {
  142. switch ($type) {
  143. case 1:
  144. $type = 'JSAPI';
  145. break;
  146. case 2:
  147. $type = 'NATIVE';
  148. break;
  149. }
  150. return $type;
  151. }
  152. }
  153. class Callback extends \WxPayNotify
  154. {
  155. public function NotifyProcess($objData, $config, &$msg)
  156. {
  157. $data = $objData->GetValues();
  158. $this->log('支付回调-获取数据', $data);
  159. $obj = Dever::load('pay/lib/core');
  160. $callback = function($msg = '') use ($obj, $data) {
  161. if ($msg) {
  162. $msg = $data['transaction_id'] . ':' . $msg;
  163. }
  164. $obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg);
  165. };
  166. if(!array_key_exists("transaction_id", $data)){
  167. $msg = '输入参数不正确';
  168. $callback($msg);
  169. return false;
  170. }
  171. # 参数校验
  172. if(!array_key_exists("return_code", $data)
  173. ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) {
  174. $msg = $data['return_code'] . '(异常)';
  175. $callback($msg);
  176. return false;
  177. }
  178. # 进行签名验证
  179. try {
  180. $checkResult = $objData->CheckSign($config);
  181. if($checkResult == false){
  182. $msg = '签名错误';
  183. $callback($msg);
  184. return false;
  185. }
  186. } catch(Exception $e) {
  187. $msg = '签名异常';
  188. $callback($msg);
  189. return false;
  190. }
  191. # 查询订单,判断订单真实性
  192. if(!$this->Queryorder($data["transaction_id"])){
  193. $msg = '订单查询失败';
  194. $callback($msg);
  195. return false;
  196. }
  197. # 处理业务逻辑
  198. $callback();
  199. return true;
  200. }
  201. }