jmawebtech

paypal recurring

Oct 29th, 2013
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 34.22 KB | None | 0 0
  1. using System;
  2. using System.Web.Routing;
  3. using Nop.Core;
  4. using Nop.Core.Domain;
  5. using Nop.Core.Domain.Catalog;
  6. using Nop.Core.Domain.Directory;
  7. using Nop.Core.Domain.Orders;
  8. using Nop.Core.Domain.Payments;
  9. using Nop.Core.Plugins;
  10. using Nop.Plugin.Payments.PayFlowPro.Controllers;
  11. using Nop.Services.Configuration;
  12. using Nop.Services.Customers;
  13. using Nop.Services.Directory;
  14. using Nop.Services.Payments;
  15. using PayPal.Payments.Common.Utility;
  16. using PayPal.Payments.DataObjects;
  17. using PayPal.Payments.Transactions;
  18.  
  19. namespace Nop.Plugin.Payments.PayFlowPro
  20. {
  21.     /// <summary>
  22.     /// PayPalDirect payment processor
  23.     /// </summary>
  24.     public class PayFlowProPaymentProcessor : BasePlugin, IPaymentMethod
  25.     {
  26.  
  27.         #region PayFlowPro 101
  28.  
  29.         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  30.         // PayFlowPro 101
  31.         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  32.         // A sale transaction requires the following objects to be set:
  33.         //      a) UserInfo Object: Info about the merchant making the transaction.
  34.         //      b) PFProConnectionData Object: The URL of the PayPal transaction server and the location of the local certificate.
  35.         //      c) Currency Object: The amount of money and currency type for this transaction.
  36.         //      d) BillTo Object: Customer's billing information.
  37.         //      e) CreditCard Object: Includes name of cardholder, cardnumber, expiration date, and card security code
  38.         //
  39.         //
  40.         // Of course, to make things confusing, some of these objects are used as parameters to other objects in an unintuitive way.
  41.         //
  42.         // To help you understand the relationships between thess objects, here's a basic diagram using the class names. (Not instance names.)
  43.         //
  44.         // SaleTransaction ---(Has A)---
  45.         //                              |---UserInfo
  46.         //                              |
  47.         //                              |---PFProConnectionData
  48.         //                              |
  49.         //                              |---Invoice---(Has A)-------
  50.         //                                                          |---Currency
  51.         //                                                          |
  52.         //                                                          |---BillTo
  53.         //                                                          |
  54.         //                                                          |---ShipTo*
  55.         //                              |---CardTender---(Has A)----
  56.         //                                                          |---CreditCard
  57.         //
  58.         // *ShipTo, which is a member of Invoice contains the shipping address. It is not required, but I have placed it in diagram
  59.         // and the code below.
  60.         //
  61.         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  62.  
  63.         #endregion
  64.  
  65.  
  66.         #region Fields
  67.  
  68.         private readonly PayFlowProPaymentSettings _payFlowProPaymentSettings;
  69.         private readonly ISettingService _settingService;
  70.         private readonly ICurrencyService _currencyService;
  71.         private readonly CurrencySettings _currencySettings;
  72.         private readonly IWebHelper _webHelper;
  73.         private readonly StoreInformationSettings _storeInformationSettings;
  74.         private readonly ICustomerService _customerService;
  75.  
  76.         private const int PFP_SUCCESS = 0;
  77.         private const string US_DOLLARS = "USD";
  78.         private const string TRANSACTION_TYPE_SALE = "S";
  79.         private const string TRANSACTION_TYPE_DELAYED_CAPTURE = "D";
  80.  
  81.         #endregion
  82.  
  83.         #region Ctor
  84.  
  85.         public PayFlowProPaymentProcessor(PayFlowProPaymentSettings payFlowProPaymentSettings,
  86.             ISettingService settingService, ICurrencyService currencyService,
  87.             CurrencySettings currencySettings, IWebHelper webHelper,
  88.             StoreInformationSettings storeInformationSettings, ICustomerService customerService)
  89.         {
  90.             _payFlowProPaymentSettings = payFlowProPaymentSettings;
  91.             _settingService = settingService;
  92.             _currencyService = currencyService;
  93.             _currencySettings = currencySettings;
  94.             _webHelper = webHelper;
  95.             _storeInformationSettings = storeInformationSettings;
  96.             _customerService = customerService;
  97.         }
  98.  
  99.         #endregion
  100.  
  101.         #region Utilities
  102.  
  103.         /// <summary>
  104.         /// Gets PayFlowPro URL
  105.         /// </summary>
  106.         /// <returns></returns>
  107.         private string GetPayFlowProUrl()
  108.         {
  109.             return _payFlowProPaymentSettings.UseSandbox ? "pilot-payflowpro.paypal.com" : "payflowpro.paypal.com";
  110.         }
  111.  
  112.         protected string GetApiVersion()
  113.         {
  114.             return "4.3.1.0";
  115.         }
  116.  
  117.         #endregion
  118.  
  119.         #region Methods
  120.  
  121.         /// <summary>
  122.         /// Process a payment
  123.         /// </summary>
  124.         /// <param name="processPaymentRequest">Payment info required for an order processing</param>
  125.         /// <returns>Process payment result</returns>
  126.         public ProcessPaymentResult ProcessPayment(ProcessPaymentRequest processPaymentRequest)
  127.         {
  128.             /////////////////////////////////////////////////
  129.             //  UserInfo Section
  130.             /////////////////////////////////////////////////
  131.  
  132.             _payFlowProPaymentSettings.Unlock();
  133.             UserInfo userInfo = new UserInfo(_payFlowProPaymentSettings.User, _payFlowProPaymentSettings.Vendor,
  134.                 _payFlowProPaymentSettings.Partner, _payFlowProPaymentSettings.Password);
  135.             _payFlowProPaymentSettings.Lock();
  136.  
  137.             /////////////////////////////////////////////////
  138.             //  PFProConnectionData Section
  139.             /////////////////////////////////////////////////
  140.  
  141.             PayflowConnectionData connectionData = new PayflowConnectionData(GetPayFlowProUrl());
  142.  
  143.             /////////////////////////////////////////////////
  144.             //  Invoice Section
  145.             /////////////////////////////////////////////////
  146.  
  147.             // Refer to diagram above - to construct an Invoice object, we need to
  148.             // create a Currency and a BillTo object to pass to the Invoice ctor.
  149.  
  150.             PayPal.Payments.DataObjects.Currency currency = new PayPal.Payments.DataObjects.Currency(processPaymentRequest.OrderTotal, PayFlowProPaymentProcessor.US_DOLLARS);
  151.             currency.NoOfDecimalDigits = 2;
  152.             currency.Round = true;
  153.  
  154.             // Set the billing address information
  155.             BillTo billTo = new BillTo();
  156.             var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);
  157.             billTo.Street = customer.BillingAddress.Address1;
  158.             billTo.Zip = customer.BillingAddress.ZipPostalCode;
  159.  
  160.             // Create a new Invoice data object and set the amount of sale
  161.             // and billing address information
  162.             Invoice invoice = new Invoice();
  163.             invoice.Amt = currency;
  164.             invoice.BillTo = billTo;
  165.  
  166.             if (customer.ShippingAddress != null)
  167.             {
  168.                 ShipTo shipTo = new ShipTo();
  169.                 Core.Domain.Common.Address shipAddr = customer.ShippingAddress;
  170.                 shipTo.ShipToFirstName = shipAddr.FirstName;
  171.                 shipTo.ShipToLastName = shipAddr.LastName;
  172.                 shipTo.ShipToStreet = shipAddr.Address1;
  173.                 shipTo.ShipToStreet2 = shipAddr.Address2;
  174.                 shipTo.ShipToCity = shipAddr.City;
  175.                 shipTo.ShipToState = shipAddr.StateProvince.Abbreviation;
  176.                 shipTo.ShipToZip = shipAddr.ZipPostalCode;
  177.                 invoice.ShipTo = shipTo;
  178.             }
  179.  
  180.             /////////////////////////////////////////////////
  181.             //  CardTender Section
  182.             /////////////////////////////////////////////////
  183.  
  184.             // Refer to diagram above - to construct an CardTender object, we need
  185.             // to create a CreditCard object to pass to the CardTender ctor.
  186.             // Note: Expiration date must be in MMYY format.
  187.             string expDate = string.Format("{0:00}{1}", processPaymentRequest.CreditCardExpireMonth, processPaymentRequest.CreditCardExpireYear.ToString().Substring(2));
  188.             CreditCard creditCard = new CreditCard(processPaymentRequest.CreditCardNumber, expDate);
  189.             creditCard.Name = processPaymentRequest.CreditCardName;
  190.             creditCard.Cvv2 = processPaymentRequest.CreditCardCvv2;
  191.  
  192.             CardTender cardTender = new CardTender(creditCard);
  193.  
  194.             /////////////////////////////////////////////////
  195.             //  SaleTransaction Section
  196.             /////////////////////////////////////////////////
  197.  
  198.             SaleTransaction saleXAction = new SaleTransaction(userInfo, connectionData, invoice, cardTender, PayflowUtility.RequestId);
  199.             saleXAction.Verbosity = "LOW";
  200.             //saleXAction.TrxType = "
  201.  
  202.             /////////////////////////////////////////////////
  203.             //  Let's do what we came here to do - Submit It!
  204.             /////////////////////////////////////////////////
  205.  
  206.             Response submitResponse = saleXAction.SubmitTransaction();
  207.             if (submitResponse == null)
  208.             {
  209.                 // If the Response instance is null, we have some strange problem that will prevent us from
  210.                 // continuing. We will have to abort processing.
  211.                 throw new Exception("Unknown error while processing sale transaction. Transaction aborted.");
  212.             }
  213.  
  214.             TransactionResponse transactionResponse = submitResponse.TransactionResponse;
  215.             if (transactionResponse == null)
  216.             {
  217.                 // If the TransactionResponse instance is null, we will have to abort processing.
  218.                 throw new Exception("Unknown error while processing sale transaction. Transaction aborted.");
  219.             }
  220.  
  221.             ProcessPaymentResult result = new ProcessPaymentResult();
  222.             if (transactionResponse.Result == PayFlowProPaymentProcessor.PFP_SUCCESS)
  223.             {
  224.                 result.CaptureTransactionId = transactionResponse.Pnref;
  225.                 result.CaptureTransactionResult = transactionResponse.RespMsg;
  226.                 result.NewPaymentStatus = PaymentStatus.Paid;
  227.                
  228.             }
  229.             else
  230.             {
  231.                 result.CaptureTransactionResult = transactionResponse.RespMsg;
  232.                 result.AddError(transactionResponse.RespMsg);
  233.             }
  234.  
  235.             return result;
  236.         }
  237.  
  238.         /// <summary>
  239.         /// Post process payment (used by payment gateways that require redirecting to a third-party URL)
  240.         /// </summary>
  241.         /// <param name="postProcessPaymentRequest">Payment info required for an order processing</param>
  242.         public void PostProcessPayment(PostProcessPaymentRequest postProcessPaymentRequest)
  243.         {
  244.             // Not required for PayFlowPro
  245.         }
  246.  
  247.         /// <summary>
  248.         /// Gets additional handling fee
  249.         /// </summary>
  250.         /// <returns>Additional handling fee</returns>
  251.         public decimal GetAdditionalHandlingFee()
  252.         {
  253.             return 0.00m;
  254.         }
  255.  
  256.         /// <summary>
  257.         /// Captures payment
  258.         /// </summary>
  259.         /// <param name="capturePaymentRequest">Capture payment request</param>
  260.         /// <returns>Capture payment result</returns>
  261.         public CapturePaymentResult Capture(CapturePaymentRequest capturePaymentRequest)
  262.         {
  263.             var result = new CapturePaymentResult();
  264.             result.AddError("Capture method not supported");
  265.             return result;
  266.         }
  267.  
  268.         /// <summary>
  269.         /// Refunds a payment
  270.         /// </summary>
  271.         /// <param name="refundPaymentRequest">Request</param>
  272.         /// <returns>Result</returns>
  273.         public RefundPaymentResult Refund(RefundPaymentRequest refundPaymentRequest)
  274.         {
  275.             var result = new RefundPaymentResult();
  276.            
  277.            
  278.              /////////////////////////////////////////////////
  279.             //  UserInfo Section
  280.             /////////////////////////////////////////////////
  281.  
  282.             _payFlowProPaymentSettings.Unlock();
  283.             UserInfo userInfo = new UserInfo(_payFlowProPaymentSettings.User, _payFlowProPaymentSettings.Vendor,
  284.                 _payFlowProPaymentSettings.Partner, _payFlowProPaymentSettings.Password);
  285.             _payFlowProPaymentSettings.Lock();
  286.  
  287.             /////////////////////////////////////////////////
  288.             //  PFProConnectionData Section
  289.             /////////////////////////////////////////////////
  290.  
  291.             PayflowConnectionData connectionData = new PayflowConnectionData(GetPayFlowProUrl());
  292.  
  293.             /////////////////////////////////////////////////
  294.             //  Invoice Section
  295.             /////////////////////////////////////////////////
  296.  
  297.             // Refer to diagram above - to construct an Invoice object, we need to
  298.             // create a Currency and a BillTo object to pass to the Invoice ctor.
  299.  
  300.             PayPal.Payments.DataObjects.Currency currency = new PayPal.Payments.DataObjects.Currency(refundPaymentRequest.AmountToRefund, PayFlowProPaymentProcessor.US_DOLLARS);
  301.             currency.NoOfDecimalDigits = 2;
  302.             currency.Round = true;
  303.  
  304.             // Set the billing address information
  305.             BillTo billTo = new BillTo();
  306.             var customer = refundPaymentRequest.Order.Customer;
  307.             billTo.Street = customer.BillingAddress.Address1;
  308.             billTo.Zip = customer.BillingAddress.ZipPostalCode;
  309.  
  310.             // Create a new Invoice data object and set the amount of sale
  311.             // and billing address information
  312.             Invoice invoice = new Invoice();
  313.             invoice.Amt = currency;
  314.             invoice.BillTo = billTo;
  315.  
  316.             if (customer.ShippingAddress != null)
  317.             {
  318.                 ShipTo shipTo = new ShipTo();
  319.                 Core.Domain.Common.Address shipAddr = customer.ShippingAddress;
  320.                 shipTo.ShipToFirstName = shipAddr.FirstName;
  321.                 shipTo.ShipToLastName = shipAddr.LastName;
  322.                 shipTo.ShipToStreet = shipAddr.Address1;
  323.                 shipTo.ShipToStreet2 = shipAddr.Address2;
  324.                 shipTo.ShipToCity = shipAddr.City;
  325.                 shipTo.ShipToState = shipAddr.StateProvince.Abbreviation;
  326.                 shipTo.ShipToZip = shipAddr.ZipPostalCode;
  327.                 invoice.ShipTo = shipTo;
  328.             }
  329.  
  330.            
  331.             ///////////////////////////////////////////////////////////////////
  332.  
  333.             // Create a new Credit Transaction.
  334.             // Following is an example of a independent credit type of transaction.
  335.             CreditTransaction Trans = new CreditTransaction(refundPaymentRequest.Order.CaptureTransactionId, userInfo, PayflowUtility.RequestId);
  336.  
  337.             // Submit the Transaction
  338.             Response Resp = Trans.SubmitTransaction();
  339.  
  340.             // Display the transaction response parameters.
  341.             if (Resp != null)
  342.             {
  343.                 // Get the Transaction Response parameters.
  344.                 TransactionResponse TrxnResponse =  Resp.TransactionResponse;
  345.  
  346.                 if (TrxnResponse != null)
  347.                 {
  348.                     Console.WriteLine("RESULT = " + TrxnResponse.Result);
  349.                     Console.WriteLine("PNREF = " + TrxnResponse.Pnref);
  350.                     Console.WriteLine("RESPMSG = " + TrxnResponse.RespMsg);
  351.                     Console.WriteLine("AUTHCODE = " + TrxnResponse.AuthCode);
  352.                     Console.WriteLine("AVSADDR = " + TrxnResponse.AVSAddr);
  353.                     Console.WriteLine("AVSZIP = " + TrxnResponse.AVSZip);
  354.                     Console.WriteLine("IAVS = " + TrxnResponse.IAVS);
  355.                     Console.WriteLine("CVV2MATCH = " + TrxnResponse.CVV2Match);
  356.                     // If value is true, then the Request ID has not been changed and the original response
  357.                     // of the original transction is returned.
  358.                     Console.WriteLine("DUPLICATE = " + TrxnResponse.Duplicate);
  359.                 }
  360.  
  361.                 // Get the Fraud Response parameters.
  362.                 FraudResponse FraudResp =  Resp.FraudResponse;
  363.                 // Display Fraud Response parameter
  364.                 if (FraudResp != null)
  365.                 {
  366.                     Console.WriteLine("PREFPSMSG = " + FraudResp.PreFpsMsg);
  367.                     Console.WriteLine("POSTFPSMSG = " + FraudResp.PostFpsMsg);
  368.                 }
  369.  
  370.                 // Display the response.
  371.                 Console.WriteLine(Environment.NewLine + PayflowUtility.GetStatus(Resp));   
  372.            
  373.                 // Get the Transaction Context and check for any contained SDK specific errors (optional code).
  374.                 PayPal.Payments.Common.Context TransCtx = Resp.TransactionContext;
  375.                 if (TransCtx != null && TransCtx.getErrorCount() > 0)
  376.                 {
  377.                     Console.WriteLine(Environment.NewLine + "Transaction Errors = " + TransCtx.ToString());
  378.                 }
  379.             }
  380.  
  381.             return result;
  382.         }
  383.  
  384.         /// <summary>
  385.         /// Voids a payment
  386.         /// </summary>
  387.         /// <param name="voidPaymentRequest">Request</param>
  388.         /// <returns>Result</returns>
  389.         public VoidPaymentResult Void(VoidPaymentRequest voidPaymentRequest)
  390.         {
  391.             var result = new VoidPaymentResult();
  392.  
  393.             _payFlowProPaymentSettings.Unlock();
  394.             UserInfo userInfo = new UserInfo(_payFlowProPaymentSettings.User, _payFlowProPaymentSettings.Vendor,
  395.                 _payFlowProPaymentSettings.Partner, _payFlowProPaymentSettings.Password);
  396.             _payFlowProPaymentSettings.Lock();
  397.  
  398.             /////////////////////////////////////////////////
  399.             //  PFProConnectionData Section
  400.             /////////////////////////////////////////////////
  401.  
  402.             PayflowConnectionData connectionData = new PayflowConnectionData(GetPayFlowProUrl());
  403.  
  404.  
  405.             ///////////////////////////////////////////////////////////////////
  406.  
  407.             // Create a new Void Transaction.
  408.             // The ORIGID is the PNREF no. for a previous transaction.
  409.             VoidTransaction Trans = new VoidTransaction(voidPaymentRequest.Order.CaptureTransactionId,
  410.                 userInfo, connectionData, PayflowUtility.RequestId);
  411.  
  412.             // Submit the Transaction
  413.             Response Resp = Trans.SubmitTransaction();
  414.  
  415.             if (Resp == null)
  416.             {
  417.                 // If the Response instance is null, we have some strange problem that will prevent us from
  418.                 // continuing. We will have to abort processing.
  419.                 throw new Exception("Unknown error while processing void transaction. Transaction aborted.");
  420.             }
  421.  
  422.             TransactionResponse transactionResponse = Resp.TransactionResponse;
  423.             if (transactionResponse == null)
  424.             {
  425.                 // If the TransactionResponse instance is null, we will have to abort processing.
  426.                 throw new Exception("Unknown error while processing void transaction. Transaction aborted.");
  427.             }
  428.  
  429.             if (transactionResponse.Result == PayFlowProPaymentProcessor.PFP_SUCCESS)
  430.             {
  431.                 result.NewPaymentStatus = Core.Domain.Payments.PaymentStatus.Voided;
  432.             }
  433.             else
  434.             {
  435.                 result.AddError(transactionResponse.RespMsg);
  436.             }
  437.  
  438.             return result;
  439.         }
  440.  
  441.         /// <summary>
  442.         /// Process recurring payment
  443.         /// </summary>
  444.         /// <param name="processPaymentRequest">Payment info required for an order processing</param>
  445.         /// <returns>Process payment result</returns>
  446.         public ProcessPaymentResult ProcessRecurringPayment(ProcessPaymentRequest processPaymentRequest)
  447.         {
  448.             var result = new ProcessPaymentResult();
  449.             /////////////////////////////////////////////////
  450.             //  UserInfo Section
  451.             /////////////////////////////////////////////////
  452.  
  453.             _payFlowProPaymentSettings.Unlock();
  454.             UserInfo userInfo = new UserInfo(_payFlowProPaymentSettings.User, _payFlowProPaymentSettings.Vendor,
  455.                 _payFlowProPaymentSettings.Partner, _payFlowProPaymentSettings.Password);
  456.             _payFlowProPaymentSettings.Lock();
  457.  
  458.             /////////////////////////////////////////////////
  459.             //  PFProConnectionData Section
  460.             /////////////////////////////////////////////////
  461.  
  462.             PayflowConnectionData connectionData = new PayflowConnectionData(GetPayFlowProUrl());
  463.  
  464.  
  465.             PayPal.Payments.DataObjects.Currency currency = new PayPal.Payments.DataObjects.Currency(processPaymentRequest.OrderTotal, PayFlowProPaymentProcessor.US_DOLLARS);
  466.             currency.NoOfDecimalDigits = 2;
  467.             currency.Round = true;
  468.  
  469.             // Set the billing address information
  470.             BillTo billTo = new BillTo();
  471.             var customer = _customerService.GetCustomerById(processPaymentRequest.CustomerId);
  472.             billTo.Street = customer.BillingAddress.Address1;
  473.             billTo.Zip = customer.BillingAddress.ZipPostalCode;
  474.  
  475.             // Create a new Invoice data object and set the amount of sale
  476.             // and billing address information
  477.             Invoice invoice = new Invoice();
  478.             invoice.Amt = currency;
  479.             invoice.BillTo = billTo;
  480.  
  481.             if (customer.ShippingAddress != null)
  482.             {
  483.                 ShipTo shipTo = new ShipTo();
  484.                 Core.Domain.Common.Address shipAddr = customer.ShippingAddress;
  485.                 shipTo.ShipToFirstName = shipAddr.FirstName;
  486.                 shipTo.ShipToLastName = shipAddr.LastName;
  487.                 shipTo.ShipToStreet = shipAddr.Address1;
  488.                 shipTo.ShipToStreet2 = shipAddr.Address2;
  489.                 shipTo.ShipToCity = shipAddr.City;
  490.                 shipTo.ShipToState = shipAddr.StateProvince.Abbreviation;
  491.                 shipTo.ShipToZip = shipAddr.ZipPostalCode;
  492.                 invoice.ShipTo = shipTo;
  493.             }
  494.  
  495.  
  496.  
  497.             /////////////////////////////////////////////////
  498.             //  CardTender Section
  499.             /////////////////////////////////////////////////
  500.  
  501.             // Refer to diagram above - to construct an CardTender object, we need
  502.             // to create a CreditCard object to pass to the CardTender ctor.
  503.             // Note: Expiration date must be in MMYY format.
  504.             string expDate = string.Format("{0:00}{1}", processPaymentRequest.CreditCardExpireMonth, processPaymentRequest.CreditCardExpireYear.ToString().Substring(2));
  505.             CreditCard creditCard = new CreditCard(processPaymentRequest.CreditCardNumber, expDate);
  506.             creditCard.Name = processPaymentRequest.CreditCardName;
  507.             creditCard.Cvv2 = processPaymentRequest.CreditCardCvv2;
  508.  
  509.             CardTender cardTender = new CardTender(creditCard);
  510.  
  511.             RecurringInfo RecurInfo = new RecurringInfo();
  512.             // The date that the first payment will be processed.
  513.             // This will be of the format mmddyyyy.
  514.             RecurInfo.Start = DateTime.UtcNow.AddDays(1).ToString("MMddyyyy");
  515.             RecurInfo.ProfileName = "PayPal";
  516.             // Specifies how often the payment occurs. All PAYPERIOD values must use
  517.             // capital letters and can be any of WEEK / BIWK / SMMO / FRWK / MONT /
  518.             // QTER / SMYR / YEAR
  519.  
  520.             switch (processPaymentRequest.RecurringCyclePeriod)
  521.             {
  522.                 case RecurringProductCyclePeriod.Days:
  523.                     RecurInfo.PayPeriod = "DAY";
  524.                     break;
  525.                 case RecurringProductCyclePeriod.Weeks:
  526.                     RecurInfo.PayPeriod = "WEEK";
  527.                     break;
  528.                 case RecurringProductCyclePeriod.Months:
  529.                     RecurInfo.PayPeriod = "MONTH";
  530.                     break;
  531.                 case RecurringProductCyclePeriod.Years:
  532.                     RecurInfo.PayPeriod = "YEAR";
  533.                     break;
  534.                 default:
  535.                     throw new NopException("Not supported cycle period");
  536.             }
  537.  
  538.  
  539.             //RecurInfo.PayPeriod = "WEEK";
  540.             ///////////////////////////////////////////////////////////////////
  541.  
  542.             // Create a new Recurring Transaction.
  543.             RecurringTransaction Trans = new RecurringTransaction("A", RecurInfo,
  544.                 userInfo, connectionData, invoice, cardTender, PayflowUtility.RequestId);
  545.  
  546.             // Submit the Transaction
  547.             Response Resp = Trans.SubmitTransaction();
  548.  
  549.             //if (Resp != null)
  550.             //{
  551.             //    // Get the Transaction Response parameters.
  552.             //    TransactionResponse TrxnResponse = Resp.TransactionResponse;
  553.  
  554.             //    if (TrxnResponse != null)
  555.             //    {
  556.             //        Console.WriteLine("RESULT = " + TrxnResponse.Result);
  557.             //        Console.WriteLine("RESPMSG = " + TrxnResponse.RespMsg);
  558.             //    }
  559.  
  560.             //    // Get the Recurring Response parameters.
  561.             //    RecurringResponse RecurResponse = Resp.RecurringResponse;
  562.             //    if (RecurResponse != null)
  563.             //    {
  564.             //        Console.WriteLine("RPREF = " + RecurResponse.RPRef);
  565.             //        Console.WriteLine("PROFILEID = " + RecurResponse.ProfileId);
  566.             //    }
  567.             //}
  568.  
  569.             if (Resp == null)
  570.             {
  571.                 // If the Response instance is null, we have some strange problem that will prevent us from
  572.                 // continuing. We will have to abort processing.
  573.                 throw new Exception("Unknown error while processing sale transaction. Transaction aborted.");
  574.             }
  575.  
  576.             TransactionResponse transactionResponse = Resp.TransactionResponse;
  577.             if (transactionResponse == null)
  578.             {
  579.                 // If the TransactionResponse instance is null, we will have to abort processing.
  580.                 throw new Exception("Unknown error while processing sale transaction. Transaction aborted.");
  581.             }
  582.  
  583.             if (transactionResponse.Result == PayFlowProPaymentProcessor.PFP_SUCCESS)
  584.             {
  585.                 // Get the Recurring Response parameters.
  586.                 RecurringResponse RecurResponse = Resp.RecurringResponse;
  587.                 if (RecurResponse != null)
  588.                 {
  589.                     result.CaptureTransactionId = RecurResponse.RPRef;
  590.                     result.SubscriptionTransactionId = RecurResponse.ProfileId;
  591.                     result.NewPaymentStatus = PaymentStatus.Paid;
  592.                 }
  593.             }
  594.             else
  595.             {
  596.                 result.CaptureTransactionResult = transactionResponse.RespMsg;
  597.                 result.AddError(transactionResponse.RespMsg);
  598.             }
  599.  
  600.             // Display the response.
  601.             //result.AddError(Environment.NewLine + PayflowUtility.GetStatus(Resp));
  602.  
  603.             // Get the Transaction Context and check for any contained SDK specific errors (optional code).
  604.             PayPal.Payments.Common.Context TransCtx = Resp.TransactionContext;
  605.             if (TransCtx != null && TransCtx.getErrorCount() > 0)
  606.             {
  607.                 result.AddError(Environment.NewLine + "Transaction Errors = " + TransCtx.ToString());
  608.             }
  609.  
  610.             return result;
  611.  
  612.         }
  613.  
  614.         /// <summary>
  615.         /// Cancels a recurring payment
  616.         /// </summary>
  617.         /// <param name="cancelPaymentRequest">Request</param>
  618.         /// <returns>Result</returns>
  619.         public CancelRecurringPaymentResult CancelRecurringPayment(CancelRecurringPaymentRequest cancelPaymentRequest)
  620.         {
  621.             var result = new CancelRecurringPaymentResult();
  622.  
  623.             _payFlowProPaymentSettings.Unlock();
  624.             UserInfo userInfo = new UserInfo(_payFlowProPaymentSettings.User, _payFlowProPaymentSettings.Vendor,
  625.                 _payFlowProPaymentSettings.Partner, _payFlowProPaymentSettings.Password);
  626.             _payFlowProPaymentSettings.Lock();
  627.  
  628.             PayflowConnectionData connectionData = new PayflowConnectionData(GetPayFlowProUrl());
  629.  
  630.             RecurringInfo RecurInfo = new RecurringInfo();
  631.             //RecurInfo.OrigProfileId = "<PROFILE_ID>";  // RT0000001350
  632.             RecurInfo.OrigProfileId = cancelPaymentRequest.Order.SubscriptionTransactionId;
  633.             // Create a new Recurring Cancel Transaction.
  634.             RecurringCancelTransaction Trans = new RecurringCancelTransaction(
  635.                 userInfo, connectionData, RecurInfo, PayflowUtility.RequestId);
  636.  
  637.             // Submit the Transaction
  638.             Response Resp = Trans.SubmitTransaction();
  639.             // Display the transaction response parameters.
  640.  
  641.             // Display the transaction response parameters.
  642.             if (Resp != null)
  643.             {
  644.                 RecurringResponse RecurResponse = Resp.RecurringResponse;
  645.  
  646.                 if (RecurResponse.TrxResult != "0")
  647.                 {
  648.                     result.AddError(RecurResponse.TrxResult.ToString());
  649.                     result.AddError(RecurResponse.TrxRespMsg);
  650.                 }
  651.  
  652.  
  653.                 //if (TrxnResponse != null)
  654.                 //{
  655.                 //    Console.WriteLine("RESULT = " + TrxnResponse.Result);
  656.                 //    Console.WriteLine("RESPMSG = " + TrxnResponse.RespMsg);
  657.                 //}
  658.  
  659.                 // Get the Recurring Response parameters.
  660.                 //if (RecurResponse != null)
  661.                 //{
  662.                 //    Console.WriteLine("RPREF = " + RecurResponse.RPRef);
  663.                 //    Console.WriteLine("PROFILEID = " + RecurResponse.ProfileId);
  664.                 //}
  665.  
  666.                 // Display the response.
  667.                 //Console.WriteLine(Environment.NewLine + PayflowUtility.GetStatus(Resp));
  668.  
  669.  
  670.                 // Get the Transaction Context and check for any contained SDK specific errors (optional code).
  671.                 PayPal.Payments.Common.Context TransCtx = Resp.TransactionContext;
  672.                 //if (TransCtx != null && TransCtx.getErrorCount() > 0)
  673.                 //{
  674.                 //    Console.WriteLine(Environment.NewLine + "Transaction Errors = " + TransCtx.ToString());
  675.                 //}
  676.             }
  677.  
  678.            
  679.             return result;
  680.         }
  681.  
  682.         /// <summary>
  683.         /// Gets a value indicating whether customers can complete a payment after order is placed but not completed (for redirection payment methods)
  684.         /// </summary>
  685.         /// <param name="order">Order</param>
  686.         /// <returns>Result</returns>
  687.         public bool CanRePostProcessPayment(Order order)
  688.         {
  689.             if (order == null)
  690.                 throw new ArgumentNullException("order");
  691.  
  692.             //it's not a redirection payment method. So we always return false
  693.             return false;
  694.         }
  695.  
  696.         /// <summary>
  697.         /// Gets a route for provider configuration
  698.         /// </summary>
  699.         /// <param name="actionName">Action name</param>
  700.         /// <param name="controllerName">Controller name</param>
  701.         /// <param name="routeValues">Route values</param>
  702.         public void GetConfigurationRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
  703.         {
  704.             actionName = "Configure";
  705.             controllerName = "PaymentPayFlowPro";
  706.             routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Payments.PayFlowPro.Controllers" }, { "area", null } };
  707.         }
  708.  
  709.         /// <summary>
  710.         /// Gets a route for payment info
  711.         /// </summary>
  712.         /// <param name="actionName">Action name</param>
  713.         /// <param name="controllerName">Controller name</param>
  714.         /// <param name="routeValues">Route values</param>
  715.         public void GetPaymentInfoRoute(out string actionName, out string controllerName, out RouteValueDictionary routeValues)
  716.         {
  717.             actionName = "PaymentInfo";
  718.             controllerName = "PaymentPayFlowPro";
  719.             routeValues = new RouteValueDictionary() { { "Namespaces", "Nop.Plugin.Payments.PayFlowPro.Controllers" }, { "area", null } };
  720.         }
  721.  
  722.         public Type GetControllerType()
  723.         {
  724.             return typeof(PaymentPayFlowProController);
  725.         }
  726.  
  727.         public override void Install()
  728.         {
  729.             var settings = new PayFlowProPaymentSettings()
  730.             {
  731.                 UseSandbox = true,
  732.                 User = "User Name",
  733.                 Vendor = "Vendor Id",
  734.                 Partner = "Partner",
  735.                 Password = "Password"
  736.             };
  737.  
  738.             settings.Lock();
  739.             _settingService.SaveSetting(settings);
  740.  
  741.             base.Install();
  742.         }
  743.  
  744.         #endregion
  745.  
  746.         #region Properies
  747.  
  748.         /// <summary>
  749.         /// Gets a value indicating whether capture is supported
  750.         /// </summary>
  751.         public bool SupportCapture
  752.         {
  753.             get
  754.             {
  755.                 return false;
  756.             }
  757.         }
  758.  
  759.         /// <summary>
  760.         /// Gets a value indicating whether partial refund is supported
  761.         /// </summary>
  762.         public bool SupportPartiallyRefund
  763.         {
  764.             get
  765.             {
  766.                 return false;
  767.             }
  768.         }
  769.  
  770.         /// <summary>
  771.         /// Gets a value indicating whether refund is supported
  772.         /// </summary>
  773.         public bool SupportRefund
  774.         {
  775.             get
  776.             {
  777.                 return true;
  778.             }
  779.         }
  780.  
  781.         /// <summary>
  782.         /// Gets a value indicating whether void is supported
  783.         /// </summary>
  784.         public bool SupportVoid
  785.         {
  786.             get
  787.             {
  788.                 return true;
  789.             }
  790.         }
  791.  
  792.         /// <summary>
  793.         /// Gets a recurring payment type of payment method
  794.         /// </summary>
  795.         public RecurringPaymentType RecurringPaymentType
  796.         {
  797.             get
  798.             {
  799.                 return RecurringPaymentType.Automatic;
  800.             }
  801.         }
  802.  
  803.         /// <summary>
  804.         /// Gets a payment method type
  805.         /// </summary>
  806.         public PaymentMethodType PaymentMethodType
  807.         {
  808.             get
  809.             {
  810.                 return PaymentMethodType.Standard;
  811.             }
  812.         }
  813.  
  814.         #endregion
  815.  
  816.  
  817.         public decimal GetAdditionalHandlingFee(System.Collections.Generic.IList<ShoppingCartItem> cart)
  818.         {
  819.             return 0;
  820.             //throw new NotImplementedException();
  821.         }
  822.     }
  823. }
Add Comment
Please, Sign In to add comment