config = new \WxPayConfig(); # 通知接口 $config['notify'] = $this->url($config['type'], $config['id']); # 证书 $config['ssl'] = array ( 'cert' => $config['file_cert'], 'key' => $config['file_key'], ); $this->config->set($config['appid'], $config['appsecret'], $config['mchid'], $config['notify'], $config['key'], $config['ssl'], $config['type'], $config['timeout']); } /** * 通知 */ public function notify() { $this->log('支付回调-初始化', file_get_contents("php://input")); $callback = new Callback(); $result = $callback->Handle($this->config, false); } /** * 获取统一下单的基本信息 */ public function order($account_id, $project_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1, $order_id = false) { $trade_type = $this->getType($type); $order_id = $this->createOrder($uid, $username, $account_id, $project_id, $product_id, $name, $cash, $this->config->GetType(), $order_id); $tools = new \JsApiPay($this->config); if ($openid != -1) { $openid = $openid ? $openid : $tools->GetOpenid(); } $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetAttach($name); $input->SetOut_trade_no($order_id); $input->SetTotal_fee($cash * 100); $input->SetTime_start(date("YmdHis")); $input->SetTime_expire(date("YmdHis", time() + $this->config->GetTimeOut())); //$input->SetGoods_tag($name); $input->SetNotify_url($this->config->GetNotifyUrl()); $input->SetTrade_type($trade_type); $input->SetProduct_id($product_id); if ($openid != -1) { $input->SetOpenid($openid); } if ($type == 1 || $type == 3) { $order = \WxPayApi::unifiedOrder($this->config, $input); # 下单信息 $order['time'] = '' . time() . ''; $order['order_id'] = $order_id; $order['sign_type'] = $this->config->GetSignType(); unset($order['mch_id']); $this->updateOrderParam($order_id, $order); return $order; } else { # 下单信息 $this->updateOrderParam($order_id, $input); return $input; } } /** * 获取二维码支付 */ public function qrcode($order, $refer) { $notify = new \NativePay(); $result = $notify->GetPayUrl($order); $url = $result['code_url']; return $url; } /** * 获取小程序支付 */ public function applet($order) { if (isset($order['prepay_id'])) { $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(); $this->log('支付签名-applet', $string); if($order['sign_type'] == "MD5"){ $string = md5($string); } else { $string = hash_hmac("sha256", $string, $this->config->GetKey()); } $order['sign'] = $string; } return $order; } /** * 获取app支付 */ public function app($order) { if (isset($order['prepay_id'])) { $order['partnerid'] = $this->config->GetMerchantId(); $order['package_string'] = 'Sign=WXPay'; $string = array(); $string['appid'] = $this->config->GetAppId(); $string['partnerid'] = $order['partnerid']; $string['prepayid'] = $order['prepay_id']; $string['package'] = $order['package_string']; $string['noncestr'] = $order['nonce_str']; $string['timestamp'] = $order['time']; ksort($string); $string = str_replace('%3D', '=', http_build_query($string)); $string .= '&key=' . $this->config->GetKey(); $this->log('支付签名-app', $string); if($order['sign_type'] == "MD5"){ $string = md5($string); } else { $string = hash_hmac("sha256", $string, $this->config->GetKey()); } $order['sign'] = $string; } return $order; } /** * 获取页面支付 */ public function page($order, $refer) { $refer = urldecode($refer); $tools = new \JsApiPay($this->config); $info = $tools->GetJsApiParameters($order); $html = ''; return $html; } private function getType($type) { switch ($type) { case 1: $type = 'JSAPI'; break; case 2: $type = 'NATIVE'; break; case 3: $type = 'APP'; break; } return $type; } } class Callback extends \WxPayNotify { public function NotifyProcess($objData, $config, &$msg) { $data = $objData->GetValues(); $obj = Dever::load('pay/lib/core'); $obj->log('支付回调-获取数据', $data); $callback = function($msg = '') use ($obj, $data) { if ($msg) { $msg = $data['transaction_id'] . ':' . $msg; } $obj->updateOrder($data['out_trade_no'], $data['cash_fee'], $msg); }; if(!array_key_exists("transaction_id", $data)){ $msg = '输入参数不正确'; $callback($msg); return false; } # 参数校验 if(!array_key_exists("return_code", $data) ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) { $msg = $data['return_code'] . '(异常)'; $callback($msg); return false; } # 进行签名验证 try { $checkResult = $objData->CheckSign($config); if($checkResult == false){ $msg = '签名错误'; $callback($msg); return false; } } catch(Exception $e) { $msg = '签名异常'; $callback($msg); return false; } # 查询订单,判断订单真实性 /* if(!$this->Queryorder($data["transaction_id"])){ $msg = '订单查询失败'; $callback($msg); return false; } */ # 处理业务逻辑 $callback(); return true; } }