Ejemplo n.º 1
0
 /**
  * @inheritdoc
  */
 public function createToken(array $payload = array())
 {
     if (isset($payload['pairingCode']) && 1 !== preg_match('/^[a-zA-Z0-9]{7}$/', $payload['pairingCode'])) {
         throw new \Exception('[ERROR] In Client::createToken(): The pairing code provided is not legal.');
     }
     $this->request = $this->createNewRequest();
     $this->request->setMethod(Request::METHOD_POST);
     $this->request->setPath('tokens');
     $payload['guid'] = Util::guid();
     $this->request->setBody(json_encode($payload));
     $this->response = $this->sendRequest($this->request);
     $body = json_decode($this->response->getBody(), true);
     if (isset($body['error'])) {
         throw new \Bitpay\Client\BitpayException($this->response->getStatusCode() . ': ' . $body['error']);
     }
     $tkn = $body['data'][0];
     $createdAt = new \DateTime();
     $pairingExpiration = new \DateTime();
     $token = new \Bitpay\Token();
     $token->setPolicies($tkn['policies'])->setToken($tkn['token'])->setFacade($tkn['facade'])->setCreatedAt($createdAt->setTimestamp(floor($tkn['dateCreated'] / 1000)));
     if (isset($tkn['resource'])) {
         $token->setResource($tkn['resource']);
     }
     if (isset($tkn['pairingCode'])) {
         $token->setPairingCode($tkn['pairingCode']);
         $token->setPairingExpiration($pairingExpiration->setTimestamp(floor($tkn['pairingExpiration'] / 1000)));
     }
     return $token;
 }