Wechat.php 6.2 KB

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