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()...');
 }
コード例 #2
0
function bitpay_callback()
{
    global $wpdb;
    try {
        if (isset($_GET['bitpay_callback'])) {
            $post = file_get_contents("php://input");
            if (true === empty($post)) {
                return array('error' => 'No post data');
            }
            $json = json_decode($post, true);
            if (true === is_string($json)) {
                return array('error' => $json);
            }
            if (false === array_key_exists('posData', $json)) {
                return array('error' => 'no posData');
            }
            if (false === array_key_exists('id', $json)) {
                return 'Cannot find invoice ID';
            }
            // Don't trust parameters from the scary internet.
            // Use invoice ID from the $json in  getInvoice($invoice_id) and get status from that.
            $client = new \Bitpay\Client\Client();
            $adapter = new \Bitpay\Client\Adapter\CurlAdapter();
            $network = strpos($json['url'], 'test') === false ? new \Bitpay\Network\Livenet() : new \Bitpay\Network\Testnet();
            $client->setAdapter($adapter);
            $client->setNetwork($network);
            // Checking invoice is valid...
            $response = $client->getInvoice($json['id']);
            $sessionid = $response->getPosData();
            // get buyer email
            $sql = "SELECT * FROM `" . WPSC_TABLE_PURCHASE_LOGS . "` WHERE `sessionid`=" . $sessionid;
            $purchase_log = $wpdb->get_results($sql, ARRAY_A);
            $email_form_field = $wpdb->get_var("SELECT `id` FROM `" . WPSC_TABLE_CHECKOUT_FORMS . "` WHERE `type` IN ('email') AND `active` = '1' ORDER BY `checkout_order` ASC LIMIT 1");
            $email = $wpdb->get_var($wpdb->prepare("SELECT `value` FROM `" . WPSC_TABLE_SUBMITTED_FORM_DATA . "` WHERE `log_id` = %d AND `form_id` = %d LIMIT 1", $purchase_log[0]['id'], $email_form_field));
            // get cart contents
            $sql = "SELECT * FROM `" . WPSC_TABLE_CART_CONTENTS . "` WHERE `purchaseid`=" . $purchase_log[0]['id'];
            $cart_contents = $wpdb->get_results($sql, ARRAY_A);
            // get currency symbol
            $currency_id = get_option('currency_type');
            $sql = "SELECT * FROM `" . WPSC_TABLE_CURRENCY_LIST . "` WHERE `id`=" . $currency_id;
            $currency_data = $wpdb->get_results($sql, ARRAY_A);
            $currency_symbol = $currency_data[0]['symbol'];
            // list products and individual prices in the email
            $message_product = "\r\n\r\nTransaction Details:\r\n\r\n";
            $pnp = 0.0;
            $subtotal = 0.0;
            foreach ($cart_contents as $product) {
                // shipping for each item
                $pnp += $product['pnp'];
                $message_product .= 'x' . $product['quantity'] . ' ' . $product['name'] . ' - ' . $currency_symbol . $product['price'] * $product['quantity'] . "\r\n";
                $subtotal += $product['price'] * $product['quantity'];
            }
            //list subtotal
            $subtotal = number_format($subtotal, 2, '.', ',');
            $message_product .= "\r\n" . 'Subtotal: ' . $currency_symbol . $subtotal . "\r\n";
            //list total taxes and total shipping costs in the email
            $message_product .= 'Taxes: ' . $currency_symbol . $purchase_log[0]['wpec_taxes_total'] . "\r\n";
            $message_product .= 'Shipping: ' . $currency_symbol . ($purchase_log[0]['base_shipping'] + $pnp) . "\r\n\r\n";
            //display total price in the email
            $message_product .= 'Total Price: ' . $currency_symbol . $purchase_log[0]['totalprice'];
            switch ($response->getStatus()) {
                //For low and medium transaction speeds, the order status is set to "Order Received" . The customer receives
                //an initial email stating that the transaction has been paid.
                case 'paid':
                    if (true === is_numeric($sessionid)) {
                        $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `processed`= '2' WHERE `sessionid`=" . $sessionid;
                        $wpdb->query($sql);
                        $message = 'Thank you! Your payment has been received, but the transaction has not been confirmed on the bitcoin network. You will receive another email when the transaction has been confirmed.';
                        $message .= $message_product;
                        $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `notes`= 'The payment has been received, but the transaction has not been confirmed on the bitcoin network. This will be updated when the transaction has been confirmed.' WHERE `sessionid`=" . $sessionid;
                        $wpdb->query($sql);
                        if (wp_mail($email, 'Payment Received', $message)) {
                            $mail_sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `email_sent`= '1' WHERE `sessionid`=" . $sessionid;
                            $wpdb->query($mail_sql);
                        }
                        transaction_results($sessionid, false);
                        //false because this is just for email notification
                    }
                    break;
                    //For low and medium transaction speeds, the order status will not change. For high transaction speed, the order
                    //status is set to "Order Received" here. For all speeds, an email will be sent stating that the transaction has
                    //been confirmed.
                //For low and medium transaction speeds, the order status will not change. For high transaction speed, the order
                //status is set to "Order Received" here. For all speeds, an email will be sent stating that the transaction has
                //been confirmed.
                case 'confirmed':
                    if (true === is_numeric($sessionid)) {
                        $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `processed`= '2' WHERE `sessionid`=" . $sessionid;
                        $wpdb->query($sql);
                        $mail_sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `email_sent`= '1' WHERE `sessionid`=" . $sessionid;
                        //display initial "thank you" if transaction speed is high, as the 'paid' status is skipped on high speed
                        if (get_option('bitpay_transaction_speed') == 'high') {
                            $message = 'Thank you! Your payment has been received, and the transaction has been confirmed on the bitcoin network. You will receive another email when the transaction is complete.';
                            $message .= $message_product;
                            $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `notes`= 'The payment has been received, and the transaction has been confirmed on the bitcoin network. This will be updated when the transaction has been completed.' WHERE `sessionid`=" . $sessionid;
                            $wpdb->query($sql);
                            if (wp_mail($email, 'Payment Received', $message)) {
                                $wpdb->query($mail_sql);
                            }
                        } else {
                            $message = 'Your transaction has now been confirmed on the bitcoin network. You will receive another email when the transaction is complete.';
                            $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `notes`= 'The payment has been received, and the transaction has been confirmed on the bitcoin network. This will be updated when the transaction has been completed.' WHERE `sessionid`=" . $sessionid;
                            $wpdb->query($sql);
                            if (wp_mail($email, 'Transaction Confirmed', $message)) {
                                $wpdb->query($mail_sql);
                            }
                        }
                        //false because this is just for email notification
                        transaction_results($sessionid, false);
                    }
                    break;
                    //The purchase receipt email is sent upon the invoice status changing to "complete", and the order
                    //status is changed to Accepted Payment
                //The purchase receipt email is sent upon the invoice status changing to "complete", and the order
                //status is changed to Accepted Payment
                case 'complete':
                    if (true === is_numeric($sessionid)) {
                        $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `processed`= '3' WHERE `sessionid`=" . $sessionid;
                        $wpdb->query($sql);
                        $message = 'Your transaction is now complete! Thank you for using BitPay!';
                        $sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `notes`= 'The transaction is now complete.' WHERE `sessionid`=" . $sessionid;
                        $wpdb->query($sql);
                        if (wp_mail($email, 'Transaction Complete', $message)) {
                            $mail_sql = "UPDATE `" . WPSC_TABLE_PURCHASE_LOGS . "` SET `email_sent`= '1' WHERE `sessionid`=" . $sessionid;
                            $wpdb->query($mail_sql);
                        }
                        //false because this is just for email notification
                        transaction_results($sessionid, false);
                    }
                    break;
                    // END OF switch ($response->getStatus())
            }
        }
    } catch (\Exception $e) {
        debuglog('[Error] In Bitpay plugin, form_bitpay() function on line ' . $e->getLine() . ', with the error "' . $e->getMessage() . '".');
        throw $e;
    }
}