Bitpay\Client\Client::createInvoice PHP Method

createInvoice() public method

public createInvoice ( Bitpay\InvoiceInterface $invoice )
$invoice Bitpay\InvoiceInterface
    public function createInvoice(InvoiceInterface $invoice)
    {
        $request = $this->createNewRequest();
        $request->setMethod(Request::METHOD_POST);
        $request->setPath('invoices');
        $currency = $invoice->getCurrency();
        $item = $invoice->getItem();
        $buyer = $invoice->getBuyer();
        $buyerAddress = $buyer->getAddress();
        $this->checkPriceAndCurrency($item->getPrice(), $currency->getCode());
        $body = array('price' => $item->getPrice(), 'currency' => $currency->getCode(), 'posData' => $invoice->getPosData(), 'notificationURL' => $invoice->getNotificationUrl(), 'transactionSpeed' => $invoice->getTransactionSpeed(), 'fullNotifications' => $invoice->isFullNotifications(), 'notificationEmail' => $invoice->getNotificationEmail(), 'redirectURL' => $invoice->getRedirectUrl(), 'orderID' => $invoice->getOrderId(), 'itemDesc' => $item->getDescription(), 'itemCode' => $item->getCode(), 'physical' => $item->isPhysical(), 'buyerName' => trim(sprintf('%s %s', $buyer->getFirstName(), $buyer->getLastName())), 'buyerAddress1' => isset($buyerAddress[0]) ? $buyerAddress[0] : '', 'buyerAddress2' => isset($buyerAddress[1]) ? $buyerAddress[1] : '', 'buyerCity' => $buyer->getCity(), 'buyerState' => $buyer->getState(), 'buyerZip' => $buyer->getZip(), 'buyerCountry' => $buyer->getCountry(), 'buyerEmail' => $buyer->getEmail(), 'buyerPhone' => $buyer->getPhone(), 'guid' => Util::guid(), 'nonce' => Util::nonce(), 'token' => $this->token->getToken());
        $request->setBody(json_encode($body));
        $this->addIdentityHeader($request);
        $this->addSignatureHeader($request);
        $this->request = $request;
        $this->response = $this->sendRequest($request);
        $body = json_decode($this->response->getBody(), true);
        $error_message = false;
        $error_message = !empty($body['error']) ? $body['error'] : $error_message;
        $error_message = !empty($body['errors']) ? $body['errors'] : $error_message;
        $error_message = is_array($error_message) ? implode("\n", $error_message) : $error_message;
        if (false !== $error_message) {
            throw new \Exception($error_message);
        }
        $data = $body['data'];
        $invoiceToken = new \Bitpay\Token();
        $invoice->setToken($invoiceToken->setToken($data['token']))->setId($data['id'])->setUrl($data['url'])->setStatus($data['status'])->setBtcPrice($data['btcPrice'])->setPrice($data['price'])->setInvoiceTime($data['invoiceTime'])->setExpirationTime($data['expirationTime'])->setCurrentTime($data['currentTime'])->setBtcPaid($data['btcPaid'])->setRate($data['rate'])->setExceptionStatus($data['exceptionStatus']);
        return $invoice;
    }

Usage Example

 public function createBitPayInvoice($invoiceList, $amount)
 {
     if ($invoiceList != '' && !empty($amount)) {
         $storageEngine = new Bitpay\Storage\FilesystemStorage();
         if (_BIT_PAY_PRODUCTION_) {
             $privateKey = $storageEngine->load('/tmp/bitpay.pri');
             $publicKey = $storageEngine->load('/tmp/bitpay.pub');
         } else {
             $privateKey = $storageEngine->load('/tmp/bitpaydev.pri');
             $publicKey = $storageEngine->load('/tmp/bitpaydev.pub');
         }
         $client = new Bitpay\Client\Client();
         if (_BIT_PAY_PRODUCTION_) {
             $network = new Bitpay\Network\Livenet();
         } else {
             $network = new Bitpay\Network\Testnet();
         }
         $adapter = new Bitpay\Client\Adapter\CurlAdapter();
         $client->setPrivateKey($privateKey);
         $client->setPublicKey($publicKey);
         $client->setNetwork($network);
         $client->setAdapter($adapter);
         $token = new Bitpay\Token();
         if (_BIT_PAY_PRODUCTION_) {
             $token->setToken(_BIT_PAY_TOKEN_LIVE_NET_);
         } else {
             $token->setToken(_BIT_PAY_TOKEN_TEST_NET_);
         }
         $client->setToken($token);
         $item = new Bitpay\Item();
         $item->setCode($invoiceList);
         $item->setDescription('');
         $item->setPrice($amount);
         $invoice = new Bitpay\Invoice();
         $invoice->setItem($item);
         $invoice->setPosData('{"invoiceList": "' . $invoiceList . '", "amount": "' . $amount . '", "userID": ' . $this->userID . '}');
         $invoice->setNotificationUrl(_DOMAIN_API_HOST_ . '/bit-pay-ipn/');
         $invoice->setCurrency(new Bitpay\Currency('USD'));
         try {
             $client->createInvoice($invoice);
         } catch (\Exception $e) {
             // todo: record failure somewhere? and turn off bitPay until problem is fixed
             //PaymentSystemData::turnOffBitPay(); cant turn it off unless we open up the IPN Need to decide.
             $error = array('Request' => $client->getRequest(), 'Response' => $client->getResponse());
             file_put_contents('bitPayError.txt', print_r($error, true));
         }
         return $invoice->getUrl();
     }
     return false;
 }