Paypal standard fix - allows showing of items in Paypal

This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.
13 years ago
Andrei,

All right... that fixed it! I now have code that can support gift cards, reward points AND checkout attributes!!

Here is the way that it works:

When a customer is going through the cart in nopCommerce and uses gift cards/reward points then the value of the gift card/rewared points is subtracted from the cart total. Now when you hit the PaypalStandardPaymentProcessor.cs file I run through all of the items and get a total of what the cart SHOULD be and then subtract the cart's ACTUAL total and that equals the gift card/reward point amount. That amount is then passed to Paypal as a "discount" and the proper total that matches the nopCommerce cart total is displayed in Paypal.

Checkout attributes that add dollar value to the cart are treated as items and sent to Paypal to be paid for, attributes that have no monetary value are ignored and can be found in the nopCommerce order after they come back from Paypal.

I'll post the code later on today... have to clean it up.

One question for you though...

if the order had not been saved to the database already, would the code that I posted work and get me the attributes?


B
13 years ago
Andrei,

Here is the final code... supports gift cards, reward points and checkout attributes!

public string PostProcessPayment(Order order)
{
   StringBuilder builder = new StringBuilder();
   string returnURL = CommonHelper.GetStoreLocation(false) + "PaypalPDTHandler.aspx";
   string cancel_returnURL = CommonHelper.GetStoreLocation(false) + "PaypalCancel.aspx";
   builder.Append(GetPaypalUrl());
   builder.AppendFormat("?cmd=_cart&business={0}", HttpUtility.UrlEncode(businessEmail));
   builder.AppendFormat("&upload=1");

   //get the items in the cart
   var cartItems = order.NpOrderProductVariants;
   int x = 1; //items must start at 1
   decimal cartTotal = decimal.Zero;
   foreach (var item in cartItems) //loop through the cart items
   {
      //get the productvariant so we can get the name
      var pName = ProductManager.GetProductVariantById(item.ProductVariantId);
      string vName = pName.FullProductName; //set the name
      builder.AppendFormat("&item_name_" + x + "={0}", HttpUtility.UrlEncode(vName)); //name
      builder.AppendFormat("&amount_" + x + "={0}", item.UnitPriceExclTax); //amount
      builder.AppendFormat("&quantity_" + x + "={0}", item.Quantity); //quantity
      x++; //increment item number
      cartTotal = cartTotal + item.PriceExclTax; //keep a running cart total
   }

   //the checkout attributes that have a dollar value and send them to Paypal as items to be paid for
   var caValues = CheckoutAttributeHelper.ParseCheckoutAttributeValues(order.CheckoutAttributesXml);
   foreach (var val in caValues)
   {
      var attPrice = TaxManager.GetCheckoutAttributePrice(val, false, NopContext.Current.User);
      if (attPrice > 0) //if it has a price
      {
         var caName = CheckoutAttributeManager.GetCheckoutAttributeById(val.CheckoutAttributeId);
         var attName = caName.LocalizedName; //set the name
         builder.AppendFormat("&item_name_" + x + "={0}", attName); //name
         builder.AppendFormat("&amount_" + x + "={0}", attPrice); //amount
         builder.AppendForma&quantity_" + x + "={0}", 1); //quantity
         x++;
      }
   }

   //add tax as an item - if tax not configured in Paypal
   //builder.AppendFormat("&item_name_" + x + "={0}", "Sales Tax");
   //builder.AppendFormat("&amount_" + x + "={0}", order.OrderTax);

   cartTotal = cartTotal + order.OrderShippingExclTax + order.OrderTax;
   decimal discountTotal = decimal.Zero;
   if (cartTotal > order.OrderTotal) //else we're not even going to Paypal :)
   {
      /*/ Take the difference between what the order total is and what it should be and use that as the "discount".
      The difference equals the amount of the gift card and/or reward points used. /*/

      discountTotal = cartTotal - order.OrderTotal;
   }

   //gift card or rewared point amount applied to cart in nopCommerce - shows in Paypal as "discount"
   builder.AppendFormat("&discount_amount_cart={0}", discountTotal);

   builder.AppendFormat("&custom={0}", order.OrderGuid);
   builder.Append(string.Format("&no_note=1&currency_code={0}", HttpUtility.UrlEncode(CurrencyManager.PrimaryStoreCurrency.CurrencyCode)));
   builder.AppendFormat("&invoice={0}", order.OrderId);
   builder.AppendFormat("&rm=2", new object[0]);
   if (order.ShippingStatus != ShippingStatusEnum.ShippingNotRequired)
      builder.AppendFormat("&no_shipping=2", new object[0]);
   else
      builder.AppendFormat("&no_shipping=1", new object[0]);
   builder.AppendFormat("&shipping_1={0}", order.OrderShippingExclTax);

   //can use "handling" for extra charges - will be added to "shipping & handling"
   builder.AppendFormat("&handling_1={0}", GetAdditionalHandlingFee());

   builder.AppendFormat("&return={0}&cancel_return={1}", HttpUtility.UrlEncode(returnURL), HttpUtility.UrlEncode(cancel_returnURL));
   builder.AppendFormat("&first_name={0}", HttpUtility.UrlEncode(order.BillingFirstName));
   builder.AppendFormat("&last_name={0}", HttpUtility.UrlEncode(order.BillingLastName));
   builder.AppendFormat("&address1={0}", HttpUtility.UrlEncode(order.BillingAddress1));
   builder.AppendFormat("&address2={0}", HttpUtility.UrlEncode(order.BillingAddress2));
   builder.AppendFormat("&city={0}", HttpUtility.UrlEncode(order.BillingCity));
            StateProvince billingStateProvince = StateProvinceManager.GetStateProvinceById(order.BillingStateProvinceId);
   if (billingStateProvince != null)
      builder.AppendFormat("&state={0}", HttpUtility.UrlEncode(billingStateProvince.Abbreviation));
   else
      builder.AppendFormat("&state={0}", HttpUtility.UrlEncode(order.BillingStateProvince));
   Country billingCountry = CountryManager.GetCountryById(order.BillingCountryId);
   if (billingCountry != null)
      builder.AppendFormat("&country={0}", HttpUtility.UrlEncode(billingCountry.TwoLetterIsoCode));
   else
      builder.AppendFormat("&country={0}", HttpUtility.UrlEncode(order.BillingCountry));
    builder.AppendFormat("&zip={0}", HttpUtility.UrlEncode(order.BillingZipPostalCode));

   //To send phone number - work on this later - not required to pass.
   //Paypal requires email addressess and phone numbers from ALL customers, so it would be good to pre-fill them -
   //would be an issue with countries other than US.
   //var phoneN = order.BillingPhoneNumber; //parse?
   //doesn't look like phone number is formatted in any particular way in nopCommerce... hard to parse
   //builder.AppendFormat("&night_phone_a={0}", "123"); //area code
   //builder.AppendFormat("&night_phone_b={0}", "555"); //prefix
   //builder.AppendFormat("&night_phone_c={0}", "1111"); //last four

   builder.AppendFormat("&email={0}", HttpUtility.UrlEncode(order.BillingEmail));
   HttpContext.Current.Response.Redirect(builder.ToString());
   return string.Empty;
}


Perhaps you can slip this into the 1.80 beta so people can test it? :)
13 years ago
Barry,

Thanks a lot
13 years ago
Andrei,

I downloaded 1.80 and took a look at the Paypal code... nice improvements! I would have tried to build it in as an option, but that is a little more than I know how to do right now.

I noticed that you figured out something that may work for phone numbers but it is commented out... why's that? I'd love to pass the phone number if it is possible and that code looks good to me. Paypal is going to ask the customer for the phone number anyway (if they don't already have a Paypal account), so why not pass it if we have it and save the customer some hassle? Do you want to move it to the Paypal settings along with the address?

By the way, do I get a contributor mention? :)
13 years ago
bfranklin825 wrote:
why's that?

I don't remember exactly why I commented it. But there was some issue

bfranklin825 wrote:
By the way, do I get a contributor mention? :)

Sure, Barry. Just added it. Thanks again
13 years ago
hi i have this characters problem for all of my products in V1.9 do you have any sluotion ?hebrew charchters
12 years ago
Hello ,
m having this issue for the new version 2.1 ,
i tried u r code and it didn't worked , but m sure many things changed after a whole year :)

Thank you
12 years ago
mrcooll020 wrote:
Hello ,
m having this issue for the new version 2.1 ,
i tried u r code and it didn't worked , but m sure many things changed after a whole year :)

Thank you


This code should be built into version 2.1, it was put into 1.90 (maybe 1.80 I don't remember) after I wrote it. It is possible that there is a setting somewhere that allows you to show the items - I just can't tell you where it is right now as I have had little time to mess with 2.0+.
12 years ago
Hi,


There is a issue for integrating with paypal standard. when we confirmed the item it will go to paypal standard account. If it is fails for paying or if you come back from paypal standard account page to confirm page the stock(quantity) will be reduced. It should not reduce when it fails for paying amount. So please help us out we need to integrate paypal standard account.send solution to  sangu.arali@gmail.com
This topic was automatically closed 365 days after the last reply. New replies are no longer allowed.