/** * * 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); } }
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; } }
public function ipn_callback() { $this->log(' [Info] Entered ipn_callback()...'); // Retrieve the Invoice ID and Network URL from the supposed IPN data $post = file_get_contents("php://input"); if (true === empty($post)) { $this->log(' [Error] No post data sent to IPN handler!'); error_log('[Error] BitPay plugin received empty POST data for an IPN message.'); wp_die('No post data'); } else { $this->log(' [Info] The post data sent to IPN handler is present...'); } $json = json_decode($post, true); if (true === empty($json)) { $this->log(' [Error] Invalid JSON payload sent to IPN handler: ' . $post); error_log('[Error] BitPay plugin received an invalid JSON payload sent to IPN handler: ' . $post); wp_die('Invalid JSON'); } else { $this->log(' [Info] The post data was decoded into JSON...'); } if (false === array_key_exists('id', $json)) { $this->log(' [Error] No invoice ID present in JSON payload: ' . var_export($json, true)); error_log('[Error] BitPay plugin did not receive an invoice ID present in JSON payload: ' . var_export($json, true)); wp_die('No Invoice ID'); } else { $this->log(' [Info] Invoice ID present in JSON payload...'); } if (false === array_key_exists('url', $json)) { $this->log(' [Error] No invoice URL present in JSON payload: ' . var_export($json, true)); error_log('[Error] BitPay plugin did not receive an invoice URL present in JSON payload: ' . var_export($json, true)); wp_die('No Invoice URL'); } else { $this->log(' [Info] Invoice URL present in JSON payload...'); } // Get a BitPay Client to prepare for invoice fetching $client = new \Bitpay\Client\Client(); if (false === isset($client) && true === empty($client)) { $this->log(' [Error] The Bitpay payment plugin was called to handle an IPN but could not instantiate a client object.'); throw new \Exception('The Bitpay payment plugin was called to handle an IPN but could not instantiate a client object. Cannot continue!'); } else { $this->log(' [Info] Created new Client object in IPN handler...'); } if (false === strpos($json['url'], 'test')) { $network = new \Bitpay\Network\Livenet(); $this->log(' [Info] Set network to Livenet.'); } else { $network = new \Bitpay\Network\Testnet(); $this->log(' [Info] Set network to Testnet.'); } $this->log(' [Info] Checking IPN response is valid via ' . $network->getName() . '...'); $client->setNetwork($network); $curlAdapter = new \Bitpay\Client\Adapter\CurlAdapter(); if (false === isset($curlAdapter) && true === empty($curlAdapter)) { $this->log(' [Error] The Bitpay payment plugin was called to handle an IPN but could not instantiate a CurlAdapter object.'); throw new \Exception('The Bitpay payment plugin was called to handle an IPN but could not instantiate a CurlAdapter object. Cannot continue!'); } else { $this->log(' [Info] Created new CurlAdapter object in IPN handler...'); } // Setting the Adapter param to a new BitPay CurlAdapter object $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 handle an IPN but could not set client->setPrivateKey to this->api_key. The empty() check failed!'); throw new \Exception('The Bitpay payment plugin was called to handle an IPN 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 handle an IPN but could not set client->setPublicKey to this->api_pub. The empty() check failed!'); throw new \Exception('The Bitpay payment plugin was called to handle an IPN 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 handle an IPN but could not set client->setToken to this->api_token. The empty() check failed!'); throw new \Exception('The Bitpay payment plugin was called to handle an IPN 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...'); // Fetch the invoice from BitPay's server to update the order try { $invoice = $client->getInvoice($json['id']); if (true === isset($invoice) && false === empty($invoice)) { $this->log(' [Info] The IPN check appears to be valid.'); } else { $this->log(' [Error] The IPN check did not pass!'); wp_die('Invalid IPN'); } } catch (\Exception $e) { $error_string = 'IPN Check: Can\'t find invoice ' . $json['id']; $this->log(" [Error] {$error_string}"); $this->log(" [Error] " . $e->getMessage()); wp_die($e->getMessage()); } $order_id = $invoice->getOrderId(); if (false === isset($order_id) && true === empty($order_id)) { $this->log(' [Error] The Bitpay payment plugin was called to process an IPN message but could not obtain the order ID from the invoice.'); throw new \Exception('The Bitpay payment plugin was called to process an IPN message but could not obtain the order ID from the invoice. Cannot continue!'); } else { $this->log(' [Info] Order ID is: ' . $order_id); } // Creating a new WooCommerce Order object with $order_id $order = wc_get_order($order_id); if (false === isset($order) && true === empty($order)) { $this->log(' [Error] The Bitpay payment plugin was called to process an IPN message but could not retrieve the order details for order_id ' . $order_id); throw new \Exception('The Bitpay payment plugin was called to process an IPN message but could not retrieve the order details for order_id ' . $order_id . '. Cannot continue!'); } else { $this->log(' [Info] Order details retrieved successfully...'); } $current_status = $order->get_status(); if (false === isset($current_status) && true === empty($current_status)) { $this->log(' [Error] The Bitpay payment plugin was called to process an IPN message but could not obtain the current status from the order.'); throw new \Exception('The Bitpay payment plugin was called to process an IPN message but could not obtain the current status from the order. Cannot continue!'); } else { $this->log(' [Info] The current order status for this order is ' . $current_status); } $order_states = $this->get_option('order_states'); $new_order_status = $order_states['new']; $paid_status = $order_states['paid']; $confirmed_status = $order_states['confirmed']; $complete_status = $order_states['complete']; $invalid_status = $order_states['invalid']; $checkStatus = $invoice->getStatus(); if (false === isset($checkStatus) && true === empty($checkStatus)) { $this->log(' [Error] The Bitpay payment plugin was called to process an IPN message but could not obtain the current status from the invoice.'); throw new \Exception('The Bitpay payment plugin was called to process an IPN message but could not obtain the current status from the invoice. Cannot continue!'); } else { $this->log(' [Info] The current order status for this invoice is ' . $checkStatus); } // Based on the payment status parameter for this // IPN, we will update the current order status. switch ($checkStatus) { // The "paid" IPN message is received almost // immediately after the BitPay invoice is paid. case 'paid': $this->log(' [Info] IPN response is a "paid" message.'); if ($current_status == $complete_status || 'wc_' . $current_status == $complete_status || $current_status == 'completed') { $error_string = 'Paid IPN, but order has status: ' . $current_status; $this->log(" [Warning] {$error_string}"); } else { $this->log(' [Info] This order has not been updated yet so setting new status...'); $order->update_status($paid_status); $order->add_order_note(__('BitPay invoice paid. Awaiting network confirmation and payment completed status.', 'bitpay')); } break; // The "confirmed" status is sent when the payment is // confirmed based on your transaction speed setting. // The "confirmed" status is sent when the payment is // confirmed based on your transaction speed setting. case 'confirmed': $this->log(' [Info] IPN response is a "confirmed" message.'); if ($current_status == $complete_status || 'wc_' . $current_status == $complete_status || $current_status == 'completed') { $error_string = 'Confirmed IPN, but order has status: ' . $current_status; $this->log(" [Warning] {$error_string}"); } else { $this->log(' [Info] This order has not been updated yet so setting confirmed status...'); $order->update_status($confirmed_status); $order->add_order_note(__('BitPay invoice confirmed. Awaiting payment completed status.', 'bitpay')); } break; // The complete status is when the Bitcoin network // obtains 6 confirmations for this transaction. // The complete status is when the Bitcoin network // obtains 6 confirmations for this transaction. case 'complete': $this->log(' [Info] IPN response is a "complete" message.'); if ($current_status == $complete_status || 'wc_' . $current_status == $complete_status || $current_status == 'completed') { $error_string = 'Complete IPN, but order has status: ' . $current_status; $this->log(" [Warning] {$error_string}"); } else { $this->log(' [Info] This order has not been updated yet so setting complete status...'); $order->payment_complete(); $order->update_status($complete_status); $order->add_order_note(__('BitPay invoice payment completed. Payment credited to your merchant account.', 'bitpay')); } break; // This order is invalid for some reason. // Either it's a double spend or some other // problem occurred. // This order is invalid for some reason. // Either it's a double spend or some other // problem occurred. case 'invalid': $this->log(' [Info] IPN response is a "invalid" message.'); if ($current_status == $complete_status || 'wc_' . $current_status == $complete_status || $current_status == 'completed') { $error_string = 'Paid IPN, but order has status: ' . $current_status; $this->log(" [Warning] {$error_string}"); } else { $this->log(' [Info] This order has a problem so setting "invalid" status...'); $order->update_status($invalid_status, __('Bitcoin payment is invalid for this order! The payment was not confirmed by the network within 1 hour. Do not ship the product for this order!', 'bitpay')); } break; // There was an unknown message received. // There was an unknown message received. default: $this->log(' [Info] IPN response is an unknown message type. See error message below:'); $error_string = 'Unhandled invoice status: ' . $invoice->getStatus(); $this->log(" [Warning] {$error_string}"); } $this->log(' [Info] Leaving ipn_callback()...'); }
$adapter = new \Bitpay\Client\Adapter\CurlAdapter(); $client->setPrivateKey($privateKey); $client->setPublicKey($publicKey); $client->setNetwork($network); $client->setAdapter($adapter); // --------------------------- /** * The last object that must be injected is the token object. */ $token = new \Bitpay\Token(); $token->setToken('UpdateThisValue'); // UPDATE THIS VALUE /** * Token object is injected into the client */ $client->setToken($token); /** * This is where we will start to create an Invoice object, make sure to check * the InvoiceInterface for methods that you can use. */ $invoice = new \Bitpay\Invoice(); /** * Item is used to keep track of a few things */ $item = new \Bitpay\Item(); $item->setCode('skuNumber')->setDescription('General Description of Item')->setPrice('1.99'); $invoice->setItem($item); /** * BitPay supports multiple different currencies. Most shopping cart applications * and applications in general have defined set of currencies that can be used. * Setting this to one of the supported currencies will create an invoice using