I'm struggling with a piece of code for more than a week. I implemented Braintree, using the PHP SDK, in my Codeigniter app and I have this issue:
Everything works as expected in sandbox mode but when I go into production mode, the redirect fails, the page is redirected back to the order confirmation, even though the CC was charged and both emails sent.
Here is my controller:
function order_confirmation()
{
require_once('application/libraries/braintree/lib/Braintree.php');
Braintree_Configuration::environment('production');
Braintree_Configuration::merchantId('MY_MERCHANT_ID');
Braintree_Configuration::publicKey('MY_PUBLIC_KEY');
Braintree_Configuration::privateKey('MY_PRIVATE_KEY');
if ($this->input->post('checkout'))
{
$price = 24.99;
$quantity = 1;
if ($this->input->post('shipping') == 1)
$shipping_price = 0;
elseif ($this->input->post('shipping') == 2)
$shipping_price = 6.99;
$amount = $price * $quantity + $shipping_price;
//BrainTree payment process
$result = Braintree_Transaction::sale(array(
'amount' => $amount,
'creditCard' => array(
'number' => $this->input->post('credit_card_number'),
'expirationDate' => $this->input->post('expiration_month') . '/' . $this->input->post('expiration_year'),
'cvv' => $this->input->post('cvv')
),
'options' => [
'submitForSettlement' => True
]
));
if ($result->success)
{
// I left only the first and last name field to save up space
$first_name = $this->db->escape($this->session->userdata('first_name'));
$last_name = $this->db->escape($this->session->userdata('last_name'));
$date_created = $this->db->escape(time());
// Add the order
$this->shop_model->add_order($first_name, $last_name, $transaction_id, $date_created);
$order_id = $this->db->insert_id();
$product_id = $this->db->escape($this->input->post('product_id'));
// Add the order items
$this->shop_model->add_order_items($order_id, $product_id, $quantity, $price);
$data['site_name'] = $this->config->item('website_name');
$data['order'] = $this->shop_model->get_order($order_id);
$data['order_items'] = $this->shop_model->get_order_items($order_id);
$customer_email = $this->session->userdata('email');
// Send the email notification to the merchant
send_html_email('order_confirmation_merchant', $this->config->item('primary_email'), $this->config->item('website_name'), $this->config->item('primary_email'), 'shop', $data);
// Send the order confirmation to the customer
send_html_email('order_confirmation_customer', $this->config->item('primary_email'), $this->config->item('website_name'), $customer_email, 'shop', $data);
redirect(SHOP . '/checkout-success');
// header("Location: checkout-success");
// echo '<script language="JavaScript">document.location.href="' . base_url() . SHOP . '/checkout-success' . '"</script>' . "
";
}
else
{
redirect(SHOP . '/checkout-error');
}
}
$this->template->set_template('no_sidebar');
$this->template->write('meta_description', 'Order confirmation');
$this->template->write('meta_keywords', 'Order confirmation');
$this->template->write('title', 'Order confirmation');
$this->template->write_view('header', 'frontend/header');
$this->template->write_view('section', 'frontend/shop/order_confirmation'/*, $data*/);
$this->template->write_view('footer', 'frontend/footer');
$this->template->render();
}
As you can see I tried serveral redirect methods (Codeigniter redirect(), native PHP, Javascript) with no success. As mentioned before, I do see the success page in sandbox mode and the error page if I enter a bogus CC, but no success page, only my order confirmation form, in production mode, even though the CC is charged, the order added in the database and the emails sent.
I would like to mention that the website has a SSL certificate and I tested using Mozilla Firefox, Google Chrome, Microsoft Edge. There are no JS errors, no PHP warnings.
Any answer would be greatly appreciated. Thank you!