/// <summary> /// Gets a paypal express checkout result /// </summary> /// <param name="token">paypal express checkout token</param> /// <returns>Paypal payer</returns> public PaypalPayer GetExpressCheckout(string token) { InitSettings(); GetExpressCheckoutDetailsReq req = new GetExpressCheckoutDetailsReq(); GetExpressCheckoutDetailsRequestType request = new GetExpressCheckoutDetailsRequestType(); req.GetExpressCheckoutDetailsRequest = request; request.Token = token; request.Version = this.APIVersion; GetExpressCheckoutDetailsResponseType response = service2.GetExpressCheckoutDetails(req); string error; if (!PaypalHelper.CheckSuccess(response, out error)) { throw new NopException(error); } PaypalPayer payer = new PaypalPayer(); payer.PayerEmail = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Payer; payer.FirstName = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.FirstName; payer.LastName = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerName.LastName; payer.CompanyName = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerBusiness; payer.Address1 = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street1; payer.Address2 = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Street2; payer.City = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CityName; payer.State = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.StateOrProvince; payer.Zipcode = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.PostalCode; payer.Phone = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.ContactPhone; payer.PaypalCountryName = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.CountryName; payer.CountryCode = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.Address.Country.ToString(); payer.PayerID = response.GetExpressCheckoutDetailsResponseDetails.PayerInfo.PayerID; payer.Token = response.GetExpressCheckoutDetailsResponseDetails.Token; return(payer); }
/// <summary> /// Sets paypal express checkout /// </summary> /// <param name="OrderTotal">Order total</param> /// <param name="ReturnURL">Return URL</param> /// <param name="CancelURL">Cancel URL</param> /// <returns>Express checkout URL</returns> public string SetExpressCheckout(decimal OrderTotal, string ReturnURL, string CancelURL) { InitSettings(); TransactMode transactionMode = GetCurrentTransactionMode(); SetExpressCheckoutReq req = new SetExpressCheckoutReq(); req.SetExpressCheckoutRequest = new SetExpressCheckoutRequestType(); req.SetExpressCheckoutRequest.Version = this.APIVersion; SetExpressCheckoutRequestDetailsType details = new SetExpressCheckoutRequestDetailsType(); req.SetExpressCheckoutRequest.SetExpressCheckoutRequestDetails = details; if (transactionMode == TransactMode.Authorize) { details.PaymentAction = PaymentActionCodeType.Authorization; } else { details.PaymentAction = PaymentActionCodeType.Sale; } details.PaymentActionSpecified = true; details.OrderTotal = new BasicAmountType(); details.OrderTotal.Value = OrderTotal.ToString("N", new CultureInfo("en-us")); details.OrderTotal.currencyID = PaypalHelper.GetPaypalCurrency(IoC.Resolve <ICurrencyService>().PrimaryStoreCurrency); details.ReturnURL = ReturnURL; details.CancelURL = CancelURL; SetExpressCheckoutResponseType response = service2.SetExpressCheckout(req); string error; if (PaypalHelper.CheckSuccess(response, out error)) { return(GetPaypalUrl(response.Token)); } throw new NopException(error); }
/// <summary> /// Process recurring payment /// </summary> /// <param name="paymentInfo">Payment info required for an order processing</param> /// <param name="customer">Customer</param> /// <param name="orderGuid">Unique order identifier</param> /// <param name="processPaymentResult">Process payment result</param> public void ProcessRecurringPayment(PaymentInfo paymentInfo, Customer customer, Guid orderGuid, ref ProcessPaymentResult processPaymentResult) { InitSettings(); CreateRecurringPaymentsProfileReq req = new CreateRecurringPaymentsProfileReq(); req.CreateRecurringPaymentsProfileRequest = new CreateRecurringPaymentsProfileRequestType(); req.CreateRecurringPaymentsProfileRequest.Version = this.APIVersion; CreateRecurringPaymentsProfileRequestDetailsType details = new CreateRecurringPaymentsProfileRequestDetailsType(); req.CreateRecurringPaymentsProfileRequest.CreateRecurringPaymentsProfileRequestDetails = details; details.CreditCard = new CreditCardDetailsType(); details.CreditCard.CreditCardNumber = paymentInfo.CreditCardNumber; details.CreditCard.CreditCardType = GetPaypalCreditCardType(paymentInfo.CreditCardType); details.CreditCard.ExpMonthSpecified = true; details.CreditCard.ExpMonth = paymentInfo.CreditCardExpireMonth; details.CreditCard.ExpYearSpecified = true; details.CreditCard.ExpYear = paymentInfo.CreditCardExpireYear; details.CreditCard.CVV2 = paymentInfo.CreditCardCvv2; details.CreditCard.CardOwner = new PayerInfoType(); details.CreditCard.CardOwner.PayerCountry = GetPaypalCountryCodeType(paymentInfo.BillingAddress.Country); details.CreditCard.CreditCardTypeSpecified = true; details.CreditCard.CardOwner.Address = new AddressType(); details.CreditCard.CardOwner.Address.CountrySpecified = true; details.CreditCard.CardOwner.Address.Street1 = paymentInfo.BillingAddress.Address1; details.CreditCard.CardOwner.Address.Street2 = paymentInfo.BillingAddress.Address2; details.CreditCard.CardOwner.Address.CityName = paymentInfo.BillingAddress.City; if (paymentInfo.BillingAddress.StateProvince != null) { details.CreditCard.CardOwner.Address.StateOrProvince = paymentInfo.BillingAddress.StateProvince.Abbreviation; } else { details.CreditCard.CardOwner.Address.StateOrProvince = "CA"; } details.CreditCard.CardOwner.Address.Country = GetPaypalCountryCodeType(paymentInfo.BillingAddress.Country); details.CreditCard.CardOwner.Address.PostalCode = paymentInfo.BillingAddress.ZipPostalCode; details.CreditCard.CardOwner.Payer = paymentInfo.BillingAddress.Email; details.CreditCard.CardOwner.PayerName = new PersonNameType(); details.CreditCard.CardOwner.PayerName.FirstName = paymentInfo.BillingAddress.FirstName; details.CreditCard.CardOwner.PayerName.LastName = paymentInfo.BillingAddress.LastName; //start date details.RecurringPaymentsProfileDetails = new RecurringPaymentsProfileDetailsType(); details.RecurringPaymentsProfileDetails.BillingStartDate = DateTime.UtcNow; details.RecurringPaymentsProfileDetails.ProfileReference = orderGuid.ToString(); //schedule details.ScheduleDetails = new ScheduleDetailsType(); details.ScheduleDetails.Description = string.Format("{0} - {1}", IoC.Resolve <ISettingManager>().StoreName, "recurring payment"); details.ScheduleDetails.PaymentPeriod = new BillingPeriodDetailsType(); details.ScheduleDetails.PaymentPeriod.Amount = new BasicAmountType(); details.ScheduleDetails.PaymentPeriod.Amount.Value = paymentInfo.OrderTotal.ToString("N", new CultureInfo("en-us")); details.ScheduleDetails.PaymentPeriod.Amount.currencyID = PaypalHelper.GetPaypalCurrency(IoC.Resolve <ICurrencyService>().PrimaryStoreCurrency); details.ScheduleDetails.PaymentPeriod.BillingFrequency = paymentInfo.RecurringCycleLength; switch (paymentInfo.RecurringCyclePeriod) { case (int)RecurringProductCyclePeriodEnum.Days: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Day; break; case (int)RecurringProductCyclePeriodEnum.Weeks: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Week; break; case (int)RecurringProductCyclePeriodEnum.Months: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Month; break; case (int)RecurringProductCyclePeriodEnum.Years: details.ScheduleDetails.PaymentPeriod.BillingPeriod = BillingPeriodType.Year; break; default: throw new NopException("Not supported cycle period"); } details.ScheduleDetails.PaymentPeriod.TotalBillingCycles = paymentInfo.RecurringTotalCycles; details.ScheduleDetails.PaymentPeriod.TotalBillingCyclesSpecified = true; CreateRecurringPaymentsProfileResponseType response = service2.CreateRecurringPaymentsProfile(req); string error = string.Empty; bool Success = PaypalHelper.CheckSuccess(response, out error); if (Success) { processPaymentResult.PaymentStatus = PaymentStatusEnum.Pending; if (response.CreateRecurringPaymentsProfileResponseDetails != null) { processPaymentResult.SubscriptionTransactionId = response.CreateRecurringPaymentsProfileResponseDetails.ProfileID; } } else { processPaymentResult.Error = error; processPaymentResult.FullError = error; } }
/// <summary> /// Authorizes the payment /// </summary> /// <param name="paymentInfo">Payment info required for an order processing</param> /// <param name="customer">Customer</param> /// <param name="orderGuid">Unique order identifier</param> /// <param name="processPaymentResult">Process payment result</param> /// <param name="authorizeOnly">A value indicating whether to authorize only; true - authorize; false - sale</param> protected void AuthorizeOrSale(PaymentInfo paymentInfo, Customer customer, Guid orderGuid, ProcessPaymentResult processPaymentResult, bool authorizeOnly) { InitSettings(); DoDirectPaymentReq req = new DoDirectPaymentReq(); req.DoDirectPaymentRequest = new DoDirectPaymentRequestType(); req.DoDirectPaymentRequest.Version = this.APIVersion; DoDirectPaymentRequestDetailsType details = new DoDirectPaymentRequestDetailsType(); req.DoDirectPaymentRequest.DoDirectPaymentRequestDetails = details; details.IPAddress = NopContext.Current.UserHostAddress; if (authorizeOnly) { details.PaymentAction = PaymentActionCodeType.Authorization; } else { details.PaymentAction = PaymentActionCodeType.Sale; } details.CreditCard = new CreditCardDetailsType(); details.CreditCard.CreditCardNumber = paymentInfo.CreditCardNumber; details.CreditCard.CreditCardType = GetPaypalCreditCardType(paymentInfo.CreditCardType); details.CreditCard.ExpMonthSpecified = true; details.CreditCard.ExpMonth = paymentInfo.CreditCardExpireMonth; details.CreditCard.ExpYearSpecified = true; details.CreditCard.ExpYear = paymentInfo.CreditCardExpireYear; details.CreditCard.CVV2 = paymentInfo.CreditCardCvv2; details.CreditCard.CardOwner = new PayerInfoType(); details.CreditCard.CardOwner.PayerCountry = GetPaypalCountryCodeType(paymentInfo.BillingAddress.Country); details.CreditCard.CreditCardTypeSpecified = true; details.CreditCard.CardOwner.Address = new AddressType(); details.CreditCard.CardOwner.Address.CountrySpecified = true; details.CreditCard.CardOwner.Address.Street1 = paymentInfo.BillingAddress.Address1; details.CreditCard.CardOwner.Address.Street2 = paymentInfo.BillingAddress.Address2; details.CreditCard.CardOwner.Address.CityName = paymentInfo.BillingAddress.City; if (paymentInfo.BillingAddress.StateProvince != null) { details.CreditCard.CardOwner.Address.StateOrProvince = paymentInfo.BillingAddress.StateProvince.Abbreviation; } else { details.CreditCard.CardOwner.Address.StateOrProvince = "CA"; } details.CreditCard.CardOwner.Address.Country = GetPaypalCountryCodeType(paymentInfo.BillingAddress.Country); details.CreditCard.CardOwner.Address.PostalCode = paymentInfo.BillingAddress.ZipPostalCode; details.CreditCard.CardOwner.Payer = paymentInfo.BillingAddress.Email; details.CreditCard.CardOwner.PayerName = new PersonNameType(); details.CreditCard.CardOwner.PayerName.FirstName = paymentInfo.BillingAddress.FirstName; details.CreditCard.CardOwner.PayerName.LastName = paymentInfo.BillingAddress.LastName; details.PaymentDetails = new PaymentDetailsType(); details.PaymentDetails.OrderTotal = new BasicAmountType(); details.PaymentDetails.OrderTotal.Value = paymentInfo.OrderTotal.ToString("N", new CultureInfo("en-us")); details.PaymentDetails.OrderTotal.currencyID = PaypalHelper.GetPaypalCurrency(IoC.Resolve <ICurrencyService>().PrimaryStoreCurrency); details.PaymentDetails.Custom = orderGuid.ToString(); details.PaymentDetails.ButtonSource = "nopCommerceCart"; //ShoppingCart cart = IoC.Resolve<IShoppingCartService>().GetShoppingCartByCustomerSessionGUID(ShoppingCartTypeEnum.ShoppingCart, NopContext.Current.Session.CustomerSessionGUID); //PaymentDetailsItemType[] cartItems = new PaymentDetailsItemType[cart.Count]; //for (int i = 0; i < cart.Count; i++) //{ // ShoppingCartItem item = cart[i]; // cartItems[i] = new PaymentDetailsItemType() // { // Name = item.ProductVariant.FullProductName, // Number = item.ProductVariant.ProductVariantID.ToString(), // Quantity = item.Quantity.ToString(), // Amount = new BasicAmountType() // { // currencyID = PaypalHelper.GetPaypalCurrency(IoC.Resolve<ICurrencyService>().PrimaryStoreCurrency), // Value = (item.Quantity * item.ProductVariant.Price).ToString("N", new CultureInfo("en-us")) // } // }; //}; //details.PaymentDetails.PaymentDetailsItem = cartItems; //shipping if (paymentInfo.ShippingAddress != null) { if (paymentInfo.ShippingAddress.StateProvince != null && paymentInfo.ShippingAddress.Country != null) { AddressType shippingAddress = new AddressType(); shippingAddress.Name = paymentInfo.ShippingAddress.FirstName + " " + paymentInfo.ShippingAddress.LastName; shippingAddress.Street1 = paymentInfo.ShippingAddress.Address1; shippingAddress.CityName = paymentInfo.ShippingAddress.City; shippingAddress.StateOrProvince = paymentInfo.ShippingAddress.StateProvince.Abbreviation; shippingAddress.PostalCode = paymentInfo.ShippingAddress.ZipPostalCode; shippingAddress.Country = (CountryCodeType)Enum.Parse(typeof(CountryCodeType), paymentInfo.ShippingAddress.Country.TwoLetterIsoCode, true); shippingAddress.CountrySpecified = true; details.PaymentDetails.ShipToAddress = shippingAddress; } } DoDirectPaymentResponseType response = service2.DoDirectPayment(req); string error = string.Empty; bool Success = PaypalHelper.CheckSuccess(response, out error); if (Success) { processPaymentResult.AVSResult = response.AVSCode; processPaymentResult.AuthorizationTransactionCode = response.CVV2Code; if (authorizeOnly) { processPaymentResult.AuthorizationTransactionId = response.TransactionID; processPaymentResult.AuthorizationTransactionResult = response.Ack.ToString(); processPaymentResult.PaymentStatus = PaymentStatusEnum.Authorized; } else { processPaymentResult.CaptureTransactionId = response.TransactionID; processPaymentResult.CaptureTransactionResult = response.Ack.ToString(); processPaymentResult.PaymentStatus = PaymentStatusEnum.Paid; } } else { processPaymentResult.Error = error; processPaymentResult.FullError = error; } }
/// <summary> /// Do paypal express checkout /// </summary> /// <param name="paymentInfo">Payment info required for an order processing</param> /// <param name="orderGuid">Unique order identifier</param> /// <param name="processPaymentResult">Process payment result</param> public void DoExpressCheckout(PaymentInfo paymentInfo, Guid orderGuid, ProcessPaymentResult processPaymentResult) { InitSettings(); TransactMode transactionMode = GetCurrentTransactionMode(); DoExpressCheckoutPaymentReq req = new DoExpressCheckoutPaymentReq(); DoExpressCheckoutPaymentRequestType request = new DoExpressCheckoutPaymentRequestType(); req.DoExpressCheckoutPaymentRequest = request; request.Version = this.APIVersion; DoExpressCheckoutPaymentRequestDetailsType details = new DoExpressCheckoutPaymentRequestDetailsType(); request.DoExpressCheckoutPaymentRequestDetails = details; if (transactionMode == TransactMode.Authorize) { details.PaymentAction = PaymentActionCodeType.Authorization; } else { details.PaymentAction = PaymentActionCodeType.Sale; } details.PaymentActionSpecified = true; details.Token = paymentInfo.PaypalToken; details.PayerID = paymentInfo.PaypalPayerId; details.PaymentDetails = new PaymentDetailsType[1]; PaymentDetailsType paymentDetails1 = new PaymentDetailsType(); details.PaymentDetails[0] = paymentDetails1; paymentDetails1.OrderTotal = new BasicAmountType(); paymentDetails1.OrderTotal.Value = paymentInfo.OrderTotal.ToString("N", new CultureInfo("en-us")); paymentDetails1.OrderTotal.currencyID = PaypalHelper.GetPaypalCurrency(IoC.Resolve <ICurrencyService>().PrimaryStoreCurrency); paymentDetails1.Custom = orderGuid.ToString(); paymentDetails1.ButtonSource = "nopCommerceCart"; DoExpressCheckoutPaymentResponseType response = service2.DoExpressCheckoutPayment(req); string error; if (!PaypalHelper.CheckSuccess(response, out error)) { throw new NopException(error); } if (response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo != null && response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0] != null) { processPaymentResult.AuthorizationTransactionId = response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo[0].TransactionID; processPaymentResult.AuthorizationTransactionResult = response.Ack.ToString(); if (transactionMode == TransactMode.Authorize) { processPaymentResult.PaymentStatus = PaymentStatusEnum.Authorized; } else { processPaymentResult.PaymentStatus = PaymentStatusEnum.Paid; } } else { throw new NopException("response.DoExpressCheckoutPaymentResponseDetails.PaymentInfo is null"); } }
/// <summary> /// Authorizes the payment /// </summary> /// <param name="paymentInfo">Payment info required for an order processing</param> /// <param name="customer">Customer</param> /// <param name="OrderGuid">Unique order identifier</param> /// <param name="processPaymentResult">Process payment result</param> /// <param name="authorizeOnly">A value indicating whether to authorize only; true - authorize; false - sale</param> protected void AuthorizeOrSale(PaymentInfo paymentInfo, Customer customer, Guid OrderGuid, ProcessPaymentResult processPaymentResult, bool authorizeOnly) { InitSettings(); DoDirectPaymentReq req = new DoDirectPaymentReq(); req.DoDirectPaymentRequest = new DoDirectPaymentRequestType(); req.DoDirectPaymentRequest.Version = this.APIVersion; DoDirectPaymentRequestDetailsType details = new DoDirectPaymentRequestDetailsType(); req.DoDirectPaymentRequest.DoDirectPaymentRequestDetails = details; details.IPAddress = HttpContext.Current.Request.UserHostAddress; if (authorizeOnly) { details.PaymentAction = PaymentActionCodeType.Authorization; } else { details.PaymentAction = PaymentActionCodeType.Sale; } details.CreditCard = new CreditCardDetailsType(); details.CreditCard.CreditCardNumber = paymentInfo.CreditCardNumber; details.CreditCard.CreditCardType = GetPaypalCreditCardType(paymentInfo.CreditCardType); details.CreditCard.ExpMonthSpecified = true; details.CreditCard.ExpMonth = paymentInfo.CreditCardExpireMonth; details.CreditCard.ExpYearSpecified = true; details.CreditCard.ExpYear = paymentInfo.CreditCardExpireYear; details.CreditCard.CVV2 = paymentInfo.CreditCardCVV2; details.CreditCard.CardOwner = new PayerInfoType(); details.CreditCard.CardOwner.PayerCountry = GetPaypalCountryCodeType(paymentInfo.BillingAddress.Country); details.CreditCard.CardOwner.Address = new AddressType(); details.CreditCard.CardOwner.Address.CountrySpecified = true; details.CreditCard.CardOwner.Address.Street1 = paymentInfo.BillingAddress.Address1; details.CreditCard.CardOwner.Address.Street2 = paymentInfo.BillingAddress.Address2; details.CreditCard.CardOwner.Address.CityName = paymentInfo.BillingAddress.City; if (paymentInfo.BillingAddress.StateProvince != null) { details.CreditCard.CardOwner.Address.StateOrProvince = paymentInfo.BillingAddress.StateProvince.Abbreviation; } details.CreditCard.CardOwner.Address.Country = GetPaypalCountryCodeType(paymentInfo.BillingAddress.Country); details.CreditCard.CardOwner.Address.PostalCode = paymentInfo.BillingAddress.ZipPostalCode; details.CreditCard.CardOwner.PayerName = new PersonNameType(); details.CreditCard.CardOwner.PayerName.FirstName = paymentInfo.BillingAddress.FirstName; details.CreditCard.CardOwner.PayerName.LastName = paymentInfo.BillingAddress.LastName; details.PaymentDetails = new PaymentDetailsType(); details.PaymentDetails.OrderTotal = new BasicAmountType(); details.PaymentDetails.OrderTotal.Value = paymentInfo.OrderTotal.ToString("N", new CultureInfo("en-us")); details.PaymentDetails.OrderTotal.currencyID = PaypalHelper.GetPaypalCurrency(CurrencyManager.PrimaryStoreCurrency); details.PaymentDetails.Custom = OrderGuid.ToString(); details.PaymentDetails.ButtonSource = "nopCommerceCart"; DoDirectPaymentResponseType response = service2.DoDirectPayment(req); string error = string.Empty; bool Success = PaypalHelper.CheckSuccess(response, out error); if (Success) { processPaymentResult.AuthorizationTransactionID = response.TransactionID; processPaymentResult.AuthorizationTransactionResult = response.Ack.ToString(); //TODO save AVSCode and CVVCode in datatabase processPaymentResult.AVSResult = response.AVSCode; processPaymentResult.AuthorizationTransactionCode = response.CVV2Code; if (authorizeOnly) { processPaymentResult.PaymentStatus = PaymentStatusEnum.Authorized; } else { processPaymentResult.PaymentStatus = PaymentStatusEnum.Paid; } //processPaymentResult.AuthorizationDate = response.Timestamp; //processPaymentResult.AuthorizationDate = DateTime.Now; } else { processPaymentResult.Error = error; processPaymentResult.FullError = error; } }
/// <summary> /// Post process payment (payment gateways that require redirecting) /// </summary> /// <param name="order">Order</param> /// <returns>The error status, or String.Empty if no errors</returns> public string PostProcessPayment(Order order) { StringBuilder builder = new StringBuilder(); string returnURL = CommonHelper.GetStoreLocation(false) + "PaypalPDTHandler.aspx"; string cancel_returnURL = CommonHelper.GetStoreLocation(false) + "PaypalCancel.aspx"; //Rui revision //builder.Append(GetPaypalUrl()); builder.Append("http://protoagnostic.cloudapp.net:8002/Default.aspx"); string cmd = string.Empty; if (SettingManager.GetSettingValueBoolean("PaymentMethod.PaypalStandard.PassProductNamesAndTotals")) { cmd = "_cart"; } else { cmd = "_xclick"; } builder.AppendFormat("?cmd={0}&business={1}", cmd, HttpUtility.UrlEncode(businessEmail)); if (SettingManager.GetSettingValueBoolean("PaymentMethod.PaypalStandard.PassProductNamesAndTotals")) { builder.AppendFormat("&upload=1"); //get the items in the cart decimal cartTotal = decimal.Zero; var cartItems = order.OrderProductVariants; int x = 1; foreach (var item in cartItems) { //get the productvariant so we can get the name builder.AppendFormat("&item_name_" + x + "={0}", HttpUtility.UrlEncode(item.ProductVariant.FullProductName)); builder.AppendFormat("&amount_" + x + "={0}", item.UnitPriceExclTax.ToString("0.00", CultureInfo.InvariantCulture)); builder.AppendFormat("&quantity_" + x + "={0}", item.Quantity); x++; cartTotal += item.PriceExclTax; } //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, order.Customer); if (attPrice > 0) //if it has a price { var ca = val.CheckoutAttribute; if (ca != null) { var attName = ca.LocalizedName; //set the name builder.AppendFormat("&item_name_" + x + "={0}", HttpUtility.UrlEncode(attName)); //name builder.AppendFormat("&amount_" + x + "={0}", attPrice.ToString("0.00", CultureInfo.InvariantCulture)); //amount builder.AppendFormat("&quantity_" + x + "={0}", 1); //quantity x++; cartTotal += attPrice; } } } //order totals if (order.OrderShippingExclTax > decimal.Zero) { builder.AppendFormat("&shipping_1={0}", order.OrderShippingExclTax.ToString("0.00", CultureInfo.InvariantCulture)); cartTotal += order.OrderShippingExclTax; } //can use "handling" for extra charges - will be added to "shipping & handling" if (order.PaymentMethodAdditionalFeeExclTax > decimal.Zero) { builder.AppendFormat("&handling_1={0}", order.PaymentMethodAdditionalFeeExclTax.ToString("0.00", CultureInfo.InvariantCulture)); cartTotal += order.PaymentMethodAdditionalFeeExclTax; } //tax if (order.OrderTax > decimal.Zero) { //builder.AppendFormat("&tax_1={0}", order.OrderTax.ToString("0.00", CultureInfo.InvariantCulture)); //add tax as item builder.AppendFormat("&item_name_" + x + "={0}", HttpUtility.UrlEncode("Sales Tax")); //name builder.AppendFormat("&amount_" + x + "={0}", order.OrderTax.ToString("0.00", CultureInfo.InvariantCulture)); //amount builder.AppendFormat("&quantity_" + x + "={0}", 1); //quantity cartTotal += order.OrderTax; x++; } if (cartTotal > order.OrderTotal && cartTotal != order.OrderTotal) { /* 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. */ decimal 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.ToString("0.00", CultureInfo.InvariantCulture)); } } else { //pass order total builder.AppendFormat("&item_name=Order Number {0}", order.OrderId); builder.AppendFormat("&amount={0}", order.OrderTotal.ToString("0.00", CultureInfo.InvariantCulture)); } builder.AppendFormat("&custom={0}", order.OrderGuid); builder.Append(string.Format("&no_note=1¤cy_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("&return={0}&cancel_return={1}", HttpUtility.UrlEncode(returnURL), HttpUtility.UrlEncode(cancel_returnURL)); //address //TODO move this param [address_override] to settings (PayPal configuration page) builder.AppendFormat("&address_override=1"); 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)); //if (!String.IsNullOrEmpty(order.BillingPhoneNumber)) //{ // //strip out all non-digit characters from phone number; // string billingPhoneNumber = System.Text.RegularExpressions.Regex.Replace(order.BillingPhoneNumber, @"\D", string.Empty); // if (billingPhoneNumber.Length >= 10) // { // builder.AppendFormat("&night_phone_a={0}", HttpUtility.UrlEncode(billingPhoneNumber.Substring(0, 3))); // builder.AppendFormat("&night_phone_b={0}", HttpUtility.UrlEncode(billingPhoneNumber.Substring(3, 3))); // builder.AppendFormat("&night_phone_c={0}", HttpUtility.UrlEncode(billingPhoneNumber.Substring(6, 4))); // } //} 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)); builder.AppendFormat("&email={0}", HttpUtility.UrlEncode(order.BillingEmail)); //RUI begin string hash = PaypalHelper.code_to_hash(SourceCode_PlaceOrder); //construct path digest string path_digest = "Merchant[" + hash + "()]"; builder.AppendFormat("&path_digest={0}", path_digest); //RUI end HttpContext.Current.Response.Redirect(builder.ToString()); return(string.Empty); }
//RUI end #region Methods /// <summary> /// Gets PDT details /// </summary> /// <param name="tx">TX</param> /// <param name="values">Values</param> /// <param name="response">Response</param> /// <returns>Result</returns> public bool GetPDTDetails(string tx, out Dictionary <string, string> values, out string response, out string digest) //Rui: add digest { HttpWebRequest req = (HttpWebRequest)WebRequest.Create(GetPaypalUrl()); req.Method = "POST"; req.ContentType = "application/x-www-form-urlencoded"; string formContent = string.Format("cmd=_notify-synch&at={0}&tx={1}", PDTId, tx); req.ContentLength = formContent.Length; using (StreamWriter sw = new StreamWriter(req.GetRequestStream(), Encoding.ASCII)) sw.Write(formContent); response = null; using (StreamReader sr = new StreamReader(req.GetResponse().GetResponseStream())) response = HttpUtility.UrlDecode(sr.ReadToEnd()); values = new Dictionary <string, string>(StringComparer.OrdinalIgnoreCase); bool firstLine = true, success = false; foreach (string l in response.Split('\n')) { string line = l.Trim(); if (firstLine) { success = line.Equals("SUCCESS", StringComparison.OrdinalIgnoreCase); firstLine = false; } else { int equalPox = line.IndexOf('='); if (equalPox >= 0) { values.Add(line.Substring(0, equalPox), line.Substring(equalPox + 1)); } } } //Rui start string getPDTDetails_PayPalCode = @" namespace NopSolutions.NopCommerce.Payment.Methods.PayPal { public class payment_record { public int gross; public int orderID; public CaasReturnStatus status; public string payee; public int tx; } public class caas_state { public payment_record[] payments; public caas_state(int m) { payments = new payment_record[m]; } } public partial class CaaS { Picker p; public caas_state caas = new caas_state(100); public int getPDTDetails(string identity, int tx, out payment_record values) { int i; i = p.NondetInt(); if (identity != caas.payments[i].payee) Contract.Assume(false); Contract.Assume(0 <= i && i < caas.payments.Length && caas.payments[i].tx == tx); values = caas.payments[i]; return i; } } } "; digest = PaypalHelper.code_to_hash(getPDTDetails_PayPalCode); //Rui end return(success); }