/** * 合并和校验参数 */ public function resolveOptions() { $defaults = array('nonce_str' => Util::getRandomString(), 'client_ip' => Util::getClientIp()); $resolver = new OptionsResolver(); $resolver->setDefined($this->required)->setRequired($this->required)->setDefaults($defaults); return $resolver->resolve($this->toArray()); }
/** * 获取授权 URL */ public function getAuthorizeUrl() { if (null === $this->state) { $this->state = Util::getRandomString(16); } $this->stateManager->setState($this->state); $query = array('appid' => $this->appid, 'redirect_uri' => $this->redirectUri ?: Util::getCurrentUrl(), 'response_type' => 'code', 'scope' => $this->resolveScope(), 'state' => $this->state); return $this->resolveAuthorizeUrl() . '?' . http_build_query($query); }
/** * 获取配置 */ public function getConfig($asArray = false) { $options = array('appid' => $this->accessToken->getAppid(), 'url' => Util::getCurrentUrl(), 'timestamp' => Util::getTimestamp(), 'noncestr' => Util::getRandomString(), 'accesstoken' => $this->accessToken['access_token']); // 按 ASCII 码排序 ksort($options); $signature = http_build_query($options); $signature = urldecode($signature); $signature = sha1($signature); $config = array('appId' => $options['appid'], 'scope' => 'jsapi_address', 'signType' => 'sha1', 'addrSign' => $signature, 'timeStamp' => $options['timestamp'], 'nonceStr' => $options['noncestr']); return $asArray ? $config : Serializer::jsonEncode($config); }
/** * 成功响应 */ public function success(Unifiedorder $unifiedorder) { $unifiedorder->set('trade_type', 'NATIVE'); $response = $unifiedorder->getResponse(); $options = array('appid' => $unifiedorder['appid'], 'mch_id' => $unifiedorder['mch_id'], 'prepay_id' => $response['prepay_id'], 'nonce_str' => Util::getRandomString(), 'return_code' => static::SUCCESS, 'result_code' => static::SUCCESS); // 按 ASCII 码排序 ksort($options); $signature = urldecode(http_build_query($options)); $signature = strtoupper(md5($signature . '&key=' . $unifiedorder->getKey())); $options['sign'] = $signature; $this->xmlResponse($options); }
/** * 获取配置文件 */ public function getConfig($asArray = false) { $ticket = new Ticket($this->accessToken); if ($this->cache) { $ticket->setCache($this->cache); } $options = array('jsapi_ticket' => $ticket->getTicketString(), 'timestamp' => Util::getTimestamp(), 'url' => Util::getCurrentUrl(), 'noncestr' => Util::getRandomString()); ksort($options); $signature = sha1(urldecode(http_build_query($options))); $configure = array('appId' => $this->accessToken['appid'], 'nonceStr' => $options['noncestr'], 'timestamp' => $options['timestamp'], 'signature' => $signature, 'jsApiList' => $this->api, 'debug' => (bool) $this->debug); return $asArray ? $configure : Serializer::jsonEncode($configure); }
/** * 构造方法 */ public function __construct(Unifiedorder $unifiedorder, array $defaults = array()) { $res = $unifiedorder->getResponse(); $key = $unifiedorder->getKey(); $config = array('appId' => $unifiedorder['appid'], 'timeStamp' => Util::getTimestamp(), 'nonceStr' => Util::getRandomString(), 'package' => 'prepay_id=' . $res['prepay_id'], 'signType' => 'MD5'); // 如果需要指定以上参数,可以通过 $defaults 变量传入 $options = array_replace($config, $defaults); ksort($options); $queryString = urldecode(http_build_query($options)); $paySign = strtoupper(md5($queryString . '&key=' . $key)); $options['paySign'] = $paySign; parent::__construct($options); }
/** * 合并和校验参数 */ public function resolveOptions() { $normalizer = function ($options, $value) { if ($value === 'JSAPI' && !isset($options['openid'])) { throw new \InvalidArgumentException(sprintf('订单的 trade_type 为 “%s” 时,必需指定 “openid” 字段', $value)); } return $value; }; $defaults = array('trade_type' => current($this->tradeTypes), 'spbill_create_ip' => Util::getClientIp(), 'nonce_str' => Util::getRandomString()); $resolver = new OptionsResolver(); $resolver->setDefined($this->defined)->setRequired($this->required)->setAllowedValues('trade_type', $this->tradeTypes)->setNormalizer('trade_type', $normalizer)->setDefaults($defaults); return $resolver->resolve($this->toArray()); }
/** * 获取支付链接 */ public function getPayurl($productId, array $defaults = array()) { $defaultOptions = array('appid' => $this['appid'], 'mch_id' => $this['mch_id'], 'time_stamp' => Util::getTimestamp(), 'nonce_str' => Util::getRandomString()); $options = array_replace($defaultOptions, $defaults); $options['product_id'] = $productId; // 按 ASCII 码排序 ksort($options); $signature = urldecode(http_build_query($options)); $signature = strtoupper(md5($signature . '&key=' . $this->key)); $options['sign'] = $signature; $query = http_build_query($options); return self::PAYMENT_URL . '?' . urlencode($query); }
/** * 查询订单 */ public function doQuery(array $by) { $options = array_merge($this->toArray(), $by); $options['nonce_str'] = Util::getRandomString(); // 按 ASCII 码排序 ksort($options); $signature = urldecode(http_build_query($options)); $signature = strtoupper(md5($signature . '&key=' . $this->key)); $options['sign'] = $signature; $response = Http::request('POST', static::QUERY)->withXmlBody($options)->send(); if ($response['result_code'] === 'FAIL') { throw new \Exception($response['err_code_des']); } if ($response['return_code'] === 'FAIL') { throw new \Exception($response['return_msg']); } return $response; }