Advertisement
Guest User

Untitled

a guest
Oct 23rd, 2014
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.04 KB | None | 0 0
  1. function _wpec_get_checkout_token( $product, $return_url ) {
  2.     global $wpdb, $wpsc_cart, $current_user;
  3.  
  4.     wpsc_empty_cart();
  5.     // Manually add the product to the WPEC cart
  6.     $_POST['product_id'] = $product->get_id();
  7.     $_POST['collected_data'] = false;
  8.     wpsc_add_to_cart();
  9.  
  10.     // Duplication of code in wpsc_submit_checkout, removes all shipping, tax & validation related routines
  11.     do_action( 'wpsc_before_submit_checkout' );
  12.     $sessionid = (mt_rand( 100, 999 ) . time());
  13.     wpsc_update_customer_meta( 'checkout_session_id', $sessionid );
  14.  
  15.     $wpsc_checkout     = new wpsc_checkout();
  16.     $submitted_gateway = 'paypal-digital-goods'; // Only allow one payment gateway for now
  17.  
  18.     // Keep track of tax if taxes are exclusive
  19.     $wpec_taxes_controller = new wpec_taxes_controller();
  20.  
  21.     if ( ! $wpec_taxes_controller->wpec_taxes_isincluded() ) {
  22.         $tax = $wpsc_cart->calculate_total_tax();
  23.         $tax_percentage = $wpsc_cart->tax_percentage;
  24.     } else {
  25.         $tax = 0.00;
  26.         $tax_percentage = 0.00;
  27.     }
  28.  
  29.     $total = $wpsc_cart->calculate_total_price();
  30.  
  31.     $args = array(
  32.         'totalprice'       => $total,
  33.         'statusno'         => '0',
  34.         'sessionid'        => $sessionid,
  35.         'user_ID'          => $current_user->ID, // This should be set in WPOAuthProvider::plugins_loaded()
  36.         'date'             => time(),
  37.         'gateway'          => $submitted_gateway,
  38.         'billing_country'  => $wpsc_cart->selected_country,
  39.         'shipping_country' => $wpsc_cart->delivery_country,
  40.         'billing_region'   => $wpsc_cart->selected_region,
  41.         'shipping_region'  => $wpsc_cart->delivery_region,
  42.         'base_shipping'    => 0,
  43.         'shipping_method'  => '',
  44.         'shipping_option'  => '',
  45.         'plugin_version'   => WPSC_VERSION,
  46.         'discount_value'   => $wpsc_cart->coupons_amount,
  47.         'discount_data'    => $wpsc_cart->coupons_name,
  48.         'find_us'          => '',
  49.         'wpec_taxes_total' => $tax,
  50.         'wpec_taxes_rate'  => $tax_percentage
  51.     );
  52.  
  53.     $purchase_log = new WPSC_Purchase_Log( $args );
  54.     $purchase_log->save();
  55.     $purchase_log_id = $purchase_log->get( 'id' );
  56.     $wpsc_checkout->save_forms_to_db( $purchase_log_id );
  57.     $wpsc_cart->save_to_db( $purchase_log_id );
  58.     $wpsc_cart->submit_stock_claims( $purchase_log_id );
  59.  
  60.     $wpsc_cart->log_id = $purchase_log_id;
  61.  
  62.     do_action( 'wpsc_submit_checkout', array( 'purchase_log_id' => $purchase_log_id, 'our_user_id' => $current_user->ID ) );
  63.     do_action( 'wpsc_submit_chekcout_gateway', $submitted_gateway, $purchase_log );
  64.  
  65.     // Submit to PayPal Digital Goods to request token
  66.     $options = array(
  67.         'cancel_url' => add_query_arg( array( 'cancel_payment' => $product->get_slug() ), $return_url )
  68.     );
  69.  
  70.     $gateway = wpsc_get_payment_gateway( $submitted_gateway );
  71.     $gateway->gateway->set_options( $options );
  72.  
  73.     $gateway->set_purchase_log( new WPSC_Purchase_Log( $purchase_log_id ) );
  74.     $checkout_url = $gateway->process( array( 'return_only' => true ) );
  75.  
  76.     // Store the confirmed_payment_redirect URL for this transaction
  77.     set_transient( 'wpec_payment_details_' . $sessionid, array( 'redirect_uri' => $return_url, 'product_slug' => $product->get_slug() ), 60 * 60 );
  78.  
  79.     return $checkout_url;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement