/** * 获取统一下单结果 * * @param bool|false $force 是否忽略缓存强制更新 * * @return array * @throws Exception * @throws \Overtrue\Wechat\Exception */ public function getResponse($force = false) { if (is_null($this->business)) { throw new Exception('Business is required'); } if (is_null($this->order)) { throw new Exception('Order is required'); } if ($this->unifiedOrder !== null && $force === false) { return $this->unifiedOrder; } $params = $this->order->toArray(); $params['appid'] = $this->business->appid; $params['mch_id'] = $this->business->mch_id; $signGenerator = new SignGenerator($params); $signGenerator->onSortAfter(function (SignGenerator $that) { $that->key = $this->business->mch_key; }); $params['sign'] = $signGenerator->getResult(); $request = XML::build($params); $http = new Http(); $response = $http->request(static::UNIFIEDORDER_URL, Http::POST, $request); if (empty($response)) { throw new Exception('Get UnifiedOrder Failure:'); } $unifiedOrder = XML::parse($response); if (isset($unifiedOrder['result_code']) && $unifiedOrder['result_code'] === 'FAIL') { throw new Exception($unifiedOrder['err_code'] . ': ' . $unifiedOrder['err_code_des']); } if (isset($unifiedOrder['return_code']) && $unifiedOrder['return_code'] === 'FAIL') { throw new Exception($unifiedOrder['return_code'] . ': ' . $unifiedOrder['return_msg']); } return $this->unifiedOrder = $unifiedOrder; }
/** * 获取统一下单结果 * * @param bool|false $force 是否忽略缓存强制更新 * * @return array * @throws Exception * @throws \Overtrue\Wechat\Exception */ public function getResponse($force = false) { if (is_null($this->business)) { throw new Exception('Business is required'); } if (is_null($this->order)) { throw new Exception('Order is required'); } if ($this->unifiedOrder !== null && $force === false) { return $this->unifiedOrder; } $params = $this->order->toArray(); $params['appid'] = $this->business->appid; $params['mch_id'] = $this->business->mch_id; ksort($params); $sign = http_build_query($params); $sign = urldecode($sign) . '&key=' . $this->business->mch_key; $sign = strtoupper(md5($sign)); $params['sign'] = $sign; $request = XML::build($params); $http = new Http(new AccessToken($this->business->appid, $this->business->appsecret)); $response = $http->request(static::UNIFIEDORDER_URL, Http::POST, $request); if (empty($response)) { throw new Exception('Get UnifiedOrder Failure:'); } $unifiedOrder = XML::parse($response); if (isset($unifiedOrder['result_code']) && $unifiedOrder['result_code'] === 'FAIL') { throw new Exception($unifiedOrder['err_code'] . ': ' . $unifiedOrder['err_code_des']); } if (isset($unifiedOrder['return_code']) && $unifiedOrder['return_code'] === 'FAIL') { throw new Exception($unifiedOrder['return_code'] . ': ' . $unifiedOrder['return_msg']); } return $this->unifiedOrder = $unifiedOrder; }