Пример #1
0
        public MobSocialProcessPaymentResultModel ProcessPayment(Customer Customer, CustomerPaymentMethod PaymentMethod, decimal PaymentAmount, bool AuthorizeOnly = false)
        {
            //check if the plugin is installed and activated
            //depending on the payment amount, we may wish to switch between macro and micro payments
            //check if plugin is installed or not
            //TODO: Change the other payment method to appropriate one for macro payments
            var paymentPluginSystemName = PaymentAmount < MicroMacroPaymentSwitchingAmount ? "Payments.PayPalDirect" : "Payments.PayPalDirect";

            //if we get an instance, then the plugin is installed
            var plugin = GetPluginInstance(paymentPluginSystemName);

            //plugin should be available and if it's an authorization transaction, the plugin should actually support authorize and capture
            if (plugin == null || (!plugin.SupportCapture && AuthorizeOnly))
            {
                return(null);
            }

            //default to first billing address
            Customer.BillingAddress = Customer.BillingAddress ?? Customer.Addresses.First();

            var key  = _mobSecurityService.GetSavedEncryptionKey();
            var salt = _mobSecurityService.GetSavedSalt();

            //decrypt card number stored for processing
            var cardNumber = _mobSecurityService.Decrypt(PaymentMethod.CardNumber, key, salt); //decrypt the card info

            //now create a payment processing request
            var paymentProcessingRequest = new ProcessPaymentRequest()
            {
                CustomerId              = Customer.Id,
                OrderTotal              = PaymentAmount,
                CreditCardName          = PaymentMethod.NameOnCard,
                CreditCardExpireMonth   = int.Parse(PaymentMethod.ExpireMonth),
                CreditCardExpireYear    = int.Parse(PaymentMethod.ExpireYear),
                PaymentMethodSystemName = paymentPluginSystemName,
                CreditCardType          = PaymentMethod.CardIssuerType,
                CreditCardNumber        = cardNumber,
                StoreId = _storeContext.CurrentStore.Id
            };

            var resultModel = new MobSocialProcessPaymentResultModel()
            {
                ProcessPaymentResult    = plugin.ProcessPayment(paymentProcessingRequest),
                PaymentMethodSystemName = paymentPluginSystemName
            };

            return(resultModel);
        }
Пример #2
0
        public ActionResult PurchasePass(PurchasePassModel Model)
        {
            if (Model.Amount <= 0)
            {
                return(Json(new { Success = false, Message = "Minimum amount to pay should be greater than zero" }));
            }

            //check if the payment method provided by customer is new or an existing one
            CustomerPaymentMethod paymentMethod = null;

            if (Model.CustomerPaymentMethodId == 0)
            {
                paymentMethod = new CustomerPaymentMethod()
                {
                    CustomerId    = _workContext.CurrentCustomer.Id,
                    IsVerified    = false,
                    PaymentMethod = Model.CustomerPaymentRequest.PaymentMethod
                };

                switch (Model.CustomerPaymentRequest.PaymentMethod)
                {
                case PaymentMethodType.CreditCard:
                case PaymentMethodType.DebitCard:
                    //if it's a card, it should be valid. why send to payment processor if basic checks fail?

                    var cardNumber = CardHelper.StripCharacters(Model.CustomerPaymentRequest.CardNumber);
                    //let's validate the card for level 1 check (luhn's test) first before storing
                    var isCardValid = CardHelper.IsCardNumberValid(cardNumber);

                    if (isCardValid)
                    {
                        var cardIssuer       = CardHelper.GetCardTypeFromNumber(cardNumber);
                        var cardNumberMasked = CardHelper.MaskCardNumber(cardNumber);

                        var key  = _mobSecurityService.GetSavedEncryptionKey();
                        var salt = _mobSecurityService.GetSavedSalt();
                        cardNumber = _mobSecurityService.Encrypt(cardNumber, key, salt);     //encrypt the card info

                        //fine if the card is valid, but is the card number already in our record, then not possible to save the same again
                        if (_paymentMethodService.DoesCardNumberExist(cardNumber))
                        {
                            return(Json(new { Success = false, Message = "The card number is already saved in records" }));
                        }
                        //all good so far, but payment method will still be non-verified till first transaction is done.

                        paymentMethod.CardNumber       = cardNumber;
                        paymentMethod.CardNumberMasked = cardNumberMasked;
                        paymentMethod.ExpireMonth      = Model.CustomerPaymentRequest.ExpireMonth;
                        paymentMethod.ExpireYear       = Model.CustomerPaymentRequest.ExpireYear;
                        paymentMethod.NameOnCard       = Model.CustomerPaymentRequest.NameOnCard;
                        paymentMethod.CardIssuerType   = cardIssuer.ToString().ToLower();
                        paymentMethod.DateCreated      = DateTime.UtcNow;
                        paymentMethod.DateUpdated      = DateTime.UtcNow;
                    }
                    else
                    {
                        return(Json(new { Success = false, Message = "Invalid card number" }));
                    }
                    break;

                case PaymentMethodType.BitCoin:
                    //TODO: Bitcoin related data here
                    break;

                case PaymentMethodType.Paypal:
                    //TODO: Paypal related data here
                    break;
                }

                //save the payment method
                _paymentMethodService.Insert(paymentMethod);
            }
            else
            {
                //so we have a saved method, let's retrieve it
                paymentMethod = _paymentMethodService.GetById(Model.CustomerPaymentMethodId);

                //okays...but does this payment method actually belongs to this customer?
                if (paymentMethod.CustomerId != _workContext.CurrentCustomer.Id)
                {
                    return(Json(new { Success = false, Message = "Invalid payment method" }));
                }
            }
            //so we are good to go with this transaction...let's see how to proceed

            //we need to make sure that purchase amount is at least as minimum as battle
            //TODO: Remove comment when picture battles are ready
            var battle = _videoBattleService.GetById(Model.BattleId); // Model.BattleType == BattleType.Video ? _videoBattleService.GetById(Model.BattleId) : null;

            if (Model.PurchaseType == PurchaseType.VoterPass && Model.Amount < battle.MinimumVotingCharge)
            {
                return(Json(new { Success = false, Message = "Payment amount is less than minimum voting amount" }));
            }

            if (Model.PurchaseType == PurchaseType.SponsorPass && Model.Amount < battle.MinimumSponsorshipAmount)
            {
                //only if user is not increasing the sponsorship amount should we send error, else accept any amount
                var sponsorshipOrders = _sponsorPassService.GetSponsorPassOrders(_workContext.CurrentCustomer.Id, Model.BattleId, Model.BattleType);
                if (!sponsorshipOrders.Any())
                {
                    return(Json(new { Success = false, Message = "Payment amount is less than minimum sponsorship amount" }));
                }
            }

            //process the payment now
            var paymentResponse = _paymentProcessingService.ProcessPayment(_workContext.CurrentCustomer, paymentMethod);

            if (paymentResponse.Success)
            {
                //let's verify the payment method first if it's not
                if (!paymentMethod.IsVerified)
                {
                    paymentMethod.IsVerified = true;
                    _paymentMethodService.Update(paymentMethod);
                }

                switch (Model.PurchaseType)
                {
                case PurchaseType.VoterPass:
                    //let's create voter pass
                    var voterPassId = _voterPassService.CreateVoterPass(Model.BattleType, Model.BattleId,
                                                                        paymentResponse, paymentMethod, Model.Amount);
                    return(Json(new { Success = true, PassId = voterPassId }));

                case PurchaseType.SponsorPass:
                    //let's create sponsorship pass
                    var sponsorPassId = _sponsorPassService.CreateSponsorPass(Model.BattleType, Model.BattleId, paymentResponse, paymentMethod, Model.Amount);
                    return(Json(new { Success = true, PassId = sponsorPassId }));
                }
            }
            return(Json(new { Success = false, Message = "Payment failed", Errors = paymentResponse.Errors }));
        }