Ejemplo n.º 1
0
        public async Task <ActionResult> ProcessPayment([FromBody] PaymentCreation payment)
        {
            var command   = _mapper.Map <ProcessPaymentCommand>(payment);
            var paymentId = await Mediator.Send(command);

            return(CustomResponse("GetPayment", new { command.MerchantId, paymentId }));
        }
        public ActionResult PaymentInfo()
        {
            var model = new PaymentInfoModel();

            var storeScope = this.GetActiveStoreScopeConfiguration(_storeService, _workContext);
            var payPalPlusBrasilPaymentSettings = _settingService.LoadSetting <PayPalPlusBrasilPaymentSettings>(storeScope);

            var customer           = _customerService.GetCustomerById(_workContext.CurrentCustomer.Id);
            var customerPayPalPlus = _payPalPlusCustomerService.GetCustomerPayPalPlus(customer);

            string username = payPalPlusBrasilPaymentSettings.RestAPIClientId;
            string password = payPalPlusBrasilPaymentSettings.RestAPISecrect;

            TokenResponse   tokenResponse   = null;
            PaymentResponse paymentResponse = null;
            PaymentMessage  paymentMessage  = null;

            var number     = string.Empty;
            var complement = string.Empty;
            var cpfCnpj    = string.Empty;

            try
            {
                using (var token = new Token(payPalPlusBrasilPaymentSettings.UseSandbox))
                    tokenResponse = token.CreateAsync(username, password).ConfigureAwait(false).GetAwaiter().GetResult();

                VerifyProfileExperience(payPalPlusBrasilPaymentSettings, tokenResponse);

                paymentMessage = GetPaymentoMessage(payPalPlusBrasilPaymentSettings, customer);

                if (payPalPlusBrasilPaymentSettings.Log)
                {
                    _logger.InsertLog(LogLevel.Information, "Nop.Plugin.Payments.PayPalPlusBrasil.PaymentMessage", JsonConvert.SerializeObject(paymentMessage, _jsonSettings), customer);
                }

                using (var paymentCreation = new PaymentCreation(payPalPlusBrasilPaymentSettings.UseSandbox))
                    paymentResponse = paymentCreation.CreateAsync(paymentMessage, tokenResponse.AcessToken).ConfigureAwait(false).GetAwaiter().GetResult();

                if (payPalPlusBrasilPaymentSettings.Log)
                {
                    _logger.InsertLog(LogLevel.Information, "Nop.Plugin.Payments.PayPalPlusBrasil.PaymentResponse", JsonConvert.SerializeObject(paymentResponse, _jsonSettings), customer);
                }

                new AddressHelper(_addressAttributeParser, _workContext).GetCustomNumberAndComplement(customer.BillingAddress.CustomAttributes,
                                                                                                      out number, out complement, out cpfCnpj);

                model.ApprovalUrl    = GetAprovalUrl(paymentResponse.Links);
                model.Mode           = payPalPlusBrasilPaymentSettings.UseSandbox ? "sandbox" : "live";
                model.PayerFirstName = customer.BillingAddress.FirstName;
                model.PayerLastName  = customer.BillingAddress.LastName;

                string email = !string.IsNullOrWhiteSpace(customer.Email) ? customer.Email : customer.BillingAddress.Email;

                model.PayerEmail            = email;
                model.PayerPhone            = AddressHelper.FormatarCelular(customer.ShippingAddress.PhoneNumber);
                model.PayerTaxId            = cpfCnpj;
                model.DisableContinue       = payPalPlusBrasilPaymentSettings.IdButtonConfirmOrFunction;
                model.EnableContinue        = payPalPlusBrasilPaymentSettings.IdButtonConfirmOrFunction;
                model.HabilitarParcelamento = payPalPlusBrasilPaymentSettings.HabilitarParcelamento ? "1" : "0";
                model.ParcelamentoMaximo    = payPalPlusBrasilPaymentSettings.ParcelamentoMaximo.ToString();
                model.CPFCNPJ         = (cpfCnpj.Length <= 11) ? "BR_CPF" : "BR_CNPJ";
                model.PaymentIdPayPal = paymentResponse.Id;
                model.RememberedCards = (customerPayPalPlus != null) ? customerPayPalPlus.RememberedCards : string.Empty;

                return(View("~/Plugins/Payments.PayPalPlusBrasil/Views/PaymentPayPalPlusBrasil/PaymentInfo.cshtml", model));
            }
            catch (Exception ex)
            {
                _logger.Error("PNop.Plugin.Payments.PayPalPlusBrasil.Controllers", ex, customer);
                throw;
            }
        }