public function testCreateInvoice()
 {
     $buyer = $this->getMockBuyer();
     $buyer->method('getAddress')->will($this->returnValue(array()));
     $currency = $this->getMockCurrency();
     $currency->method('getCode')->will($this->returnValue('USD'));
     $invoice = new \Bitpay\Invoice();
     $invoice->setOrderId('TEST-01');
     $invoice->setCurrency($currency);
     $item = new \Bitpay\Item();
     $item->setPrice('19.95');
     $invoice->setItem($item);
     $response = $this->getMockResponse();
     $response->method('getBody')->willReturn(file_get_contents(__DIR__ . '/../../DataFixtures/invoice.json'));
     $adapter = $this->getMockAdapter();
     $adapter->method('sendRequest')->willReturn($response);
     $this->client->setAdapter($adapter);
     $invoice = $this->client->createInvoice($invoice);
     $this->assertInstanceOf('Bitpay\\InvoiceInterface', $invoice);
     $this->assertEquals('abcdefghijkmnopqrstuvw', $invoice->getId());
     $this->assertEquals('https://test.bitpay.com/invoice?id=abcdefghijkmnopqrstuvw', $invoice->getUrl());
     $this->assertEquals('new', $invoice->getStatus());
     $this->assertEquals('0.0632', $invoice->getBtcPrice());
     $this->assertEquals(19.95, $invoice->getPrice());
     $this->assertEquals(1412594514486.0, $invoice->getInvoiceTime());
     $this->assertEquals(1412595414486.0, $invoice->getExpirationTime());
     $this->assertEquals(1412594514518.0, $invoice->getCurrentTime());
     $this->assertEquals('0.0000', $invoice->getBtcPaid());
     $this->assertEquals(315.7, $invoice->getRate());
     $this->assertEquals(false, $invoice->getExceptionStatus());
     $this->assertEquals('abcdefghijklmno', $invoice->getToken()->getToken());
 }
Ejemplo n.º 2
0
 /**
  * Prepares an Invoice to send to BitPay
  *
  * @param array $order_info OpenCart checkout order
  * @return Invoice
  */
 private function prepareInvoice($order_info = array())
 {
     $invoice = new \Bitpay\Invoice();
     if (empty($order_info['order_id'])) {
         $this->log('error', 'Cannot prepare invoice without `order_id`');
         throw Exception('Cannot prepare invoice without `order_id`');
     }
     $this->log('info', sprintf('Preparing Invoice for Order %s', (string) $order_info['order_id']));
     $invoice->setOrderId((string) $order_info['order_id']);
     if (empty($order_info['currency_code'])) {
         $this->log('error', 'Cannot prepare invoice without `currency_code`');
         throw Exception('Cannot prepare invoice without `currency_code`');
     }
     $invoice->setCurrency(new \Bitpay\Currency($order_info['currency_code']));
     if (empty($order_info['total'])) {
         $this->log('error', 'Cannot prepare invoice without `total`');
         throw Exception('Cannot prepare invoice without `total`');
     }
     $invoice->setPrice((double) $order_info['total']);
     // Send Buyer Information?
     if ($this->setting('send_buyer_info')) {
         $buyer = new \Bitpay\Buyer();
         $buyer->setFirstName($order_info['firstname'])->setLastName($order_info['lastname'])->setEmail($order_info['email'])->setPhone($order_info['telephone'])->setAddress(array($order_info['payment_address_1'], $order_info['payment_address_2']))->setCity($order_info['payment_city'])->setState($order_info['payment_zone_code'])->setZip($order_info['payment_postcode'])->setCountry($order_info['payment_country']);
         $invoice->setBuyer($buyer);
     }
     $invoice->setFullNotifications(true);
     $return_url = $this->setting('return_url');
     if (empty($return_url)) {
         $return_url = $this->url->link('payment/bitpay/success', $this->config->get('config_secure'));
     }
     $notify_url = $this->setting('notify_url');
     if (empty($notify_url)) {
         $notify_url = $this->url->link('payment/bitpay/callback', $this->config->get('config_secure'));
     }
     $invoice->setRedirectUrl($return_url);
     $invoice->setNotificationUrl($notify_url);
     $invoice->setTransactionSpeed($this->setting('risk_speed'));
     return $invoice;
 }
Ejemplo n.º 3
0
 /**
  * Adds currency information to the invoice
  *
  * @param Bitpay\Invoice         $invoice
  * @param Mage_Sales_Model_Order $order
  * @return Bitpay\Invoice
  */
 private function addCurrencyInfo($invoice, $order)
 {
     if (false === isset($invoice) || true === empty($invoice) || false === isset($order) || true === empty($order)) {
         $this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): missing or invalid invoice or order parameter.');
         throw new \Exception('In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): missing or invalid invoice or order parameter.');
     } else {
         $this->debugData('[INFO] In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): function called with good invoice and order parameters.');
     }
     $currency = new Bitpay\Currency();
     if (false === isset($currency) || true === empty($currency)) {
         $this->debugData('[ERROR] In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): could not construct new BitPay currency object.');
         throw new \Exception('In Bitpay_Core_Model_Method_Bitcoin::addCurrencyInfo(): could not construct new BitPay currency object.');
     }
     $currency->setCode($order->getOrderCurrencyCode());
     $invoice->setCurrency($currency);
     return $invoice;
 }
Ejemplo n.º 4
0
 /**
  * @inheritdoc
  */
 public function getInvoice($invoiceId)
 {
     $this->request = $this->createNewRequest();
     $this->request->setMethod(Request::METHOD_GET);
     if ($this->token->getFacade() === 'merchant') {
         $this->request->setPath(sprintf('invoices/%s?token=%s', $invoiceId, $this->token->getToken()));
         $this->addIdentityHeader($this->request);
         $this->addSignatureHeader($this->request);
     } else {
         $this->request->setPath(sprintf('invoices/%s', $invoiceId));
     }
     $this->response = $this->sendRequest($this->request);
     $body = json_decode($this->response->getBody(), true);
     if (isset($body['error'])) {
         throw new \Exception($body['error']);
     }
     $data = $body['data'];
     $invoice = new \Bitpay\Invoice();
     $invoiceToken = new \Bitpay\Token();
     $invoice->setToken($invoiceToken->setToken($data['token']))->setUrl($data['url'])->setPosData($data['posData'])->setStatus($data['status'])->setBtcPrice($data['btcPrice'])->setPrice($data['price'])->setCurrency(new \Bitpay\Currency($data['currency']))->setOrderId($data['orderId'])->setInvoiceTime($data['invoiceTime'] / 1000)->setExpirationTime($data['expirationTime'] / 1000)->setCurrentTime($data['currentTime'] / 1000)->setId($data['id'])->setBtcPaid($data['btcPaid'])->setRate($data['rate'])->setExceptionStatus($data['exceptionStatus']);
     return $invoice;
 }
Ejemplo n.º 5
0
 /**
  * Adds data to model based on an Invoice that has been retrieved from
  * BitPay's API
  *
  * @param Bitpay\Invoice $invoice
  * @return Bitpay_Core_Model_Invoice
  */
 public function prepareWithBitpayInvoice($invoice)
 {
     if (false === isset($invoice) || true === empty($invoice)) {
         \Mage::helper('bitpay')->debugData('[ERROR] In Bitpay_Core_Model_Invoice::prepareWithBitpayInvoice(): Missing or empty $invoice parameter.');
         throw new \Exception('In Bitpay_Core_Model_Invoice::prepareWithBitpayInvoice(): Missing or empty $invoice parameter.');
     }
     $this->addData(array('id' => $invoice->getId(), 'url' => $invoice->getUrl(), 'pos_data' => $invoice->getPosData(), 'status' => $invoice->getStatus(), 'btc_price' => $invoice->getBtcPrice(), 'price' => $invoice->getPrice(), 'currency' => $invoice->getCurrency()->getCode(), 'order_id' => $invoice->getOrderId(), 'invoice_time' => intval($invoice->getInvoiceTime() / 1000), 'expiration_time' => intval($invoice->getExpirationTime() / 1000), 'current_time' => intval($invoice->getCurrentTime() / 1000), 'btc_paid' => $invoice->getBtcPaid(), 'rate' => $invoice->getRate(), 'exception_status' => $invoice->getExceptionStatus()));
     return $this;
 }
Ejemplo n.º 6
0
 /**
  * @When /^the user creates an invoice for "([^"]*)" "([^"]*)"$/
  */
 public function theUserCreatesAnInvoiceFor($price, $currency)
 {
     try {
         // Load keys
         list($privateKey, $publicKey, $token_id) = loadKeys();
         $network = $this->network;
         $client = createClient($network, $privateKey, $publicKey);
         $token = new \Bitpay\Token();
         $token->setToken($token_id);
         $client->setToken($token);
         $invoice = new \Bitpay\Invoice();
         $item = new \Bitpay\Item();
         $item->setCode('skuNumber')->setDescription('General Description of Item')->setPrice($price);
         $invoice->setItem($item);
         $invoice->setCurrency(new \Bitpay\Currency($currency));
         $client->createInvoice($invoice);
         $this->response = $client->getResponse();
         $body = $this->response->getBody();
         $json = json_decode($body, true);
         $this->invoiceId = $json['data']['id'];
     } catch (\Exception $e) {
         $this->error = $e;
     } finally {
         return true;
     }
 }
Ejemplo n.º 7
0
 /**
  *
  *  Method used by payment gateway.
  *
  *  If this method return a \Thelia\Core\HttpFoundation\Response instance, this response is send to the
  *  browser.
  *
  *  In many cases, it's necessary to send a form to the payment gateway. On your response you can return this form already
  *  completed, ready to be sent
  *
  * @param  \Thelia\Model\Order $order processed order
  * @return null|\Thelia\Core\HttpFoundation\Response
  */
 public function pay(Order $order)
 {
     $this->loadBitpayKeys();
     $client = new \Bitpay\Client\Client();
     $adapter = new \Bitpay\Client\Adapter\CurlAdapter();
     $config = new BitpayPaymentsConfig();
     $config->pushValues();
     if ($config->getSandbox()) {
         $pairingKey = $config->getPairingKeySandbox();
         $apiKey = $config->getApiKeySandbox();
         $network = new \Bitpay\Network\Testnet();
         $environment = "Sandbox";
     } else {
         $pairingKey = $config->getPairingKey();
         $apiKey = $config->getApiKey();
         $network = new \Bitpay\Network\Livenet();
         $environment = "Live";
     }
     $client->setPrivateKey($this->privateKey);
     $client->setPublicKey($this->publicKey);
     $client->setNetwork($network);
     $client->setAdapter($adapter);
     if (!isset($apiKey) || $apiKey == '') {
         // must create API key
         if (!isset($pairingKey) || $pairingKey == '') {
             // error: no pairing key
             $error = "Thelia BitpayPayments error: No API key or pairing key for environment {$environment} provided.";
             Tlog::getInstance()->error($error);
             throw new \Exception($error);
         } else {
             // pairing key available, now trying to get an API key
             $sin = \Bitpay\SinKey::create()->setPublicKey($this->publicKey)->generate();
             try {
                 $token = $client->createToken(array('pairingCode' => $pairingKey, 'label' => 'Thelia BitpayPayments', 'id' => (string) $sin));
             } catch (\Exception $e) {
                 $request = $client->getRequest();
                 $response = $client->getResponse();
                 $error = 'Thelia BitpayPayments error:' . PHP_EOL . PHP_EOL . $request . PHP_EOL . PHP_EOL . $response . PHP_EOL . PHP_EOL;
                 Tlog::getInstance()->error($error);
                 throw new \Exception($error);
             }
             $config->setApiKeyCurrentEnvironment($token->getToken());
             $config->setPairingKeyCurrentEnvironment('');
         }
     }
     // token should be available now
     $token = new \Bitpay\Token();
     $token->setToken($config->getApiKeyCurrentEnvironment());
     $client->setToken($token);
     $invoice = new \Bitpay\Invoice();
     $item = new \Bitpay\Item();
     $item->setCode('testCode');
     $item->setDescription('Purchase');
     $item->setPrice($order->getTotalAmount());
     $invoice->setItem($item);
     $invoice->setCurrency(new \Bitpay\Currency($order->getCurrency()->getCode()));
     try {
         $client->createInvoice($invoice);
     } catch (\Exception $e) {
         $request = $client->getRequest();
         $response = $client->getResponse();
         $error = 'Thelia BitpayPayments error:' . PHP_EOL . PHP_EOL . $request . PHP_EOL . PHP_EOL . $response . PHP_EOL . PHP_EOL;
         Tlog::getInstance()->error($error);
         throw new \Exception($error);
     }
 }
 /**
  * Process the payment and return the result
  *
  * @param   int     $order_id
  * @return  array
  */
 public function process_payment($order_id)
 {
     $this->log('    [Info] Entered process_payment() with order_id = ' . $order_id . '...');
     if (true === empty($order_id)) {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but the order_id was missing.');
         throw new \Exception('The Bitpay payment plugin was called to process a payment but the order_id was missing. Cannot continue!');
     }
     $order = wc_get_order($order_id);
     if (false === $order) {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not retrieve the order details for order_id ' . $order_id);
         throw new \Exception('The Bitpay payment plugin was called to process a payment but could not retrieve the order details for order_id ' . $order_id . '. Cannot continue!');
     }
     $this->log('    [Info] Generating payment form for order ' . $order->get_order_number() . '. Notify URL: ' . $this->notification_url);
     // Mark new order according to user settings (we're awaiting the payment)
     $new_order_states = $this->get_option('order_states');
     $new_order_status = $new_order_states['new'];
     $order->update_status($new_order_status, 'Awaiting payment notification from BitPay.');
     $thanks_link = $this->get_return_url($order);
     $this->log('    [Info] The variable thanks_link = ' . $thanks_link . '...');
     // Redirect URL & Notification URL
     $redirect_url = $this->get_option('redirect_url', $thanks_link);
     $this->log('    [Info] The variable redirect_url = ' . $redirect_url . '...');
     $notification_url = $this->get_option('notification_url', WC()->api_request_url('WC_Gateway_Bitpay'));
     $this->log('    [Info] Notification URL is now set to: ' . $notification_url . '...');
     // Setup the currency
     $currency_code = get_woocommerce_currency();
     $this->log('    [Info] The variable currency_code = ' . $currency_code . '...');
     $currency = new \Bitpay\Currency($currency_code);
     if (false === isset($currency) && true === empty($currency)) {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not instantiate a Currency object.');
         throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate a Currency object. Cannot continue!');
     }
     // Get a BitPay Client to prepare for invoice creation
     $client = new \Bitpay\Client\Client();
     if (false === isset($client) && true === empty($client)) {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not instantiate a client object.');
         throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate a client object. Cannot continue!');
     }
     if ('livenet' === $this->api_network) {
         $client->setNetwork(new \Bitpay\Network\Livenet());
         $this->log('    [Info] Set network to Livenet...');
     } else {
         $client->setNetwork(new \Bitpay\Network\Testnet());
         $this->log('    [Info] Set network to Testnet...');
     }
     $curlAdapter = new \Bitpay\Client\Adapter\CurlAdapter();
     if (false === isset($curlAdapter) || true === empty($curlAdapter)) {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not instantiate a CurlAdapter object.');
         throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate a CurlAdapter object. Cannot continue!');
     }
     $client->setAdapter($curlAdapter);
     if (false === empty($this->api_key)) {
         $client->setPrivateKey($this->api_key);
     } else {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not set client->setPrivateKey to this->api_key. The empty() check failed!');
         throw new \Exception(' The Bitpay payment plugin was called to process a payment but could not set client->setPrivateKey to this->api_key. The empty() check failed!');
     }
     if (false === empty($this->api_pub)) {
         $client->setPublicKey($this->api_pub);
     } else {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not set client->setPublicKey to this->api_pub. The empty() check failed!');
         throw new \Exception(' The Bitpay payment plugin was called to process a payment but could not set client->setPublicKey to this->api_pub. The empty() check failed!');
     }
     if (false === empty($this->api_token)) {
         $client->setToken($this->api_token);
     } else {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not set client->setToken to this->api_token. The empty() check failed!');
         throw new \Exception(' The Bitpay payment plugin was called to process a payment but could not set client->setToken to this->api_token. The empty() check failed!');
     }
     $this->log('    [Info] Key and token empty checks passed.  Parameters in client set accordingly...');
     // Setup the Invoice
     $invoice = new \Bitpay\Invoice();
     if (false === isset($invoice) || true === empty($invoice)) {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not instantiate an Invoice object.');
         throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate an Invoice object. Cannot continue!');
     } else {
         $this->log('    [Info] Invoice object created successfully...');
     }
     $order_number = $order->get_order_number();
     $invoice->setOrderId((string) $order_number);
     $invoice->setCurrency($currency);
     $invoice->setFullNotifications(true);
     // Add a priced item to the invoice
     $item = new \Bitpay\Item();
     if (false === isset($item) || true === empty($item)) {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not instantiate an item object.');
         throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate an item object. Cannot continue!');
     } else {
         $this->log('    [Info] Item object created successfully...');
     }
     if (true === isset($order->order_total) && false === empty($order->order_total)) {
         $item->setPrice($order->order_total);
     } else {
         $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not set item->setPrice to order->order_total. The empty() check failed!');
         throw new \Exception('The Bitpay payment plugin was called to process a payment but could not set item->setPrice to order->order_total. The empty() check failed!');
     }
     $invoice->setItem($item);
     // Add the Redirect and Notification URLs
     $invoice->setRedirectUrl($redirect_url);
     $invoice->setNotificationUrl($notification_url);
     $invoice->setTransactionSpeed($this->transaction_speed);
     try {
         $this->log('    [Info] Attempting to generate invoice for ' . $order->get_order_number() . '...');
         $invoice = $client->createInvoice($invoice);
         if (false === isset($invoice) || true === empty($invoice)) {
             $this->log('    [Error] The Bitpay payment plugin was called to process a payment but could not instantiate an invoice object.');
             throw new \Exception('The Bitpay payment plugin was called to process a payment but could not instantiate an invoice object. Cannot continue!');
         } else {
             $this->log('    [Info] Call to generate invoice was successful: ' . $client->getResponse()->getBody());
         }
     } catch (\Exception $e) {
         $this->log('    [Error] Error generating invoice for ' . $order->get_order_number() . ', "' . $e->getMessage() . '"');
         error_log($e->getMessage());
         return array('result' => 'success', 'messages' => 'Sorry, but Bitcoin checkout with BitPay does not appear to be working.');
     }
     // Reduce stock levels
     $order->reduce_order_stock();
     // Remove cart
     WC()->cart->empty_cart();
     $this->log('    [Info] Leaving process_payment()...');
     // Redirect the customer to the BitPay invoice
     return array('result' => 'success', 'redirect' => $invoice->getUrl());
 }
Ejemplo n.º 9
0
       'id'          => (string) $sin,
   )
);
echo 'Token Obtained "'.$token->getToken().'"'.PHP_EOL;
//exit(0);
*/
/**
 * If you already have a token, uncomment this code
 *
 */
$token = new \Bitpay\Token();
$token->setToken($tokenString);
/**
 * Code that makes the invoice
 */
$invoice = new \Bitpay\Invoice();
$item = new \Bitpay\Item();
$item->setCode('skuNumber')->setDescription('General Description of Item')->setPrice('1.99');
$invoice->setCurrency(new \Bitpay\Currency('USD'));
$invoice->setItem($item);
$client = $bitpay->get('client');
$client->setToken($token);
try {
    $client->createInvoice($invoice);
} catch (\Exception $e) {
    $request = $client->getRequest();
    $response = $client->getResponse();
    echo (string) $request . PHP_EOL . PHP_EOL . PHP_EOL;
    echo (string) $response . PHP_EOL . PHP_EOL;
    exit(1);
    // We do not want to continue if something went wrong
Ejemplo n.º 10
0
 public function buildInvoice($params)
 {
     $invoice = new \Bitpay\Invoice();
     $buyer = new \Bitpay\Buyer();
     $item = new \Bitpay\Item();
     $currency = new \Bitpay\Currency();
     $buyer->setFirstName($params['BillingNameFirst']);
     $buyer->setLastName($params['BillingNameLast']);
     $buyer->setAddress(array($params['BillingAddress']));
     $buyer->setCity($params['BillingCity']);
     $buyer->setState($params['BillingState']);
     $buyer->setCountry($params['BillingCountry']);
     $buyer->setZip($params['BillingZipCode']);
     $buyer->setEmail($params['EmailAddress']);
     $buyer->setPhone($params['PhoneNumber']);
     $item->setDescription($params['ProductDescription'])->setPrice(floatval($params['price']));
     $currency->setCode($params['currencyCode']);
     $invoice->setTransactionSpeed($this->setting('riskSpeed'));
     if ($this->setting('redirectURL') != NULL) {
         $invoice->setRedirectUrl($params['redirectURL']);
     } else {
         $invoice->setRedirectUrl($params['returnURL']);
     }
     $invoice->setPosData($params['transactionID']);
     $invoice->setFullNotifications(true);
     $invoice->setOrderId($params['orderId'])->setNotificationUrl($params['PostBackURL']);
     $invoice->setCurrency($currency);
     $invoice->setBuyer($buyer);
     $invoice->setItem($item);
     return $invoice;
 }
function gateway_bitpay($seperator, $sessionid)
{
    global $wpdb;
    global $wpsc_cart;
    try {
        // Protect your data!
        $mcrypt_ext = new \Bitpay\Crypto\McryptExtension();
        $fingerprint = substr(sha1(sha1(__DIR__)), 0, 24);
        //Use token that is in_use and with facade = pos for generating invoices
        $is_a_token_paired = $wpdb->get_var("SELECT COUNT(*) FROM " . $wpdb->prefix . "bitpay_keys WHERE `in_use` = 'true' AND `facade` = 'pos' LIMIT 1");
        if ($is_a_token_paired < 1) {
            debuglog('[Error] In Bitpay plugin, bitpay.merchant.php::gateway_bitpay(): No tokens are paired so no transactions can be done!');
            var_dump("Error Processing Transaction. Please try again later. If the problem persists, please contact us at " . get_option('admin_email'));
        }
        $row = $wpdb->get_results("SELECT * FROM " . $wpdb->prefix . "bitpay_keys WHERE `in_use` = 'true' AND `facade` = 'pos' LIMIT 1");
        $token = unserialize(base64_decode($mcrypt_ext->decrypt($row[0]->token, $fingerprint, '00000000')));
        $public_key = unserialize(base64_decode($mcrypt_ext->decrypt($row[0]->public_key, $fingerprint, '00000000')));
        $private_key = unserialize(base64_decode($mcrypt_ext->decrypt($row[0]->private_key, $fingerprint, '00000000')));
        $network = $row[0]->network === 'Livenet' ? new \Bitpay\Network\Livenet() : new \Bitpay\Network\Testnet();
        $row_id = $row[0]->id;
        $adapter = new \Bitpay\Client\Adapter\CurlAdapter();
        // This grabs the purchase log id from
        // the database that refers to the $sessionid
        $purchase_log = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid`= " . $sessionid . " LIMIT 1", ARRAY_A);
        // This grabs the users info using the
        // $purchase_log from the previous SQL query
        $usersql = "SELECT  `" . WPSC_TABLE_SUBMITED_FORM_DATA . "`.value," . "`" . WPSC_TABLE_CHECKOUT_FORMS . "`.`name`," . "`" . WPSC_TABLE_CHECKOUT_FORMS . "`.`unique_name` FROM " . "`" . WPSC_TABLE_CHECKOUT_FORMS . "` LEFT JOIN " . "`" . WPSC_TABLE_SUBMITED_FORM_DATA . "` ON " . "`" . WPSC_TABLE_CHECKOUT_FORMS . "`.id = " . "`" . WPSC_TABLE_SUBMITED_FORM_DATA . "`.`form_id` WHERE " . "`" . WPSC_TABLE_SUBMITED_FORM_DATA . "`.`log_id`='" . $purchase_log['id'] . "'";
        $userinfo = $wpdb->get_results($usersql, ARRAY_A);
        // convert from awkward format
        $ui = array();
        foreach ((array) $userinfo as $value) {
            if (strlen($value['value'])) {
                $ui[$value['unique_name']] = $value['value'];
            }
        }
        $userinfo = $ui;
        /**
         * Create Buyer object that will be used later.
         */
        $buyer = new \Bitpay\Buyer();
        // name
        if (true === isset($userinfo['billingfirstname'])) {
            $buyer->setFirstName($userinfo['billingfirstname']);
        }
        if (true === isset($userinfo['billinglastname'])) {
            $buyer->setLastName($userinfo['billinglastname']);
        }
        // address -- remove newlines
        if (true === isset($userinfo['billingaddress'])) {
            $newline = strpos($userinfo['billingaddress'], "\n");
            $address2 = '';
            if ($newline !== FALSE) {
                $address_line1 = substr($userinfo['billingaddress'], 0, $newline);
                $address_line2 = substr($userinfo['billingaddress'], $newline + 1);
                $address_line2 = preg_replace('/\\r\\n/', ' ', $address_line2, -1, $count);
            } else {
                $address_line1 = $userinfo['billingaddress'];
            }
            $buyer->setAddress(array($address_line1, $address_line2));
        }
        // state
        if (true === isset($userinfo['billingstate'])) {
            // check if State is a number code used when Selecting country as US
            if (true === ctype_digit($userinfo['billingstate'])) {
                $buyer->setState(wpsc_get_state_by_id($userinfo['billingstate'], 'code'));
            } else {
                $buyer->setState($userinfo['billingstate']);
            }
        }
        // country
        if (true === isset($userinfo['billingcountry'])) {
            $buyer->setCountry($userinfo['billingcountry']);
        }
        // city
        if (true === isset($userinfo['billingcity'])) {
            $buyer->setCity($userinfo['billingcity']);
        }
        // postal code
        if (true === isset($userinfo['billingpostcode'])) {
            $buyer->setZip($userinfo['billingpostcode']);
        }
        // email
        if (true === isset($userinfo['billingemail'])) {
            $buyer->setEmail($userinfo['billingemail']);
        }
        // phone
        if (true === isset($userinfo['billingphone'])) {
            $buyer->setPhone($userinfo['billingphone']);
        }
        // more user info
        foreach (array('billingphone' => 'buyerPhone', 'billingemail' => 'buyerEmail', 'billingcity' => 'buyerCity', 'billingcountry' => 'buyerCountry', 'billingpostcode' => 'buyerZip') as $f => $t) {
            if ($userinfo[$f]) {
                $options[$t] = $userinfo[$f];
            }
        }
        /**
         * Create an Item object that will be used later
         */
        $item = new \Bitpay\Item();
        // itemDesc, Sku, and Quantity
        if (count($wpsc_cart->cart_items) == 1) {
            $item_incart = $wpsc_cart->cart_items[0];
            $item_id = $item_incart->product_id;
            $item_sku = wpsc_product_sku($item_id);
            $item_description = $item_incart->quantity > 1 ? $item_incart->quantity . ' x ' . $item_incart->product_name : $item_incart->product_name;
        } else {
            foreach ($wpsc_cart->cart_items as $item_incart) {
                $quantity += $item_incart->quantity;
                $item_id = $item_incart->product_id;
                $item_sku_individual = wpsc_product_sku($item_id);
                $item_sku .= $item_incart->quantity . ' x ' . $item_sku_individual . ' ';
            }
            $item_description = $quantity . ' items';
        }
        // price
        $price = number_format($wpsc_cart->total_price, 2, '.', '');
        $item->setDescription($item_description)->setCode($item_sku)->setPrice($price);
        // Create new BitPay invoice
        $invoice = new \Bitpay\Invoice();
        // Add the item to the invoice
        $invoice->setItem($item);
        // Add the buyers info to invoice
        $invoice->setBuyer($buyer);
        // Configure the rest of the invoice
        $purchase_log = $wpdb->get_row("SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid`= " . $sessionid . " LIMIT 1", ARRAY_A);
        $invoice->setOrderId($purchase_log['id'])->setNotificationUrl(get_option('siteurl') . '/?bitpay_callback=true');
        /**
         * BitPay offers services for many different currencies. You will need to
         * configure the currency in which you are selling products with.
         */
        $currency = new \Bitpay\Currency();
        $currencyId = get_option('currency_type');
        $currency_code = $wpdb->get_var($wpdb->prepare("SELECT `code` FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id` = %d LIMIT 1", $currencyId));
        $currency->setCode($currency_code);
        // Set the invoice currency
        $invoice->setCurrency($currency);
        // Transaction Speed
        $invoice->setTransactionSpeed(get_option('bitpay_transaction_speed'));
        // Redirect URL
        $separator = get_option('permalink_structure') != '' ? '?' : '&';
        if (true === is_null(get_option('bitpay_redirect'))) {
            update_option('bitpay_redirect', get_site_url());
        }
        $redirect_url = get_option('bitpay_redirect');
        $invoice->setRedirectUrl($redirect_url);
        // PosData
        $invoice->setPosData($sessionid);
        // Full Notifications
        $invoice->setFullNotifications(true);
        /**
         * Create the client that will be used
         * to send requests to BitPay's API
         */
        $client = new \Bitpay\Client\Client();
        $client->setAdapter($adapter);
        $client->setNetwork($network);
        $client->setPrivateKey($private_key);
        $client->setPublicKey($public_key);
        /**
         * You will need to set the token that was
         * returned when you paired your keys.
         */
        $client->setToken($token);
        $transaction = true;
        // Send invoice
        try {
            $client->createInvoice($invoice);
        } catch (\Exception $e) {
            debuglog('[Error] In Bitpay plugin, bitpay.merchant.php::gateway_bitpay(): Call to createInvoice() failed with the message: ' . $e->getMessage());
            var_dump("Error Processing Transaction. Please try again later. If the problem persists, please contact us at " . get_option('admin_email'));
            $transaction = false;
        }
        if (true === $transaction) {
            $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `notes`= 'The payment has not been received yet.' WHERE `sessionid`=" . $sessionid;
            $wpdb->query($sql);
            $wpsc_cart->empty_cart();
            unset($_SESSION['WpscGatewayErrorMessage']);
            header('Location: ' . $invoice->getUrl());
        }
        exit;
    } catch (\Exception $e) {
        debuglog('[Error] In Bitpay plugin, form_bitpay() function on line ' . $e->getLine() . ', with the error "' . $e->getMessage() . '" .');
        throw $e;
    }
}
Ejemplo n.º 12
0
 */
require __DIR__ . '/../vendor/autoload.php';
/**
 * Create an Item object that will be used later
 */
$item = new \Bitpay\Item();
$item->setCode('skuNumber')->setDescription('General Description of Item')->setPrice('1.99');
/**
 * Create Buyer object that will be used later.
 */
$buyer = new \Bitpay\Buyer();
$buyer->setFirstName('Some')->setLastName('Customer')->setPhone('555-5555-5555')->setEmail('*****@*****.**')->setAddress(array('123 Main St', 'Suite 1'))->setCity('Atlanta')->setState('GA')->setZip('30120')->setCountry('US');
/**
 * Create the invoice
 */
$invoice = new \Bitpay\Invoice();
// Add the item to the invoice
$invoice->setItem($item);
// Add the buyers info to invoice
$invoice->setBuyer($buyer);
// Configure the rest of the invoice
$invoice->setOrderId('OrderIdFromYourSystem')->setNotificationUrl('https://store.example.com/bitpay/callback');
/**
 * BitPay offers services for many different currencies. You will need to
 * configure the currency in which you are selling products with.
 */
$currency = new \Bitpay\Currency();
$currency->setCode('USD');
// Set the invoice currency
$invoice->setCurrency($currency);
/**