/**
  * 获取下单结果
  */
 public function getResponse()
 {
     ksort($this->options);
     $signature = http_build_query($this->options);
     $signature = urldecode($signature);
     $signature = strtoupper(md5($signature . '&key=' . $this->wechat['mchkey']));
     $this->options['sign'] = $signature;
     $request = Http::post(self::UNIFIEDORDER_URL, array('body' => Serialize::encode($this->options, 'xml')));
     $response = (array) $request->xml(array('libxml_options' => LIBXML_NOCDATA));
     if (array_key_exists('result_code', $response) && $response['result_code'] === 'FAIL') {
         throw new \Exception($response['err_code'] . ': ' . $response['err_code_des']);
     }
     if (array_key_exists('return_code', $response) && $response['return_code'] === 'FAIL') {
         throw new \Exception($response['return_code'] . ': ' . $response['return_msg']);
     }
     return $response;
 }
Exemple #2
0
 /**
  * 发送模板消息
  */
 public function send()
 {
     if (empty($this->options) || $this->options->isEmpty()) {
         throw new \Exception('Options is requried');
     }
     $body = array('touser' => $this->touser, 'template_id' => $this->templateId, 'url' => $this->url, 'data' => $this->options->toArray());
     $request = Http::post(self::SEND_URL, array('body' => Serialize::encode($body, 'json'), 'query' => array('access_token' => $this->accessToken->getAccessToken())));
     $response = $request->json();
     if (array_key_exists('errcode', $response) && $response['errcode'] != 0) {
         throw new \Exception($response['errcode'] . ': ' . $response['errmsg']);
     }
     return $response['msgid'];
 }
Exemple #3
0
 /**
  * 获取统一下单结果
  */
 public function getResponse()
 {
     $signGenerator = new SignGenerator($this->bag);
     $signGenerator->onSortAfter(function ($bag) {
         $bag->set('key', $this->key);
     });
     // 生成签名
     $sign = $signGenerator->getResult();
     // 生成签名后移除 Key
     $this->bag->remove('key');
     // 调置签名
     $this->bag->set('sign', $sign);
     $body = Util::array2XML($this->bag->all());
     $response = Http::post(static::UNIFIEDORDER_URL, $body, false);
     $response = Util::XML2Array($response);
     if (isset($response['result_code']) && $response['result_code'] === 'FAIL') {
         throw new PaymentException($response['err_code'] . ': ' . $response['err_code_des']);
     }
     if (isset($response['return_code']) && $response['return_code'] === 'FAIL') {
         throw new PaymentException($response['return_code'] . ': ' . $response['return_msg']);
     }
     return $response;
 }