woocommerce/shortcodes/shortcode-pay.php

140 lines
4.2 KiB
PHP
Raw Normal View History

2011-08-10 17:11:11 +00:00
<?php
/**
* Pay Shortcode
*
2011-11-28 18:49:32 +00:00
* The pay page. Used for form based gateways to show payment forms and order info.
2011-08-10 17:11:11 +00:00
*
* @package WooCommerce
* @category Shortcode
* @author WooThemes
*/
function get_woocommerce_pay( $atts ) {
global $woocommerce;
return $woocommerce->shortcode_wrapper('woocommerce_pay', $atts);
2011-08-10 17:11:11 +00:00
}
/**
* Outputs the pay page - payment gateways can hook in here to show payment forms etc
**/
function woocommerce_pay() {
global $woocommerce, $order;
2011-08-10 17:11:11 +00:00
if ( isset($_GET['pay_for_order']) && isset($_GET['order']) && isset($_GET['order_id']) ) :
// Pay for existing order
$order_key = urldecode( $_GET['order'] );
$order_id = (int) $_GET['order_id'];
2011-12-10 18:40:51 +00:00
$order = new woocommerce_order( $order_id );
2011-08-10 17:11:11 +00:00
if ($order->id == $order_id && $order->order_key == $order_key && in_array($order->status, array('pending', 'failed'))) :
2011-08-10 17:11:11 +00:00
// Set customer location to order location
if ($order->billing_country) $woocommerce->customer->set_country( $order->billing_country );
if ($order->billing_state) $woocommerce->customer->set_state( $order->billing_state );
if ($order->billing_postcode) $woocommerce->customer->set_postcode( $order->billing_postcode );
2011-08-10 17:11:11 +00:00
// Pay form was posted - process payment
if (isset($_POST['pay']) && $woocommerce->verify_nonce('pay')) :
2011-08-10 17:11:11 +00:00
// Update payment method
if ($order->order_total > 0 ) :
$payment_method = woocommerce_clean($_POST['payment_method']);
2011-12-07 20:48:40 +00:00
$available_gateways = $woocommerce->payment_gateways->get_available_payment_gateways();
2011-08-10 17:11:11 +00:00
2011-12-07 20:48:40 +00:00
// Update meta
update_post_meta( $order_id, '_payment_method', $payment_method);
if (isset($available_gateways) && isset($available_gateways[$payment_method])) :
$payment_method_title = $available_gateways[$payment_method]->title;
endif;
update_post_meta( $order_id, '_payment_method_title', $payment_method_title);
$result = $available_gateways[$payment_method]->process_payment( $order_id );
2011-08-10 17:11:11 +00:00
// Redirect to success/confirmation/payment page
if ($result['result']=='success') :
2011-12-07 20:48:40 +00:00
wp_redirect( $result['redirect'] );
2011-08-10 17:11:11 +00:00
exit;
endif;
else :
// No payment was required for order
$order->payment_complete();
wp_safe_redirect( get_permalink(get_option('woocommerce_thanks_page_id')) );
exit;
endif;
endif;
// Show form
woocommerce_get_template('checkout/pay_for_order.php');
2011-08-10 17:11:11 +00:00
elseif (!in_array($order->status, array('pending', 'failed'))) :
2011-08-10 17:11:11 +00:00
$woocommerce->add_error( __('Your order has already been paid for. Please contact us if you need assistance.', 'woothemes') );
$woocommerce->show_messages();
2011-08-10 17:11:11 +00:00
else :
$woocommerce->add_error( __('Invalid order.', 'woothemes') );
$woocommerce->show_messages();
2011-08-10 17:11:11 +00:00
endif;
else :
// Pay for order after checkout step
if (isset($_GET['order'])) $order_id = $_GET['order']; else $order_id = 0;
if (isset($_GET['key'])) $order_key = $_GET['key']; else $order_key = '';
if ($order_id > 0) :
2011-12-10 18:40:51 +00:00
$order = new woocommerce_order( $order_id );
2011-08-10 17:11:11 +00:00
if ($order->order_key == $order_key && in_array($order->status, array('pending', 'failed'))) :
2011-08-10 17:11:11 +00:00
?>
<ul class="order_details">
<li class="order">
<?php _e('Order:', 'woothemes'); ?>
<strong># <?php echo $order->id; ?></strong>
</li>
<li class="date">
<?php _e('Date:', 'woothemes'); ?>
2011-08-17 23:42:07 +00:00
<strong><?php echo date(get_option('date_format'), strtotime($order->order_date)); ?></strong>
2011-08-10 17:11:11 +00:00
</li>
<li class="total">
<?php _e('Total:', 'woothemes'); ?>
<strong><?php echo woocommerce_price($order->order_total); ?></strong>
</li>
<li class="method">
<?php _e('Payment method:', 'woothemes'); ?>
<strong><?php
echo $order->payment_method_title;
2011-08-10 17:11:11 +00:00
?></strong>
</li>
</ul>
<?php do_action( 'woocommerce_receipt_' . $order->payment_method, $order_id ); ?>
2011-08-10 17:11:11 +00:00
<div class="clear"></div>
<?php
else :
wp_safe_redirect( get_permalink(get_option('woocommerce_myaccount_page_id')) );
exit;
endif;
else :
wp_safe_redirect( get_permalink(get_option('woocommerce_myaccount_page_id')) );
exit;
endif;
endif;
}