config = new \WxPayConfig(); # 通知接口 $config['notify'] = $this->url($config['type']); # 证书 $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() { $callback = new Callback(); $result = $callback->Handle($this->config, false); } /** * 获取统一下单的基本信息 */ public function order($account_id, $uid, $username, $product_id, $name, $cash, $openid = false, $type = 1) { $trade_type = $this->getType($type); $order_id = $this->createOrder($uid, $username, $account_id, $product_id, $name, $cash, $this->config->GetType()); $tools = new \JsApiPay($this->config); $openid = $openid ? $openid : $tools->GetOpenid(); $input = new \WxPayUnifiedOrder(); $input->SetBody($name); $input->SetAttach($name); $input->SetOut_trade_no($order_id); $input->SetTotal_fee($num); $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); $input->SetOpenid($openid); if ($type == 1) { $order = \WxPayApi::unifiedOrder($this->config, $input); # 下单信息 $this->updateOrderParam($order_id, $order); return array($order_id, $order); } else { # 下单信息 $this->updateOrderParam($order_id, $input); return array($order_id, $input); } } /** * 获取二维码支付 */ public function qrcode($order, $refer) { $notify = new \NativePay(); $result = $notify->GetPayUrl($order[1]); $url = $result['code_url']; return $url; } /** * 获取页面支付 */ public function page($order, $refer) { $tools = new \JsApiPay($this->config); $info = $tools->GetJsApiParameters($order[1]); $html = ''; return $html; } private function getType($type) { switch ($type) { case 1: $type = 'JSAPI'; break; case 2: $type = 'NATIVE'; break; } return $type; } } class Callback extends \WxPayNotify { public function NotifyProcess($objData, $config, &$msg) { $data = $objData->GetValues(); $obj = Dever::load('pay/lib/wechat'); $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("return_code", $data) ||(array_key_exists("return_code", $data) && $data['return_code'] != "SUCCESS")) { $msg = '异常'; return false; } if(!array_key_exists("transaction_id", $data)){ $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; } }