Advertisement
Guest User

Untitled

a guest
Sep 17th, 2013
609
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 30.07 KB | None | 0 0
  1. <?php
  2. /**
  3.  * This is the PayPal Payments Standard 2.0 Gateway.
  4.  * It uses the wpsc_merchant class as a base class which is handy for collating user details and cart contents.
  5.  */
  6.  
  7.  /*
  8.   * This is the gateway variable $nzshpcrt_gateways, it is used for displaying gateway information on the wp-admin pages and also
  9.   * for internal operations.
  10.   */
  11. $nzshpcrt_gateways[$num] = array(
  12.     'name' => __( 'PayPal Payments Standard 2.0', 'wpsc' ),
  13.     'api_version' => 2.0,
  14.     'image' => WPSC_URL . '/images/paypal.gif',
  15.     'class_name' => 'wpsc_merchant_paypal_standard',
  16.     'has_recurring_billing' => true,
  17.     'wp_admin_cannot_cancel' => true,
  18.     'display_name' => __( 'PayPal Payments Standard', 'wpsc' ),
  19.     'requirements' => array(
  20.         /// so that you can restrict merchant modules to PHP 5, if you use PHP 5 features
  21.         'php_version' => 4.3,
  22.          /// for modules that may not be present, like curl
  23.         'extra_modules' => array()
  24.     ),
  25.  
  26.     // this may be legacy, not yet decided
  27.     'internalname' => 'wpsc_merchant_paypal_standard',
  28.  
  29.     // All array members below here are legacy, and use the code in paypal_multiple.php
  30.     'form' => 'form_paypal_multiple',
  31.     'submit_function' => 'submit_paypal_multiple',
  32.     'payment_type' => 'paypal',
  33.     'supported_currencies' => array(
  34.         'currency_list' =>  array('AUD', 'BRL', 'CAD', 'CHF', 'CZK', 'DKK', 'EUR', 'GBP', 'HKD', 'HUF', 'ILS', 'JPY', 'MXN', 'MYR', 'NOK', 'NZD', 'PHP', 'PLN', 'SEK', 'SGD', 'THB', 'TWD', 'USD'),
  35.         'option_name' => 'paypal_curcode'
  36.     )
  37. );
  38.  
  39.  
  40.  
  41. /**
  42.     * WP eCommerce PayPal Standard Merchant Class
  43.     *
  44.     * This is the paypal standard merchant class, it extends the base merchant class
  45.     *
  46.     * @package wp-e-commerce
  47.     * @since 3.7.6
  48.     * @subpackage wpsc-merchants
  49. */
  50. class wpsc_merchant_paypal_standard extends wpsc_merchant {
  51.   var $name = '';
  52.   var $paypal_ipn_values = array();
  53.  
  54.   function __construct( $purchase_id = null, $is_receiving = false ) {
  55.     $this->name = __( 'PayPal Payments Standard', 'wpsc' );
  56.     parent::__construct( $purchase_id, $is_receiving );
  57.   }
  58.  
  59.     /**
  60.     * construct value array method, converts the data gathered by the base class code to something acceptable to the gateway
  61.     * @access public
  62.     */
  63.     function construct_value_array() {
  64.         $this->collected_gateway_data = $this->_construct_value_array();
  65.     }
  66.  
  67.     function convert( $amt ){
  68.         if ( empty( $this->rate ) ) {
  69.             $this->rate = 1;
  70.             $paypal_currency_code = $this->get_paypal_currency_code();
  71.             $local_currency_code = $this->get_local_currency_code();
  72.             if ( $local_currency_code != $paypal_currency_code ) {
  73.                 $curr=new CURRENCYCONVERTER();
  74.                 $this->rate = $curr->convert( 1, $paypal_currency_code, $local_currency_code );
  75.             }
  76.         }
  77.         return $this->format_price( $amt * $this->rate );
  78.     }
  79.  
  80.     function get_local_currency_code() {
  81.         if ( empty( $this->local_currency_code ) ) {
  82.             global $wpdb;
  83.             $this->local_currency_code = $wpdb->get_var( $wpdb->prepare( "SELECT `code` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id` = %d  LIMIT 1", get_option( 'currency_type' ) ) );
  84.         }
  85.  
  86.         return $this->local_currency_code;
  87.     }
  88.  
  89.     function get_paypal_currency_code() {
  90.         if ( empty( $this->paypal_currency_code ) ) {
  91.             global $wpsc_gateways;
  92.             $this->paypal_currency_code = $this->get_local_currency_code();
  93.  
  94.             if ( ! in_array( $this->paypal_currency_code, $wpsc_gateways['wpsc_merchant_paypal_standard']['supported_currencies']['currency_list'] ) )
  95.                 $this->paypal_currency_code = get_option( 'paypal_curcode', 'USD' );
  96.         }
  97.  
  98.         return $this->paypal_currency_code;
  99.     }
  100.  
  101.     /**
  102.     * construct value array method, converts the data gathered by the base class code to something acceptable to the gateway
  103.     * @access private
  104.     * @param boolean $aggregate Whether to aggregate the cart data or not. Defaults to false.
  105.     * @return array $paypal_vars The paypal vars
  106.     */
  107.     function _construct_value_array( $aggregate = false ) {
  108.         global $wpdb, $wpsc_cart;
  109.         $paypal_vars = array();
  110.         $add_tax = ! wpsc_tax_isincluded();
  111.  
  112.         $buy_now = defined( 'WPSC_PAYPAL_BUY_NOW' ) && WPSC_PAYPAL_BUY_NOW;
  113.  
  114.         $return_url = add_query_arg( 'sessionid', $this->cart_data['session_id'], $this->cart_data['transaction_results_url'] );
  115.  
  116.         if ( $buy_now )
  117.             $return_url = add_query_arg( 'wpsc_buy_now_return', 1, $return_url );
  118.  
  119.         // Store settings to be sent to paypal
  120.         $paypal_vars += array(
  121.             'business' => get_option( 'paypal_multiple_business' ),
  122.             'return' => $return_url,
  123.             'cancel_return' => $this->cart_data['transaction_results_url'],
  124.             'rm' => '2',
  125.             'currency_code' => $this->get_paypal_currency_code(),
  126.             'lc' => $this->cart_data['store_currency'],
  127.             'bn' => $this->cart_data['software_name'],
  128.  
  129.             'no_note' => '1',
  130.             'charset' => 'utf-8',
  131.         );
  132.  
  133.         // IPN data
  134.         if (get_option('paypal_ipn') == 1) {
  135.             $notify_url = $this->cart_data['notification_url'];
  136.             $notify_url = add_query_arg('gateway', 'wpsc_merchant_paypal_standard', $notify_url);
  137.             $notify_url = apply_filters('wpsc_paypal_standard_notify_url', $notify_url);
  138.             $paypal_vars += array(
  139.                 'notify_url' => $notify_url,
  140.             );
  141.         }
  142.  
  143.         // Shipping
  144.         if ( (bool) get_option( 'paypal_ship' ) && ! $buy_now ) {
  145.             $paypal_vars += array(
  146.                 'address_override' => '1',
  147.                 'no_shipping' => '0',
  148.             );
  149.  
  150.             // Customer details
  151.             $paypal_vars += array(
  152.                 'email' => $this->cart_data['email_address'],
  153.                 'first_name' => $this->cart_data['shipping_address']['first_name'],
  154.                 'last_name' => $this->cart_data['shipping_address']['last_name'],
  155.                 'address1' => $this->cart_data['shipping_address']['address'],
  156.                 'city' => $this->cart_data['shipping_address']['city'],
  157.                 'country' => $this->cart_data['shipping_address']['country'],
  158.                 'zip' => $this->cart_data['shipping_address']['post_code'],
  159.                 'state' => $this->cart_data['shipping_address']['state'],
  160.             );
  161.  
  162.             if ( $paypal_vars['country'] == 'UK' ) {
  163.                 $paypal_vars['country'] = 'GB';
  164.             }
  165.         }
  166.  
  167.         // Order settings to be sent to paypal
  168.         $paypal_vars += array(
  169.             'invoice' => $this->cart_data['session_id']
  170.         );
  171.  
  172.         if ( $buy_now )
  173.             $paypal_vars['custom'] = 'buy_now';
  174.  
  175.         // Two cases:
  176.         // - We're dealing with a subscription
  177.         // - We're dealing with a normal cart
  178.         if ( $this->cart_data['is_subscription'] ) {
  179.             $paypal_vars += array(
  180.                 'cmd'=> '_xclick-subscriptions',
  181.             );
  182.  
  183.             $reprocessed_cart_data['shopping_cart'] = array(
  184.                 'is_used' => false,
  185.                 'price' => 0,
  186.                 'length' => 1,
  187.                 'unit' => 'd',
  188.                 'times_to_rebill' => 1,
  189.             );
  190.  
  191.             $reprocessed_cart_data['subscription'] = array(
  192.                 'is_used' => false,
  193.                 'price' => 0,
  194.                 'length' => 1,
  195.                 'unit' => 'D',
  196.                 'times_to_rebill' => 1,
  197.             );
  198.  
  199.             foreach ( $this->cart_items as $cart_row ) {
  200.                 if ( $cart_row['is_recurring'] ) {
  201.                     $reprocessed_cart_data['subscription']['is_used'] = true;
  202.                     $reprocessed_cart_data['subscription']['price'] = $this->convert( $cart_row['price'] );
  203.                     $reprocessed_cart_data['subscription']['length'] = $cart_row['recurring_data']['rebill_interval']['length'];
  204.                     $reprocessed_cart_data['subscription']['unit'] = strtoupper( $cart_row['recurring_data']['rebill_interval']['unit'] );
  205.                     $reprocessed_cart_data['subscription']['times_to_rebill'] = $cart_row['recurring_data']['times_to_rebill'];
  206.                 } else {
  207.                     $item_cost = ( $cart_row['price'] + $cart_row['shipping'] + $cart_row['tax'] ) * $cart_row['quantity'];
  208.  
  209.                     if ( $item_cost > 0 ) {
  210.                         $reprocessed_cart_data['shopping_cart']['price'] += $item_cost;
  211.                         $reprocessed_cart_data['shopping_cart']['is_used'] = true;
  212.                     }
  213.                 }
  214.  
  215.                 $paypal_vars += array(
  216.                     'item_name' => apply_filters( 'the_title', $cart_row['name'] ),
  217.                     // I fail to see the point of sending a subscription to paypal as a subscription
  218.                     // if it does not recur, if (src == 0) then (this == underfeatured waste of time)
  219.                     'src' => '1'
  220.                 );
  221.  
  222.                 // This can be false, we don't need to have additional items in the cart/
  223.                 if ( $reprocessed_cart_data['shopping_cart']['is_used'] ) {
  224.                     $paypal_vars += array(
  225.                         "a1" => $this->convert( $reprocessed_cart_data['shopping_cart']['price'] ),
  226.                         "p1" => $reprocessed_cart_data['shopping_cart']['length'],
  227.                         "t1" => $reprocessed_cart_data['shopping_cart']['unit'],
  228.                     );
  229.                 }
  230.  
  231.                 // We need at least one subscription product,
  232.                 // If this is not true, something is rather wrong.
  233.                 if ( $reprocessed_cart_data['subscription']['is_used'] ) {
  234.                     $paypal_vars += array(
  235.                         "a3" => $this->convert( $reprocessed_cart_data['subscription']['price'] ),
  236.                         "p3" => $reprocessed_cart_data['subscription']['length'],
  237.                         "t3" => $reprocessed_cart_data['subscription']['unit'],
  238.                     );
  239.  
  240.                     // If the srt value for the number of times to rebill is not greater than 1,
  241.                     // paypal won't accept the transaction.
  242.                     if ( $reprocessed_cart_data['subscription']['times_to_rebill'] > 1 ) {
  243.                         $paypal_vars += array(
  244.                             'srt' => $reprocessed_cart_data['subscription']['times_to_rebill'],
  245.                         );
  246.                     }
  247.                 }
  248.             } // end foreach cart item
  249.         } else {
  250.             if ( $buy_now )
  251.                 $paypal_vars['cmd'] = '_xclick';
  252.             else
  253.                 $paypal_vars += array(
  254.                     'upload' => '1',
  255.                     'cmd' => '_ext-enter',
  256.                     'redirect_cmd' => '_cart',
  257.                 );
  258.             $free_shipping = false;
  259.             $coupon = wpsc_get_customer_meta( 'coupon' );
  260.             if ( $coupon ) {
  261.                 $coupon = new wpsc_coupons( $coupon );
  262.                 $free_shipping = $coupon->is_percentage == '2';
  263.             }
  264.  
  265.             if ( $this->cart_data['has_discounts'] && $free_shipping )
  266.                 $handling = 0;
  267.             else
  268.                 $handling = $this->cart_data['base_shipping'];
  269.  
  270.             $tax_total = 0;
  271.             if ( $add_tax )
  272.                 $tax_total = $this->cart_data['cart_tax'];
  273.  
  274.             // Set base shipping
  275.             $paypal_vars += array(
  276.                 'handling_cart' => $this->convert( $handling )
  277.             );
  278.  
  279.             // Stick the cart item values together here
  280.             $i = 1;
  281.  
  282.             if ( ! $buy_now ) {
  283.                 if ( ! $aggregate ) {
  284.                     foreach ( $this->cart_items as $cart_row ) {
  285.                         $item_number = get_post_meta( $cart_row['product_id'], '_wpsc_sku', true );
  286.                         if ( ! $item_number )
  287.                             $item_number = $cart_row['product_id'];
  288.  
  289.                         $paypal_vars += array(
  290.                             "item_name_$i" => apply_filters( 'the_title', $cart_row['name'] ),
  291.                             "amount_$i" => $this->convert( $cart_row['price'] ),
  292.                             "quantity_$i" => $cart_row['quantity'],
  293.                             "item_number_$i" => $item_number,
  294.                         );
  295.  
  296.                         if ( ! $free_shipping )
  297.                             $paypal_vars += array(
  298.                                 // additional shipping for the the (first item / total of the items)
  299.                                 "shipping_$i" => $this->convert( $cart_row['shipping'] / $cart_row['quantity'] ),
  300.                                 // additional shipping beyond the first item
  301.                                 "shipping2_$i" => $this->convert( $cart_row['shipping'] / $cart_row['quantity'] ),
  302.                                 "handling_$i" => '',
  303.                             );
  304.  
  305.                         if ( $add_tax && ! empty( $cart_row['tax'] ) )
  306.                             $tax_total += $cart_row['tax'];
  307.                         ++$i;
  308.                     }
  309.  
  310.                     if ( $this->cart_data['has_discounts'] && ! $free_shipping ) {
  311.                         $paypal_vars['discount_amount_cart'] = $this->convert( $this->cart_data['cart_discount_value'] );
  312.                         $subtotal = $wpsc_cart->calculate_subtotal();
  313.                         if ( $this->cart_data['cart_discount_value'] >= $wpsc_cart->calculate_subtotal() ) {
  314.                             $paypal_vars['discount_amount_cart'] = $this->convert( $subtotal ) - 0.01;
  315.                             if ( ! empty( $paypal_vars['handling_cart'] ) )
  316.                                 $paypal_vars['handling_cart'] -= 0.01;
  317.                         }
  318.                     }
  319.  
  320.                 } else {
  321.                     $paypal_vars['item_name_'.$i] = __( "Your Shopping Cart", 'wpsc' );
  322.                     $paypal_vars['amount_'.$i] = $this->convert( $this->cart_data['total_price'] ) - $this->convert( $this->cart_data['base_shipping'] );
  323.                     $paypal_vars['quantity_'.$i] = 1;
  324.                     $paypal_vars['shipping_'.$i] = 0;
  325.                     $paypal_vars['shipping2_'.$i] = 0;
  326.                     $paypal_vars['handling_'.$i] = 0;
  327.                 }
  328.  
  329.                 $paypal_vars['tax_cart'] = $this->convert( $tax_total );
  330.             } else {
  331.                 $cart_row = $this->cart_items[0];
  332.                 $item_number = get_post_meta( $cart_row['product_id'], '_wpsc_sku', true );
  333.                 $paypal_vars += array(
  334.                     'item_name'     => apply_filters( 'the_title', $cart_row['name'] ),
  335.                     'item_number'   => $item_number,
  336.                     'amount'        => $this->convert( $cart_row['price'] ),
  337.                     'quantity'      => $cart_row['quantity'],
  338.                     'handling'      => $this->convert( $handling ),
  339.                 );
  340.             }
  341.         }
  342.         return apply_filters( 'wpsc_paypal_standard_post_data', $paypal_vars );
  343.     }
  344.  
  345.     /**
  346.     * submit method, sends the received data to the payment gateway
  347.     * @access public
  348.     */
  349.     function submit() {
  350.         $name_value_pairs = array();
  351.         foreach ( $this->collected_gateway_data as $key => $value ) {
  352.             $name_value_pairs[] = $key . '=' . urlencode( $value );
  353.         }
  354.         $gateway_values = implode( '&', $name_value_pairs );
  355.  
  356.         $redirect = get_option( 'paypal_multiple_url' ) . "?" . $gateway_values;
  357.         // URLs up to 2083 characters long are short enough for an HTTP GET in all browsers.
  358.         // Longer URLs require us to send aggregate cart data to PayPal short of losing data.
  359.         // An exception is made for recurring transactions, since there isn't much we can do.
  360.         if ( strlen( $redirect ) > 2083 && ! $this->cart_data['is_subscription'] ) {
  361.             $name_value_pairs = array();
  362.             foreach( $this->_construct_value_array( true ) as $key => $value ) {
  363.                 $name_value_pairs[]= $key . '=' . urlencode( $value );
  364.             }
  365.             $gateway_values = implode( '&', $name_value_pairs );
  366.  
  367.             $redirect = get_option( 'paypal_multiple_url' ) . "?" . $gateway_values;
  368.         }
  369.  
  370.         if ( defined( 'WPSC_ADD_DEBUG_PAGE' ) && WPSC_ADD_DEBUG_PAGE ) {
  371.             echo "<a href='" . esc_url( $redirect ) . "'>" . __( "Test the URL here", 'wpsc' ) . "</a>";
  372.             echo "<pre>" . print_r( $this->collected_gateway_data, true ) . "</pre>";
  373.             exit();
  374.         } else {
  375.             if ( defined( 'WPSC_PAYPAL_BUY_NOW' ) && WPSC_PAYPAL_BUY_NOW )
  376.                 wpsc_empty_cart();
  377.             wp_redirect( $redirect );
  378.             exit();
  379.         }
  380.     }
  381.  
  382.  
  383.     /**
  384.     * parse_gateway_notification method, receives data from the payment gateway
  385.     * @access private
  386.     */
  387.     function parse_gateway_notification() {
  388.         /// PayPal first expects the IPN variables to be returned to it within 30 seconds, so we do this first.
  389.         $paypal_url = get_option( 'paypal_multiple_url' );
  390.         $received_values = array();
  391.         $received_values['cmd'] = '_notify-validate';
  392.         $received_values += stripslashes_deep( $_REQUEST );
  393.         $options = array(
  394.             'timeout' => 20,
  395.             'body' => $received_values,
  396.             'user-agent' => ( 'WP e-Commerce/' . WPSC_PRESENTABLE_VERSION )
  397.         );
  398.  
  399.         $response = wp_remote_post( $paypal_url, $options );
  400.         if ( 'VERIFIED' == $response['body'] ) {
  401.             $this->paypal_ipn_values = $received_values;
  402.             $this->session_id = $received_values['invoice'];
  403.         } else {
  404.             exit( "IPN Request Failure" );
  405.         }
  406.     }
  407.  
  408.     private function import_ipn_data() {
  409.         global $wpdb;
  410.  
  411.         $purchase_log = new WPSC_Purchase_Log( $this->cart_data['session_id'], 'sessionid' );
  412.         if ( ! $purchase_log->exists() )
  413.             return;
  414.  
  415.         // get all active form fields and organize them based on id and unique_name, because we're only
  416.         // importing fields relevant to checkout fields that have unique name
  417.         $form_fields_sql     = "SELECT id, unique_name FROM " . WPSC_TABLE_CHECKOUT_FORMS . " WHERE active='1'";
  418.         $form_fields_results = $wpdb->get_results( $form_fields_sql );
  419.         $form_fields         = array();
  420.  
  421.         foreach ( $form_fields_results as $row ) {
  422.             if ( ! empty( $row->unique_name ) )
  423.                 $form_fields[$row->id] = $row->unique_name;
  424.         }
  425.  
  426.         $purchase_log_id = $purchase_log->get( 'id' );
  427.  
  428.         // this defines how ipn response data will be parsed into checkout field values
  429.         $field_mapping = array(
  430.             'firstname' => 'first_name',
  431.             'lastname'  => 'last_name',
  432.             'country'   => 'address_country_code',
  433.             'email'     => 'payer_email',
  434.             'city'      => 'address_city',
  435.             'address'   => 'address_street',
  436.             'phone'     => 'contact_phone',
  437.         );
  438.  
  439.         $inserts = array();
  440.  
  441.         // billing & shipping will get the same values
  442.         foreach ( array( 'billing', 'shipping' ) as $type ) {
  443.             // if the corresponding checkout field is "active", prepare the data array that will
  444.             // get passed into $wpdb->insert()
  445.             foreach ( $field_mapping as $key => $value ) {
  446.                 $unique_name = $type . $key;
  447.                 $id = array_search( $unique_name, $form_fields );
  448.                 if ( $id === false || ! isset( $this->paypal_ipn_values[$value] ) )
  449.                     continue;
  450.  
  451.                 $inserts[] = array(
  452.                     'log_id'  => $purchase_log_id,
  453.                     'form_id' => $id,
  454.                     'value'   => $this->paypal_ipn_values[$value],
  455.                 );
  456.             }
  457.         }
  458.  
  459.         // loop through the prepared data array and insert them
  460.         foreach ( $inserts as $insert ) {
  461.             $wpdb->insert(
  462.                 WPSC_TABLE_SUBMITTED_FORM_DATA,
  463.                 $insert,
  464.                 array(
  465.                     '%d',
  466.                     '%d',
  467.                     '%s',
  468.                 )
  469.             );
  470.         }
  471.     }
  472.  
  473.     /**
  474.     * process_gateway_notification method, receives data from the payment gateway
  475.     * @access public
  476.     */
  477.     function process_gateway_notification() {
  478.         global $wpdb;
  479.  
  480.         $status = false;
  481. switch ( strtolower( $this->paypal_ipn_values['payment_status'] ) ) {
  482. case 'pending':
  483. if ($this->paypal_ipn_values['pending_reason'] == 'multi_currency'):
  484. $status = 3;
  485. else :
  486. $status = 2;
  487. endif;
  488. break;
  489. case 'completed':
  490. $status = 3;
  491. break;
  492. case 'denied':
  493. $status = 6;
  494. break;
  495. }
  496.  
  497.         do_action( 'wpsc_paypal_standard_ipn', $this->paypal_ipn_values, $this );
  498.         $paypal_email = strtolower( get_option( 'paypal_multiple_business' ) );
  499.  
  500.       // Compare the received store owner email address to the set one
  501.         if ( strtolower( $this->paypal_ipn_values['receiver_email'] ) == $paypal_email || strtolower( $this->paypal_ipn_values['business'] ) == $paypal_email ) {
  502.             switch ($this->paypal_ipn_values['txn_type']) {
  503.                 case 'cart':
  504.                 case 'express_checkout':
  505.                 case 'web_accept':
  506.                     // import shipping & billing details if this is from "Buy Now" button
  507.                     if ( isset( $this->paypal_ipn_values['custom'] ) && $this->paypal_ipn_values['custom'] == 'buy_now' ) {
  508.                         $this->import_ipn_data();
  509.                     }
  510.  
  511.                     if ( $status )
  512.                         $this->set_transaction_details( $this->paypal_ipn_values['txn_id'], $status );
  513.                     if ( in_array( $status, array( 2, 3 ) ) )
  514.                         transaction_results( $this->cart_data['session_id'], false );
  515.                     break;
  516.  
  517.                 case 'subscr_signup':
  518.                 case 'subscr_payment':
  519.                     if ( in_array( $status, array( 2, 3 ) ) ) {
  520.                         $this->set_transaction_details( $this->paypal_ipn_values['subscr_id'], $status );
  521.                         transaction_results( $this->cart_data['session_id'], false );
  522.                     }
  523.                     foreach ( $this->cart_items as $cart_row ) {
  524.                         if ( $cart_row['is_recurring'] == true ) {
  525.                             do_action('wpsc_activate_subscription', $cart_row['cart_item_id'], $this->paypal_ipn_values['subscr_id']);
  526.                             do_action('wpsc_activated_subscription', $cart_row['cart_item_id'], $this );
  527.                         }
  528.                     }
  529.                     break;
  530.  
  531.                 case 'subscr_cancel':
  532.                     do_action( 'wpsc_paypal_standard_deactivate_subscription', $this->paypal_ipn_values['subscr_id'], $this );
  533.                 case 'subscr_eot':
  534.                 case 'subscr_failed':
  535.                     foreach ( $this->cart_items as $cart_row ) {
  536.                         $altered_count = 0;
  537.                         if ( (bool)$cart_row['is_recurring'] == true ) {
  538.                             $altered_count++;
  539.                             wpsc_update_cartmeta( $cart_row['cart_item_id'], 'is_subscribed', 0 );
  540.                         }
  541.                     }
  542.                     break;
  543.  
  544.                 default:
  545.                     break;
  546.             }
  547.         }
  548.     }
  549.  
  550.  
  551.  
  552.     function format_price( $price, $paypal_currency_code = null ) {
  553.         if ( ! isset( $paypal_currency_code ) ) {
  554.             $paypal_currency_code = get_option( 'paypal_curcode' );
  555.         }
  556.         switch( $paypal_currency_code ) {
  557.             case "JPY":
  558.                 $decimal_places = 0;
  559.                 break;
  560.  
  561.             case "HUF":
  562.                 $decimal_places = 0;
  563.  
  564.             default:
  565.                 $decimal_places = 2;
  566.                 break;
  567.         }
  568.         $price = number_format( sprintf( "%01.2f", $price ), $decimal_places, '.', '' );
  569.         return $price;
  570.     }
  571. }
  572.  
  573.  
  574. /**
  575.  * submit_paypal_multiple function.
  576.  *
  577.  * Use this for now, but it will eventually be replaced with a better form API for gateways
  578.  * @access public
  579.  * @return void
  580.  */
  581. function submit_paypal_multiple (){
  582.     if ( isset ( $_POST['paypal_multiple_business'] ) ) {
  583.         update_option( 'paypal_multiple_business', $_POST['paypal_multiple_business'] );
  584.     }
  585.  
  586.     if ( isset ( $_POST['paypal_multiple_url'] ) ) {
  587.         update_option( 'paypal_multiple_url', $_POST['paypal_multiple_url'] );
  588.     }
  589.  
  590.     if ( isset ( $_POST['paypal_curcode'] ) ) {
  591.         update_option( 'paypal_curcode', $_POST['paypal_curcode'] );
  592.     }
  593.  
  594.     if ( isset ( $_POST['paypal_curcode'] ) ) {
  595.         update_option( 'paypal_curcode', $_POST['paypal_curcode'] );
  596.     }
  597.  
  598.     if ( isset ( $_POST['paypal_ipn'] ) ) {
  599.         update_option( 'paypal_ipn', (int)$_POST['paypal_ipn'] );
  600.     }
  601.  
  602.     if ( isset ( $_POST['address_override'] ) ) {
  603.         update_option( 'address_override', (int)$_POST['address_override'] );
  604.     }
  605.     if ( isset ( $_POST['paypal_ship'] ) ) {
  606.         update_option( 'paypal_ship', (int)$_POST['paypal_ship'] );
  607.     }
  608.  
  609.     if ( ! isset( $_POST['paypal_form'] ) )
  610.         $_POST['paypal_form'] = array();
  611.  
  612.     foreach( (array)$_POST['paypal_form'] as $form => $value ) {
  613.         update_option( ( 'paypal_form_' . $form ), $value );
  614.     }
  615.  
  616.     return true;
  617. }
  618.  
  619.  
  620.  
  621. /**
  622.  * form_paypal_multiple function.
  623.  *
  624.  * Use this for now, but it will eventually be replaced with a better form API for gateways
  625.  * @access public
  626.  * @return void
  627.  */
  628. function form_paypal_multiple() {
  629.     global $wpdb, $wpsc_gateways;
  630.  
  631.     $account_type = get_option( 'paypal_multiple_url' );
  632.     $account_types = array(
  633.         'https://www.paypal.com/cgi-bin/webscr' => __( 'Live Account', 'wpsc' ),
  634.         'https://www.sandbox.paypal.com/cgi-bin/webscr' => __( 'Sandbox Account', 'wpsc' ),
  635.     );
  636.  
  637.     $output = "
  638.     <tr>
  639.         <td>" . __( 'Username:', 'wpsc' ) . "</td>
  640.         <td>
  641.             <input type='text' size='40' value='" . get_option( 'paypal_multiple_business') . "' name='paypal_multiple_business' />
  642.             <p class='description'>
  643.                 " . __( 'This is your PayPal email address.', 'wpsc' ) . "
  644.             </p>
  645.         </td>
  646.     </tr>
  647.  
  648.     <tr>
  649.         <td>" . __( 'Account Type:', 'wpsc' ) . "</td>
  650.         <td>
  651.             <select name='paypal_multiple_url'>\n";
  652.  
  653.     foreach ( $account_types as $url => $label ) {
  654.         $output .= "<option value='{$url}' ". selected( $url, $account_type, false ) . ">" . esc_html( $label ) . "</option>";
  655.     }
  656.  
  657.     $output .= "
  658.             </select>
  659.             <p class='description'>
  660.                 " . __( 'If you have a PayPal developers Sandbox account please use Sandbox mode, if you just have a standard PayPal account then you will want to use Live mode.', 'wpsc' ) . "
  661.             </p>
  662.         </td>
  663.     </tr>\n";
  664.  
  665.     $paypal_ipn = get_option( 'paypal_ipn' );
  666.     $paypal_ipn1 = "";
  667.     $paypal_ipn2 = "";
  668.     switch( $paypal_ipn ) {
  669.         case 0:
  670.             $paypal_ipn2 = "checked='checked'";
  671.             break;
  672.  
  673.         case 1:
  674.             $paypal_ipn1 = "checked='checked'";
  675.             break;
  676.     }
  677.  
  678.     $paypal_ship = get_option( 'paypal_ship' );
  679.     $paypal_ship1 = "";
  680.     $paypal_ship2 = "";
  681.     switch( $paypal_ship ){
  682.         case 1:
  683.             $paypal_ship1 = "checked='checked'";
  684.             break;
  685.  
  686.         case 0:
  687.         default:
  688.             $paypal_ship2 = "checked='checked'";
  689.             break;
  690.  
  691.     }
  692.  
  693.     $address_override = get_option( 'address_override' );
  694.     $address_override1 = "";
  695.     $address_override2 = "";
  696.     switch( $address_override ) {
  697.         case 1:
  698.             $address_override1 = "checked='checked'";
  699.             break;
  700.  
  701.         case 0:
  702.             default:
  703.             $address_override2 = "checked='checked'";
  704.             break;
  705.     }
  706.     $output .= "
  707.     <tr>
  708.         <td>" . __( "IPN", 'wpsc' ) . ":</td>
  709.         <td>
  710.             <input type='radio' value='1' name='paypal_ipn' id='paypal_ipn1' " . $paypal_ipn1 . " /> <label for='paypal_ipn1'>" . __( 'Yes', 'wpsc' ) . "</label> &nbsp;
  711.             <input type='radio' value='0' name='paypal_ipn' id='paypal_ipn2' " . $paypal_ipn2 . " /> <label for='paypal_ipn2'>" . __( 'No', 'wpsc' ) . "</label>
  712.             <p class='description'>
  713.                 " . __( "IPN (instant payment notification) will automatically update your sales logs to 'Accepted payment' when a customers payment is successful. For IPN to work you also need to have IPN turned on in your Paypal settings. If it is not turned on, the sales sill remain as 'Order Pending' status until manually changed. It is highly recommend using IPN, especially if you are selling digital products.", 'wpsc' ) . "
  714.             </p>
  715.         </td>
  716.     </tr>
  717.     <tr>
  718.         <td style='padding-bottom: 0px;'>" . __( "Send shipping details", 'wpsc' ) . "</td>
  719.         <td style='padding-bottom: 0px;'>
  720.             <input type='radio' value='1' name='paypal_ship' id='paypal_ship1' " . $paypal_ship1 . " /> <label for='paypal_ship1'>" . __( 'Yes', 'wpsc' ) . "</label> &nbsp;
  721.             <input type='radio' value='0' name='paypal_ship' id='paypal_ship2' " . $paypal_ship2 . " /> <label for='paypal_ship2'>" . __( 'No', 'wpsc' ) . "</label>
  722.             <p class='description'>
  723.                 " . __( "Note: If your checkout page does not have a shipping details section, or if you don't want to send Paypal shipping information. You should change Send shipping details option to No.", 'wpsc' ) . "
  724.             </p>
  725.         </td>
  726.     </tr>
  727.     <tr>
  728.         <td>
  729.             " . __( 'Address Override:', 'wpsc' ) . "
  730.         </td>
  731.         <td>
  732.             <input type='radio' value='1' name='address_override' id='address_override1' " . $address_override1 . " /> <label for='address_override1'>" . __( 'Yes', 'wpsc' ) . "</label> &nbsp;
  733.             <input type='radio' value='0' name='address_override' id='address_override2' " . $address_override2 . " /> <label for='address_override2'>" . __( 'No', 'wpsc' ) . "</label>
  734.             <p class='description'>
  735.                 " . __( "This setting affects your PayPal purchase log. If your customers already have a PayPal account PayPal will try to populate your PayPal Purchase Log with their PayPal address. This setting tries to replace the address in the PayPal purchase log with the Address customers enter on your Checkout page.", 'wpsc' ) . "
  736.             </p>
  737.         </td>
  738.     </tr>\n";
  739.  
  740.     $store_currency_data = $wpdb->get_row( $wpdb->prepare( "SELECT `code`, `currency` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `id` IN (%d)", get_option( 'currency_type' ) ), ARRAY_A );
  741.     $current_currency = get_option('paypal_curcode');
  742.     if ( ( $current_currency == '' ) && in_array( $store_currency_data['code'], $wpsc_gateways['wpsc_merchant_paypal_standard']['supported_currencies']['currency_list'] ) ) {
  743.         update_option( 'paypal_curcode', $store_currency_data['code'] );
  744.         $current_currency = $store_currency_data['code'];
  745.     }
  746.  
  747.     if ( $current_currency != $store_currency_data['code'] ) {
  748.         $output .= "
  749.         <tr>
  750.             <td>
  751.             </td>
  752.             <td><strong class='form_group'>" . __( 'Currency Converter', 'wpsc' ) . "</td>
  753.         </tr>
  754.         <tr>
  755.             <td>
  756.             </td>
  757.             <td>
  758.             ".sprintf( __( 'Your website uses <strong>%s</strong>. This currency is not supported by PayPal, please  select a currency using the drop down menu below. Buyers on your site will still pay in your local currency however we will send the order through to Paypal using the currency you choose below.', 'wpsc' ), $store_currency_data['currency'] )."
  759.             </td>
  760.         </tr>
  761.  
  762.         <tr>
  763.             <td>
  764.                 " . __( 'Select Currency:', 'wpsc' ) . "
  765.             </td>
  766.             <td>
  767.                 <select name='paypal_curcode'>\n";
  768.  
  769.         $paypal_currency_list = array_map( 'esc_sql', $wpsc_gateways['wpsc_merchant_paypal_standard']['supported_currencies']['currency_list'] );
  770.  
  771.         $currency_list = $wpdb->get_results( "SELECT DISTINCT `code`, `currency` FROM `".WPSC_TABLE_CURRENCY_LIST."` WHERE `code` IN ('" . implode( "','", $paypal_currency_list ) . "')", ARRAY_A );
  772.  
  773.         foreach ( $currency_list as $currency_item ) {
  774.             $selected_currency = '';
  775.             if ( $current_currency == $currency_item['code'] ) {
  776.                 $selected_currency = "selected='selected'";
  777.             }
  778.             $output .= "<option " . $selected_currency . " value='{$currency_item['code']}'>{$currency_item['currency']}</option>";
  779.         }
  780.         $output .= "
  781.                 </select>
  782.             </td>
  783.         </tr>\n";
  784.     }
  785.  
  786.  
  787. $output .= "
  788.     <tr>
  789.         <td colspan='2'>
  790.             <strong class='form_group'>" . __( 'Forms Sent to Gateway', 'wpsc' ) . "</strong>
  791.         </td>
  792.     </tr>
  793.  
  794.     <tr>
  795.         <td>" . __( 'First Name Field', 'wpsc' ) . "</td>
  796.         <td>
  797.             <select name='paypal_form[first_name]'>
  798.             " . nzshpcrt_form_field_list( get_option( 'paypal_form_first_name' ) ) . "
  799.             </select>
  800.         </td>
  801.     </tr>
  802.     <tr>
  803.         <td>" . __( 'Last Name Field', 'wpsc' ) . "</td>
  804.         <td>
  805.             <select name='paypal_form[last_name]'>
  806.             " . nzshpcrt_form_field_list( get_option( 'paypal_form_last_name' ) ) . "
  807.             </select>
  808.         </td>
  809.     </tr>
  810.     <tr>
  811.         <td>
  812.         " . __( 'Address Field', 'wpsc' ) . "
  813.         </td>
  814.         <td>
  815.             <select name='paypal_form[address]'>
  816.             " . nzshpcrt_form_field_list( get_option( 'paypal_form_address' ) ) . "
  817.             </select>
  818.         </td>
  819.     </tr>
  820.     <tr>
  821.         <td>
  822.         " . __( 'City Field', 'wpsc' ) . "
  823.         </td>
  824.         <td>
  825.             <select name='paypal_form[city]'>
  826.             " . nzshpcrt_form_field_list( get_option( 'paypal_form_city' ) ) . "
  827.             </select>
  828.         </td>
  829.     </tr>
  830.     <tr>
  831.         <td>
  832.         " . __( 'State Field', 'wpsc' ) . "
  833.         </td>
  834.         <td>
  835.             <select name='paypal_form[state]'>
  836.             " . nzshpcrt_form_field_list( get_option( 'paypal_form_state' ) ) . "
  837.             </select>
  838.         </td>
  839.     </tr>
  840.     <tr>
  841.         <td>
  842.         " . __( 'Postal / ZIP Code Field', 'wpsc' ) . "
  843.         </td>
  844.         <td>
  845.             <select name='paypal_form[post_code]'>
  846.             ".nzshpcrt_form_field_list(get_option('paypal_form_post_code'))."
  847.             </select>
  848.         </td>
  849.     </tr>
  850.     <tr>
  851.         <td>
  852.         " . __( 'Country Field', 'wpsc' ) . "
  853.         </td>
  854.         <td>
  855.             <select name='paypal_form[country]'>
  856.             " . nzshpcrt_form_field_list( get_option( 'paypal_form_country' ) ) . "
  857.             </select>
  858.         </td>
  859.     </tr>
  860.     <tr>
  861.         <td colspan='2'>
  862.             <p class='description'>
  863.             " . sprintf( __( "For more help configuring Paypal Standard, please read our documentation <a href='%s'>here</a>", 'wpsc' ), esc_url( 'http://docs.getshopped.org/documentation/paypal-payments-standard/' ) ) . "
  864.             </p>
  865.         </td>
  866.     </tr>\n";
  867.  
  868.     return $output;
  869. }
  870.  
  871. function _wpsc_buy_now_callback() {
  872.     global $wpsc_cart, $user_ID;
  873.  
  874.     $paypal_url = get_option( 'paypal_multiple_url' );
  875.     $_POST['custom_gateway'] = 'wpsc_merchant_paypal_standard';
  876.     define( "WPSC_PAYPAL_BUY_NOW", true );
  877.     wpsc_add_to_cart();
  878.     wpsc_submit_checkout( false );
  879. }
  880.  
  881. if ( isset( $_REQUEST['wpsc_buy_now_callback'] ) && $_REQUEST['wpsc_buy_now_callback'] )
  882.     add_action( 'init', '_wpsc_buy_now_callback' );
  883.  
  884. function _wpsc_buy_now_transaction_results() {
  885.     if ( ! isset( $_REQUEST['sessionid'] ) )
  886.         return;
  887.  
  888.     $purchase_log = new WPSC_Purchase_Log( $_REQUEST['sessionid'], 'sessionid' );
  889.  
  890.     if ( ! $purchase_log->exists() || $purchase_log->is_transaction_completed() )
  891.         return;
  892.  
  893.     $purchase_log->set( 'processed', WPSC_Purchase_Log::ORDER_RECEIVED );
  894.     $purchase_log->save();
  895. }
  896.  
  897. if ( isset( $_REQUEST['wpsc_buy_now_return'] ) && $_REQUEST['wpsc_buy_now_return'] )
  898.     add_action( 'init', '_wpsc_buy_now_transaction_results' );
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement