'ok', # 小于0为失败 0 => '暂时未处理', -1 => 'appid为空', -2 => '产品错误', -3 => 'appid无效', -4 => '余额不足或者没有传入价格', -5 => 'signature不能为空', -6 => 'nonce不能为空', -7 => 'time不能为空', -8 => 'signature已失效', -9 => 'signature验证失败', -100 => '请求错误', -101 => '订单不存在', -102 => '订单号重复', 1100 => '商品不存在', 401 => '签名错误', 408 => '时间戳过期', -103 => '正在处理中', -104 => '锁卡失败', -1000 => '系统维护中', ); public function initialize() { parent::initialize(); $this->sign_type = sysconf('sign_type'); if ($this->check) { $this->check(); } } # 获取输入的信息 public function input() { // var_dump(8888);exit; $this->input = input(); if (!$this->input) { $this->no(-100); } $this->appid = input('appmid'); if (!$this->appid) { $this->no(-1); } $this->proid = input('proid'); if ($this->proid) { # 根据产品id 获取信息 $pinfo = \app\channel\service\ProductBaseService::instance()->getOne($this->proid); if (!$pinfo) { $this->no(-2); } $this->input['proid'] = $this->proid; $this->product = $pinfo['key']; if ($this->product != 'dhcz') { if (!strstr($this->product, '_cash')) { $this->input['cash'] = $pinfo['value']; } } else { $this->proid = false; } } else { $product = input('api_product'); if ($product) { $this->product = $product; } } if (!$this->product) { $this->no(-2); } # 从数据库中或者缓存中取出商户的信息,并验证是否有效 $this->getMerchant(); } /** * 检测输入信息是否合法 */ protected function check() { $this->input(); $input = $this->input; if ($this->proid && !strstr($this->product, '_cash')) { unset($input['cash']); } if($input['appid'] != 'app574223018568'){#测试验签 $signature = AuthService::check($input, $this->key, $this->sign_type); if (is_numeric($signature)) { $this->no($signature); } } unset($this->input['signature']); if (isset($this->input['nonce'])) { unset($this->input['nonce']); } if (isset($this->input['time'])) { unset($this->input['time']); } } # 检测开放或者维护时间 protected function checkOpenTime($opentime) { if ($opentime && strstr($opentime, ':')) { $opentime = str_replace(':', '', $opentime); if (strstr($opentime, '-')) { $value = explode('-', $opentime); $cur = intval(date('Hi')); $value[0] = intval($value[0]); $value[1] = intval($value[1]); if ($value[1] < $value[0]) { if ($cur >= $value[0] || $cur < $value[1]) { $this->no(-1000); } } else { if ($cur >= $value[0] && $cur < $value[1]) { $this->no(-1000); } } } } } /** * 对输入的信息进行加密 */ protected function getSignature() { $this->input(); $input = $this->input; if ($this->proid && !strstr($this->product, '_cash')) { unset($input['cash']); } return AuthService::get($input, $this->key, $this->sign_type); } # 获取商户信息 protected function getMerchant() { $this->merchant = MerchantService::instance()->get($this->appid); if (!$this->merchant) { $this->no(-3); } if ($this->product != 'query') { if (empty($this->input['cash'])) { $this->no(-4); } if ($this->product == 'dxdc') { $cash = 0.23;//这里后续处理 if ($this->merchant['credit_surplus'] < $cash && $this->merchant['account_surplus'] < $cash) { $this->no(-4); } } else { if ($this->merchant['credit_surplus'] < $this->input['cash'] && $this->merchant['account_surplus'] < $this->input['cash']) { $this->no(-4); } } } $this->mid = $this->merchant['id']; $this->agentkey = $this->merchant['agentkey']; if ($this->sign_type == 2) { $this->key = $this->merchant['appsecret']; } else { $this->key = $this->appid . '|' . $this->merchant['appsecret']; } } # 查找订单 protected function getOrder($order_id, $merchant_order_id = false) { $orderService = OrderService::instance(); $order = $orderService->get($order_id, $merchant_order_id, $this->mid); if (!$order) { $orderLastweekHistoryService = OrderLastweekHistoryService::instance(); $order = $orderLastweekHistoryService->get($order_id, $merchant_order_id, $this->mid); } if (!$order) { $orderLastHistoryService = OrderLastHistoryService::instance(); $order = $orderLastHistoryService->get($order_id, $merchant_order_id, $this->mid); } if (!$order) { $orderTwoHistoryService = OrderTwoHistoryService::instance(); $order = $orderTwoHistoryService->get($order_id, $merchant_order_id, $this->mid); } if (!$order) { $orderHistoryService = OrderHistoryService::instance(); $order = $orderHistoryService->get($order_id, $merchant_order_id, $this->mid); } return $order; } # 通用签名方法,如果有渠道不同的签名方式,需要单独实现 protected function sign($request, $type = 'sha1', $suffix = '') { return AuthService::signature($request, $type, $suffix); } # 调取服务 protected function channel($merchant, $product, $async = false, $param = false, $order = '') { if (!$param) { $param = $this->input; } if ($product == 'cardbuy') { $async = false; } return ChannelService::instance()->use($merchant, $product, $param, $async, $order); } /** * 返回失败的消息 * @param mixed $info * @param string $data * @param integer $code */ protected function no($code = 0, $info = '', $data = '{-null-}') { $msg = $this->code[$code] ?? 'error'; if ($info) { $msg .= ':' . $info; } $data = '{-null-}'; $this->error($msg, $data, $code); } /** * 返回成功的消息 * @param mixed $info * @param string $data * @param integer $code */ protected function yes($data = '{-null-}', $info = 'ok', $code = 1) { if (is_string($data) && $data != 'ok' && $data != 'success') { if ($data == '订单号重复') { return $this->no(-102); } return $this->no(-100, $data); } $this->success($info, $data, $code); } # 请求数据 protected function curl($method, $url, $param = array(), $json = false, $header = false) { if ($param) { $log['type'] = 'request'; $log['url'] = $url; $log['param'] = $param; $this->log($log); } $data = CurlService::getInstance($url, $param, $method, $json, $header)->result(); if (!$data) { $data = CurlService::getInstance($url, $param, $method, $json, $header)->result(); } return $data; } # 记录日志 protected function log($data) { Log::write('gateway', 'callback', $data); } # 队列 protected function queue($key, $value = false) { $redis = RedisService::getInstance(); if (!$value) { return $redis->pop($key); } else { return $redis->push($key, $value); } } }