Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update redirect URLs #44

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
57 changes: 36 additions & 21 deletions index.php
Expand Up @@ -345,12 +345,12 @@ public function init_form_fields()
'bitpay_custom_redirect' => array(
'title' => __('Custom Redirect Page', 'woocommerce'),
'type' => 'text',
'description' => __('Set the full url (ie. <i>https://yoursite.com/custompage</i>) if you would like the customer to be redirected to a custom page after completing theh purchase. <b>Note: this will only work if the REDIRECT mode is used</b> ', 'woocommerce'),
'description' => __('Set the full url (ie. <i>https://yoursite.com/custompage</i>) if you would like the customer to be redirected to a custom page after completing the purchase.<br>Leave this empty to redirect customers to the default Woocommerce order completed page.<br><b>Note: this will only work if the REDIRECT mode is used</b> ', 'woocommerce'),
),
'bitpay_close_url' => array(
'title' => __('Close URL', 'woocommerce'),
'type' => 'text',
'description' => __('Set the close url <br /><b>Note: this will only work if the REDIRECT mode is used</b> ', 'woocommerce'),
'description' => __('Set the close url.<br>Leave this empty to redirect customers to the default Woocommerce order payment failed page.<br /><b>Note: this will only work if the REDIRECT mode is used</b> ', 'woocommerce'),
),
'bitpay_checkout_mini' => array(
'title' => __('Show in mini cart ', 'woocommerce'),
Expand Down Expand Up @@ -384,7 +384,7 @@ public function init_form_fields()
'bitpay_checkout_error' => array(
'title' => __('Error handling', 'woocommerce'),
'type' => 'text',
'description' => __('If there is an error with creating the invoice, enter the <b>page slug</b>. <br>ie. ' . get_home_url() . '/<b>error</b><br><br>View your pages <a target = "_blank" href = "/wp-admin/edit.php?post_type=page">here</a>,.<br><br>Click the "quick edit" and copy and paste a custom slug here.', 'woocommerce'),
'description' => __('If there is an error with creating the invoice, enter the <b>page slug</b>.<br>Leave this empty to redirect customers to the default Woocommerce order payment failed page. <br>ie. ' . get_home_url() . '/<b>error</b><br><br>View your pages <a target = "_blank" href = "/wp-admin/edit.php?post_type=page">here</a>,.<br><br>Click the "quick edit" and copy and paste a custom slug here.', 'woocommerce'),

),
'bitpay_checkout_error_message' => array(
Expand Down Expand Up @@ -896,24 +896,32 @@ function woo_custom_redirect_after_purchase()
$invoiceData = json_decode($invoice->BPC_getInvoiceData());
if (property_exists($invoiceData, 'error')):
$bitpay_checkout_options = get_option('woocommerce_bitpay_checkout_gateway_settings');
$errorURL = get_home_url().'/'.$bitpay_checkout_options['bitpay_checkout_error'];
$order_status = "wc-cancelled";
$order = new WC_Order($order_id);
$items = $order->get_items();
$order->update_status($order_status, __($invoiceData->error.'.', 'woocommerce'));

//clear the cart first so things dont double up
WC()->cart->empty_cart();
foreach ($items as $item) {
//now insert for each quantity
$item_count = $item->get_quantity();
for ($i = 0; $i < $item_count; $i++):
WC()->cart->add_to_cart($item->get_product_id());
endfor;
}
$errorSlug = $bitpay_checkout_options['bitpay_checkout_error'];
$errorURL = get_home_url() . '/' . $errorSlug;
if (empty($errorSlug)):
// If Error slug is left empty, redirect the customer his order checkout payment URL.
// This should be the default behaviour for better customer experience and better conversion. Noone wants to create his cart once again.
$errorURL = $order->get_checkout_payment_url( $on_checkout = false );
else :
$order_status = "wc-cancelled";
$order = new WC_Order($order_id);
$items = $order->get_items();
$order->update_status($order_status, __($invoiceData->error.'.', 'woocommerce'));

//clear the cart first so things dont double up
WC()->cart->empty_cart();
foreach ($items as $item) {
//now insert for each quantity
$item_count = $item->get_quantity();
for ($i = 0; $i < $item_count; $i++):
WC()->cart->add_to_cart($item->get_product_id());
endfor;
}
endif;
wp_redirect($errorURL);
die();
endif;
endif;

BPC_Logger($invoiceData, 'NEW BITPAY INVOICE', true);
//now we have to append the invoice transaction id for the callback verification
Expand Down Expand Up @@ -1138,8 +1146,15 @@ function bitpay_checkout_custom_message($order_id)
$checkout_message = $bitpay_checkout_options['bitpay_checkout_checkout_message'];
if ($order->get_status() == "pending") {
$params = new stdClass();
if($bitpay_checkout_options['bitpay_close_url'] == "") {
$params->closeURL = $base_url . "/payment-cancel";
if($bitpay_checkout_options['bitpay_close_url'] == "") {
$checkout_slug = $bitpay_checkout_options['bitpay_checkout_slug'];
if (empty($checkout_slug)):
$checkout_slug = 'checkout';
endif;

// If close URL is left empty, redirect the customer his order checkout payment URL.
// This should be the default behaviour for better customer experience and better conversion. Noone wants to create his cart once again.
$params->closeURL = $order->get_checkout_payment_url( $on_checkout = false );
wp_redirect($params->closeURL);
} else {
wp_redirect($bitpay_checkout_options['bitpay_close_url']);
Expand Down Expand Up @@ -1177,4 +1192,4 @@ function wc_bitpay_checkout_add_to_gateways($gateways)
{
$gateways[] = 'WC_Gateway_BitPay';
return $gateways;
}
}