示例#1
0
 public void Init()
 {
     foreach (var method in Provider.PaymentMethods)
     {
         GatewayProviderService.Delete(method);
     }
 }
        private IPaymentResult ProcessPayment(IInvoice invoice, TransactionMode transactionMode, decimal amount, ProcessorArgumentCollection args)
        {
            var quickPayResponseData = args.AsQuickPayCallbackResponseModel();

            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, invoice.Total, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = "QuickPay";

            payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.QuickpayPaymentId, quickPayResponseData.PaymentId);
            payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.PaymentAmount, quickPayResponseData.Amount);
            payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.PaymentCurrency, quickPayResponseData.Currency);

            var result = _processor.ProcessPayment(invoice, payment, transactionMode, amount);

            GatewayProviderService.Save(payment);

            if (!result.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, result.Payment.Exception.Message, 0);
            }
            else
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit,
                                                             (transactionMode == TransactionMode.AuthorizeAndCapture ? string.Empty : "Payment authorized and ready for capture"),
                                                             transactionMode == TransactionMode.AuthorizeAndCapture ? invoice.Total : 0);
            }

            return(result);
        }
 /// <summary>
 /// Deletes all <see cref="ITaxMethod"/>s associated with the provider
 /// </summary>
 internal void DeleteAllTaxMethods()
 {
     foreach (var taxMethod in TaxMethods)
     {
         GatewayProviderService.Delete(taxMethod);
     }
 }
        /// <summary>
        /// Performs the actual work of authorizing and capturing a payment.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="amount">
        /// The amount.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentResult"/>.
        /// </returns>
        /// <remarks>
        /// This is a transaction with SubmitForSettlement = true
        /// </remarks>
        protected override IPaymentResult PerformAuthorizeCapturePayment(IInvoice invoice, decimal amount, ProcessorArgumentCollection args)
        {
            var paymentMethodNonce = args.GetPaymentMethodNonce();

            if (string.IsNullOrEmpty(paymentMethodNonce))
            {
                var error = new InvalidOperationException("No payment method nonce was found in the ProcessorArgumentCollection");
                LogHelper.Debug <BraintreeStandardTransactionPaymentGatewayMethod>(error.Message);
                return(new PaymentResult(Attempt <IPayment> .Fail(error), invoice, false));
            }

            var attempt = ProcessPayment(invoice, TransactionOption.Authorize, invoice.Total, paymentMethodNonce);

            var payment = attempt.Payment.Result;

            GatewayProviderService.Save(payment);

            if (!attempt.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, attempt.Payment.Exception.Message, 0);
            }
            else
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "Braintree transaction - authorized and captured", amount);
            }

            return(attempt);
        }
        /// <summary>
        /// Does the actual work of refunding a payment
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/></param>
        /// <param name="payment">The previously Authorize payment to be captured</param>
        /// <param name="amount">The amount to be refunded</param>
        /// <param name="args">Any arguments required to process the payment.</param>
        /// <returns>The <see cref="IPaymentResult"/></returns>
        protected override IPaymentResult PerformRefundPayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            var result = _processor.RefundPayment(invoice, payment, amount);

            if (!result.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied,
                   result.Payment.Exception.Message, 0);
                return result;
            }

            // use the overloaded AppliedPayments method here for testing if we don't have
            // a MerchelloContext
            foreach (var applied in payment.AppliedPayments(GatewayProviderService))
            {
                applied.TransactionType = AppliedPaymentType.Refund;
                applied.Amount = 0;
                applied.Description += " - Refunded";
                GatewayProviderService.Save(applied);
            }

            payment.Amount = payment.Amount - amount;

            if (payment.Amount != 0)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit,
                    "To show partial payment remaining after refund", payment.Amount);
            }

            GatewayProviderService.Save(payment);

            return new PaymentResult(Attempt<IPayment>.Succeed(payment), invoice, false);
        }
        /// <summary>
        /// The perform authorize payment.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentResult"/>.
        /// </returns>
        protected override IPaymentResult PerformAuthorizePayment(IInvoice invoice, ProcessorArgumentCollection args)
        {
            // The Provider settings
            if (BraintreeApiService.BraintreeProviderSettings.DefaultTransactionOption == TransactionOption.SubmitForSettlement)
            {
                return(this.PerformAuthorizeCapturePayment(invoice, invoice.Total, args));
            }

            var paymentMethodToken = args.GetPaymentMethodToken();

            if (string.IsNullOrEmpty(paymentMethodToken))
            {
                var error = new InvalidOperationException("No payment method token was found in the ProcessorArgumentCollection");
                LogHelper.Debug <BraintreeStandardTransactionPaymentGatewayMethod>(error.Message);
                return(new PaymentResult(Attempt <IPayment> .Fail(error), invoice, false));
            }

            var attempt = ProcessPayment(invoice, TransactionOption.Authorize, invoice.Total, paymentMethodToken);

            var payment = attempt.Payment.Result;

            GatewayProviderService.Save(payment);

            if (!attempt.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, attempt.Payment.Exception.Message, 0);
            }
            else
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "To show record of Braintree Authorization", 0);
            }

            return(attempt);
        }
        private IPaymentResult InitializePayment(IInvoice invoice, ProcessorArgumentCollection args, decimal captureAmount)
        {
            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, invoice.Total, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = "PayPal";
            payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.CaptureAmount, captureAmount.ToString(System.Globalization.CultureInfo.InvariantCulture));
            GatewayProviderService.Save(payment);

            var result = _processor.InitializePayment(invoice, payment, args);

            if (!result.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, "PayPal: request initialization error: " + result.Payment.Exception.Message, 0);
            }
            else
            {
                GatewayProviderService.Save(payment);
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "PayPal: initialized", 0);
            }

            return(result);
        }
        /// <summary>
        /// Processes the payment.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="option">
        /// The option.
        /// </param>
        /// <param name="amount">
        /// The amount.
        /// </param>
        /// <param name="token">
        /// The token.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentResult"/>.
        /// </returns>
        protected override IPaymentResult ProcessPayment(IInvoice invoice, TransactionOption option, decimal amount, string token)
        {
            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, amount, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = "Braintree Vault Transaction";
            payment.ExtendedData.SetValue(Braintree.Constants.ProcessorArguments.PaymentMethodNonce, token);

            var result = BraintreeApiService.Transaction.VaultSale(invoice, token, option);

            if (result.IsSuccess())
            {
                payment.ExtendedData.SetBraintreeTransaction(result.Target);

                if (option == TransactionOption.Authorize)
                {
                    payment.Authorized = true;
                }
                if (option == TransactionOption.SubmitForSettlement)
                {
                    payment.Authorized = true;
                    payment.Collected  = true;
                }

                return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, true));
            }

            var error = new BraintreeApiException(result.Errors, result.Message);

            return(new PaymentResult(Attempt <IPayment> .Fail(payment, error), invoice, false));
        }
示例#9
0
        private IPaymentResult ProcessPayment(IInvoice invoice, TransactionMode transactionMode, decimal amount, ProcessorArgumentCollection args)
        {
            var cc = args.AsCreditCardFormData();

            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.CreditCard, amount, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = string.Format("{0} Authorize.Net Credit Card", cc.CreditCardType);
            payment.ExtendedData.SetValue(Constants.ExtendedDataKeys.CcLastFour, cc.CardNumber.Substring(cc.CardNumber.Length - 4, 4).EncryptWithMachineKey());

            var result = _processor.ProcessPayment(invoice, payment, transactionMode, amount, cc);

            GatewayProviderService.Save(payment);

            if (!result.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, result.Payment.Exception.Message, 0);
            }
            else
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit,
                                                             payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.AuthorizationTransactionResult) +
                                                             (transactionMode == TransactionMode.AuthorizeAndCapture ? string.Empty : " to show record of Authorization"),
                                                             transactionMode == TransactionMode.AuthorizeAndCapture ? invoice.Total : 0);
            }

            return(result);
        }
示例#10
0
        /// <summary>
        /// Does the actual work of voiding a payment
        /// </summary>
        /// <param name="invoice">The invoice to which the payment is associated</param>
        /// <param name="payment">The payment to be voided</param>
        /// <param name="args">Additional arguements required by the payment processor</param>
        /// <returns>A <see cref="IPaymentResult"/></returns>
        protected override IPaymentResult PerformVoidPayment(IInvoice invoice, IPayment payment, ProcessorArgumentCollection args)
        {
            var result = _processor.VoidPayment(invoice, payment);

            if (!result.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied,
                                                             result.Payment.Exception.Message, 0);
                return(result);
            }

            // use the overloaded AppliedPayments method here for testing if we don't have
            // a MerchelloContext
            foreach (var applied in payment.AppliedPayments(GatewayProviderService))
            {
                applied.TransactionType = AppliedPaymentType.Refund;
                applied.Amount          = 0;
                applied.Description    += " - **Void**";
                GatewayProviderService.Save(applied);
            }

            payment.Voided = true;
            GatewayProviderService.Save(payment);

            return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, false));
        }
        /// <summary>
        /// Performs the capture payment operation.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="payment">
        /// The payment.
        /// </param>
        /// <param name="amount">
        /// The amount.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentResult"/>.
        /// </returns>
        protected override IPaymentResult PerformCapturePayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            // We need to determine if the entire amount authorized has been collected before marking
            // the payment collected.
            var appliedPayments = GatewayProviderService.GetAppliedPaymentsByPaymentKey(payment.Key);
            var applied         = appliedPayments.Sum(x => x.Amount);

            var isPartialPayment = amount - applied <= 0;

            var processor = new PayPalExpressCheckoutPaymentProcessor(_paypalApiService);
            var record    = processor.VerifySuccessAuthorziation(invoice, payment);

            if (record.Success)
            {
                record = _paypalApiService.ExpressCheckout.Capture(invoice, payment, amount, isPartialPayment);
                payment.SavePayPalTransactionRecord(record);

                if (record.Success)
                {
                    payment.Collected  = (amount + applied) == payment.Amount;
                    payment.Authorized = true;

                    GatewayProviderService.Save(payment);

                    GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "PayPal ExpressCheckout SUCCESS payment", amount);

                    return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, CalculateTotalOwed(invoice).CompareTo(amount) <= 0));
                }

                GatewayProviderService.Save(payment);
            }

            return(new PaymentResult(Attempt <IPayment> .Fail(payment), invoice, false));
        }
示例#12
0
        /// <summary>
        /// Performs the actual work of performing the refund.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="payment">
        /// The payment.
        /// </param>
        /// <param name="amount">
        /// The amount.
        /// </param>
        /// <param name="args">
        /// The args.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentResult"/>.
        /// </returns>
        protected override IPaymentResult PerformRefundPayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            var transaction = payment.ExtendedData.GetBraintreeTransaction();

            if (transaction == null)
            {
                var error = new NullReferenceException("Braintree transaction could not be found and/or deserialized from payment extended data collection");
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, error), invoice, false));
            }

            var attempt = BraintreeApiService.Transaction.Refund(transaction.Id, amount);

            if (!attempt.IsSuccess())
            {
                var error = new BraintreeApiException(attempt.Errors, attempt.Message);
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Refund, error.Message, 0);
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, error), invoice, false));
            }

            payment.Amount = payment.Amount - amount;

            if (payment.Amount != 0)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "To show partial payment remaining after refund", payment.Amount);
            }

            GatewayProviderService.Save(payment);

            return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, false));
        }
        /// <summary>
        /// Returns a list of all countries that can be assigned to a shipment
        /// </summary>
        /// <returns>A collection of <see cref="ICountry"/></returns>
        public IEnumerable <ICountry> GetAllowedShipmentDestinationCountries()
        {
            var shipCountries = GatewayProviderService.GetAllShipCountries().ToArray();

            var elseCountries = shipCountries.Where(x => x.CountryCode == "ELSE").ToArray();

            if (elseCountries.Any())
            {
                // get a list of all providers associated with the else countries
                var providers = new List <IShippingGatewayProvider>();
                foreach (var ec in elseCountries)
                {
                    providers.AddRange(GetGatewayProvidersByShipCountry(ec));
                }

                if (providers.Any(x => x.ShipMethods.Any()))
                {
                    return(_storeSettingService.GetAllCountries());
                }
            }

            var countries = GatewayProviderService.GetAllShipCountries().Where(x => x.CountryCode != "ELSE").Select(x => _storeSettingService.GetCountryByCode(x.CountryCode)).Where(x => x != null);

            return(countries.Distinct());
        }
        /// <summary>
        /// Does the actual work capturing a payment
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/></param>
        /// <param name="payment">The previously Authorize payment to be captured</param>
        /// <param name="amount">The amount to capture</param>
        /// <param name="args">Any arguments required to process the payment.</param>
        /// <returns>The <see cref="IPaymentResult"/></returns>
        protected override IPaymentResult PerformCapturePayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            var payedTotalList   = invoice.AppliedPayments().Select(item => item.Amount).ToList();
            var payedTotal       = (payedTotalList.Count == 0 ? 0 : payedTotalList.Aggregate((a, b) => a + b));
            var isPartialPayment = amount + payedTotal < invoice.Total;

            var result = _processor.CapturePayment(invoice, payment, amount, isPartialPayment);

            //GatewayProviderService.Save(payment);

            if (!result.Payment.Success)
            {
                //payment.VoidPayment(invoice, payment.PaymentMethodKey.Value);
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, "Sagepay: request capture error: " + result.Payment.Exception.Message, 0);
            }
            else
            {
                GatewayProviderService.Save(payment);
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "Sagepay: captured", amount);
                //GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.CaptureTransactionResult), amount);
            }


            return(result);
        }
        /// <summary>
        /// Responsible for creating a
        /// </summary>
        /// <param name="gatewayResource">
        /// The gateway resource.
        /// </param>
        /// <param name="name">
        /// The name.
        /// </param>
        /// <param name="description">
        /// The description.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentGatewayMethod"/>.
        /// </returns>
        public override IPaymentGatewayMethod CreatePaymentMethod(IGatewayResource gatewayResource, string name, string description)
        {
            var available = ListResourcesOffered().FirstOrDefault(x => x.ServiceCode == gatewayResource.ServiceCode);

            if (available == null)
            {
                var error = new InvalidOperationException("The GatewayResource has already been assigned.");

                LogHelper.Error <CardConnectPaymentGatewayProvider>("GatewayResource has alread been assigned", error);

                throw error;
            }

            var attempt = GatewayProviderService.CreatePaymentMethodWithKey(GatewayProviderSettings.Key, name, description, available.ServiceCode);

            if (attempt.Success)
            {
                PaymentMethods = null;

                return(new CardConnectPaymentGatewayMethod(GatewayProviderService, attempt.Result, GetCardConnectService()));
            }

            LogHelper.Error <CardConnectPaymentGatewayProvider>(string.Format("Failed to create a payment method name: {0}, description {1}, paymentCode {2}", name, description, available.ServiceCode), attempt.Exception);

            throw attempt.Exception;
        }
示例#16
0
        private IPaymentResult ProcessPayment(IInvoice invoice, TransactionMode transactionMode, decimal amount, ProcessorArgumentCollection args)
        {
            var po = args.AsPurchaseOrderFormData();

            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.PurchaseOrder, invoice.Total, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.Authorized        = false;
            payment.Collected         = false;
            payment.PaymentMethodName = "Purchase Order";


            var result = _processor.ProcessPayment(invoice, payment, amount, po);

            GatewayProviderService.Save(payment);

            if (!result.Payment.Success)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied, result.Payment.Exception.Message, 0);
            }
            else
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit,
                                                             payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.AuthorizationTransactionResult) +
                                                             (transactionMode == TransactionMode.AuthorizeAndCapture ? string.Empty : " to show record of Authorization"),
                                                             transactionMode == TransactionMode.AuthorizeAndCapture ? invoice.Total : 0);
            }

            return(result);
        }
        /// <summary>
        /// Does the actual work of authorizing and capturing a payment
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/></param>
        /// <param name="amount">The amount to capture</param>
        /// <param name="args">Any arguments required to process the payment.</param>
        /// <returns>The <see cref="IPaymentResult"/></returns>
        protected override IPaymentResult PerformAuthorizeCapturePayment(IInvoice invoice, decimal amount, ProcessorArgumentCollection args)
        {
            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.PurchaseOrder, amount, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.PaymentMethodName = PaymentMethod.Name;
            payment.ReferenceNumber   = PaymentMethod.PaymentCode + "-" + invoice.PrefixedInvoiceNumber();
            payment.Collected         = true;
            payment.Authorized        = true;

            var po = args.AsPurchaseOrderFormData();

            if (string.IsNullOrEmpty(po.PurchaseOrderNumber))
            {
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, new Exception("Error Purchase Order Number is empty")), invoice, false));
            }

            invoice.PoNumber = po.PurchaseOrderNumber;
            MerchelloContext.Current.Services.InvoiceService.Save(invoice);

            GatewayProviderService.Save(payment);

            GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "Cash payment", amount);

            return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, CalculateTotalOwed(invoice).CompareTo(amount) <= 0));
        }
        /// <summary>
        /// Does the actual work of creating and processing the payment
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/></param>
        /// <param name="args">Any arguments required to process the payment.</param>
        /// <returns>The <see cref="IPaymentResult"/></returns>
        protected override IPaymentResult PerformAuthorizePayment(IInvoice invoice, ProcessorArgumentCollection args)
        {
            var po = args.AsPurchaseOrderFormData();

            var payment = GatewayProviderService.CreatePayment(PaymentMethodType.PurchaseOrder, invoice.Total, PaymentMethod.Key);

            payment.CustomerKey       = invoice.CustomerKey;
            payment.PaymentMethodName = PaymentMethod.Name;
            payment.ReferenceNumber   = PaymentMethod.PaymentCode + "-" + invoice.PrefixedInvoiceNumber();
            payment.Collected         = false;
            payment.Authorized        = true;

            if (string.IsNullOrEmpty(po.PurchaseOrderNumber))
            {
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, new Exception("Error Purchase Order Number is empty")), invoice, false));
            }

            invoice.PoNumber = po.PurchaseOrderNumber;
            MerchelloContext.Current.Services.InvoiceService.Save(invoice);

            GatewayProviderService.Save(payment);

            // In this case, we want to do our own Apply Payment operation as the amount has not been collected -
            // so we create an applied payment with a 0 amount.  Once the payment has been "collected", another Applied Payment record will
            // be created showing the full amount and the invoice status will be set to Paid.
            GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, string.Format("To show promise of a {0} payment", PaymentMethod.Name), 0);

            //// If this were using a service we might want to store some of the transaction data in the ExtendedData for record
            ////payment.ExtendData

            return(new PaymentResult(Attempt.Succeed(payment), invoice, false));
        }
        protected override IPaymentResult PerformCapturePayment(IInvoice invoice, IPayment payment, decimal amount,
                                                                ProcessorArgumentCollection args)
        {
            var token   = args["token"];
            var payerId = args["PayerID"];

            var result = _processor.CompletePayment(invoice, payment, token, payerId);

            GatewayProviderService.Save(payment);

            // TODO
            GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "PayPal Payment", payment.Amount);

            /*
             * if (!result.Payment.Success)
             * {
             *      GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Denied,
             *              result.Payment.Exception.Message, 0);
             * }
             * else
             * {
             *      GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit,
             *              payment.ExtendedData.GetValue(Constants.ExtendedDataKeys.CaptureTransactionResult), amount);
             * }
             */

            return(result);
        }
示例#20
0
        /// <summary>
        /// The get taxation by product method.
        /// </summary>
        /// <returns>
        /// The <see cref="ITaxationByProductMethod"/>.
        /// </returns>
        /// <exception cref="NullReferenceException">
        /// Throws a null reference exception if a provider cannot be resolved
        /// </exception>
        private ITaxationByProductMethod GetTaxationByProductMethod()
        {
            var taxMethod = GatewayProviderService.GetTaxMethodForProductPricing();

            if (taxMethod == null)
            {
                LogHelper.Debug <TaxationContext>("Product based pricing is set in settings, but a TaxMethod has not been assigned.");
                return(null);
            }
            var provider = GatewayProviderResolver.GetProviderByKey <TaxationGatewayProviderBase>(taxMethod.ProviderKey);

            if (provider == null)
            {
                var error = new NullReferenceException("Could not reTaxationGatewayProvider for CalculateTaxForProduct could not be resolved");
                LogHelper.Error <TaxationContext>("Resolution failure", error);
                throw error;
            }

            var productProvider = provider as ITaxationByProductProvider;

            if (productProvider != null)
            {
                return(productProvider.GetTaxationByProductMethod(taxMethod.Key));
            }

            LogHelper.Debug <TaxationContext>("Resolved provider did not Implement ITaxationByProductProvider returning no tax");
            return(null);
        }
        /// <summary>
        /// Processes the Authorize and AuthorizeAndCapture transactions
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/> to be paid</param>
        /// <param name="payment">The <see cref="Core.Models.IPayment"/> record</param>
        /// <param name="args"></param>
        /// <returns>The <see cref="Core.Gateways.Payment.IPaymentResult"/></returns>
        public IPaymentResult InitializePayment(IInvoice invoice, IPayment payment, CreditCard creditCard)
        {
            try
            {
                #region New

                //Credit Card Information
                var models   = Map(invoice, payment, creditCard);
                var response = CardConnectService.AuthorizeTransaction(models);

                #endregion New

                if (response != null)
                {
                    //Get the CardConnect Response
                    switch (response.Respstat)
                    {
                    case "A":
                    {
                        payment.Collected  = false;
                        payment.Authorized = true;
                        payment.SaveCarConnectTransactionRecord(response);
                        GatewayProviderService service = new GatewayProviderService();
                        service.ApplyPaymentToInvoice(
                            payment.Key,
                            invoice.Key, Core.AppliedPaymentType.Debit,
                            "CardConnect: " + response.Resptext + " Authorized Amount " + string.Format("{0:C}", invoice.Total) + " for Capture... RetRef: " + response.Retref,
                            0
                            );

                        //If the payment was acepted, redirect the user to a thank you landing page
                        return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, true));
                    }

                    case "B":
                    case "C":
                    default:
                    {
                        payment.Collected  = false;
                        payment.Authorized = false;
                        //If the payment was'nt acepted, redirect the user to a Cancel Url
                        return(new PaymentResult(Attempt <IPayment> .Fail(payment, new Exception("CardConnect: " + response.Resptext)), invoice, true));
                    }
                    }
                }
                else
                {
                    payment.Collected  = false;
                    payment.Authorized = false;
                    return(new PaymentResult(Attempt <IPayment> .Fail(payment, new Exception("CardConnect: Null Response")), invoice, true));
                }
            }
            catch (Exception ex)
            {
                payment.Collected  = false;
                payment.Authorized = false;
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, ex), invoice, true));
            }
        }
示例#22
0
        /// <summary>
        /// The save shipping gateway method.
        /// </summary>
        /// <param name="shippingGatewayMethod">
        /// The shipping gateway method.
        /// </param>
        public override void SaveShippingGatewayMethod(IShippingGatewayMethod shippingGatewayMethod)
        {
            GatewayProviderService.Save(shippingGatewayMethod.ShipMethod);

            // I will be to moving SaveShippingGatewayMethod to the base class and make it virtual to handle
            // this for people, but it will not be a breaking change so no worries.
            base.ShipMethods = null;
        }
        /// <summary>
        /// Resolves all active shipping gateway providers for a given <see cref="IShipCountry"/>
        /// </summary>
        /// <param name="shipCountry">
        /// The ship Country.
        /// </param>
        /// <returns>
        /// A collection of all active shipping gateway providers
        /// </returns>
        public IEnumerable <ShippingGatewayProviderBase> GetGatewayProvidersByShipCountry(IShipCountry shipCountry)
        {
            var gatewayProviders = GatewayProviderService.GetGatewayProvidersByShipCountry(shipCountry);

            return
                (gatewayProviders.Select(
                     provider => GatewayProviderResolver.GetProviderByKey <ShippingGatewayProviderBase>(provider.Key)));
        }
        /// <summary>
        /// Returns a collection of ship methods assigned for this specific provider configuration (associated with the ShipCountry)
        /// </summary>
        /// <returns></returns>
        public override IEnumerable <IShippingGatewayMethod> GetAllShippingGatewayMethods(IShipCountry shipCountry)
        {
            var methods = GatewayProviderService.GetShipMethodsByShipCountryKey(GatewayProviderSettings.Key, shipCountry.Key);

            return(methods
                   .Select(
                       shipMethod => new FixedRateShippingGatewayMethod(AvailableResources.FirstOrDefault(x => shipMethod.ServiceCode.StartsWith(x.ServiceCode)), shipMethod, shipCountry, ShippingFixedRateTable.GetShipRateTable(GatewayProviderService, RuntimeCache, shipMethod.Key))
                       ).OrderBy(x => x.ShipMethod.Name));
        }
示例#25
0
 /// <summary>
 /// Activates a <see cref="IGatewayProviderSettings"/>
 /// </summary>
 /// <param name="gatewayProviderSettings">The <see cref="IGatewayProviderSettings"/> to be activated</param>
 public void ActivateProvider(IGatewayProviderSettings gatewayProviderSettings)
 {
     if (gatewayProviderSettings.Activated)
     {
         return;
     }
     GatewayProviderService.Save(gatewayProviderSettings);
     GatewayProviderResolver.RefreshCache();
 }
        public void Init()
        {
            foreach (var method in Provider.TaxMethods)
            {
                GatewayProviderService.Delete(method);
            }

            Provider.CreateTaxMethod("US");
        }
        private void AssertPaymentApplied(IPaymentResult response, IInvoice invoice)
        {
            // Apply the payment to the invoice if it was not done in the sub class
            var payment = response.Payment.Result;

            if (payment.AppliedPayments(GatewayProviderService).FirstOrDefault(x => x.InvoiceKey == invoice.Key) == null)
            {
                GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, PaymentMethod.Name, payment.Amount);
            }
        }
示例#28
0
        /// <summary>
        /// Gets a collection of available <see cref="IGatewayResource"/>.
        /// </summary>
        /// <returns>
        /// The collection of <see cref="IGatewayResource"/>
        /// </returns>
        public override IEnumerable <IGatewayResource> ListResourcesOffered()
        {
            var countryCodes = GatewayProviderService.GetAllShipCountries().Select(x => x.CountryCode).Distinct();

            var resources =
                countryCodes.Select(x => new GatewayResource(x, x + "-TaxJar"))
                .Where(code => TaxMethods.FirstOrDefault(x => x.CountryCode.Equals(code.ServiceCode)) == null);

            return(resources);
        }
        /// <summary>
        /// Does the actual work of capturing a payment
        /// </summary>
        /// <param name="invoice">The <see cref="IInvoice"/></param>
        /// <param name="payment">the <see cref="IPayment"/></param>
        /// <param name="amount">The amount</param>
        /// <param name="args">Any arguments required to process the payment. (Maybe a username, password or some Api Key)</param>
        /// <returns>The <see cref="IPaymentResult"/></returns>
        protected override IPaymentResult PerformCapturePayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            payment.Collected  = true;
            payment.Authorized = true;

            GatewayProviderService.Save(payment);

            GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "Cash payment", amount);

            return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, CalculateTotalOwed(invoice).CompareTo(amount) <= 0));
        }
示例#30
0
        /// <summary>
        /// Performs a refund or a partial refund.
        /// </summary>
        /// <param name="invoice">
        /// The invoice.
        /// </param>
        /// <param name="payment">
        /// The payment.
        /// </param>
        /// <param name="amount">
        /// The amount.
        /// </param>
        /// <param name="args">
        /// The processor arguments.
        /// </param>
        /// <returns>
        /// The <see cref="IPaymentResult"/>.
        /// </returns>
        protected override IPaymentResult PerformRefundPayment(IInvoice invoice, IPayment payment, decimal amount, ProcessorArgumentCollection args)
        {
            var record = payment.GetPayPalTransactionRecord();

            if (StringExtensions.IsNullOrWhiteSpace(record.Data.CaptureTransactionId))
            {
                var error = new NullReferenceException("PayPal transaction could not be found and/or deserialized from payment extended data collection");
                return(new PaymentResult(Attempt <IPayment> .Fail(payment, error), invoice, false));
            }

            var attempt = _paypalApiService.ExpressCheckout.Refund(invoice, payment, amount);

            // store the transaction
            var refundTransActions = record.RefundTransactions.ToList();

            refundTransActions.Add(attempt);
            record.RefundTransactions = refundTransActions;

            if (!attempt.Success())
            {
                // In the case of a failure, package up the exception so we can bubble it up.
                var ex = new PayPalApiException("PayPal Checkout Express refund response ACK was not Success");
                if (record.SetExpressCheckout.ErrorTypes.Any())
                {
                    ex.ErrorTypes = record.SetExpressCheckout.ErrorTypes;
                }

                // ensure that transaction is stored in the payment
                payment.SavePayPalTransactionRecord(record);
                GatewayProviderService.Save(payment);

                return(new PaymentResult(Attempt <IPayment> .Fail(payment, ex), invoice, false));
            }

            foreach (var applied in payment.AppliedPayments())
            {
                applied.TransactionType = AppliedPaymentType.Refund;
                applied.Amount          = 0;
                applied.Description    += " - Refunded";
                this.GatewayProviderService.Save(applied);
            }

            payment.Amount = payment.Amount - amount;

            if (payment.Amount != 0)
            {
                this.GatewayProviderService.ApplyPaymentToInvoice(payment.Key, invoice.Key, AppliedPaymentType.Debit, "To show partial payment remaining after refund", payment.Amount);
            }

            this.GatewayProviderService.Save(payment);

            return(new PaymentResult(Attempt <IPayment> .Succeed(payment), invoice, false));
        }