Exemplo n.º 1
0
        /// <summary>
        /// Encrypt with cng
        /// </summary>
        /// <param name="dummy"></param>
        /// <param name="mobilPayEncrypt"></param>
        /// <returns></returns>
        public static int EncryptWithCng(this MobilpayEncryptDecrypt.MobilpayEncryptDecrypt dummy, MobilpayEncrypt mobilPayEncrypt)
        {
            try
            {
                var bytes  = Encoding.ASCII.GetBytes(mobilPayEncrypt.Data);
                var random = new Random();
                var array  = new byte[8];
                for (var i = 0; i < array.Length; i++)
                {
                    array[i] = (byte)random.Next(0, 255);
                }
                Rc4(ref bytes, array);
                var x509Certificate = new X509Certificate2(mobilPayEncrypt.X509CertificateFilePath);

                var rSaCng = x509Certificate.GetRSAPublicKey();
                rSaCng.ExportParameters(false);
                var inArray = rSaCng.Encrypt(array, RSAEncryptionPadding.Pkcs1);
                mobilPayEncrypt.EncryptedData = Convert.ToBase64String(bytes);
                mobilPayEncrypt.EnvelopeKey   = Convert.ToBase64String(inArray);
            }
            catch (CryptographicException ex)
            {
                throw ex;
            }
            return(0);
        }
Exemplo n.º 2
0
        public PaymentResult ConfirmPayment(string textxml, string env_key)
        {
            var contentRootPath = m_HostingEnvironment.ContentRootPath.AddBackslash();
            var keypath         = Path.GetFullPath(Path.Combine(contentRootPath, m_PaymentConfiguration.PathToPrivateKey));

            PaymentResult result = new PaymentResult();

            MobilpayEncryptDecrypt.MobilpayEncryptDecrypt encdecrypt = new MobilpayEncryptDecrypt.MobilpayEncryptDecrypt();
            MobilpayDecrypt decrypt = new MobilpayDecrypt();

            decrypt.Data               = textxml;
            decrypt.EnvelopeKey        = env_key;
            decrypt.PrivateKeyFilePath = keypath;
            //encdecrypt.Decrypt(decrypt);
            encdecrypt.Decrypt(decrypt);
            Mobilpay_Payment_Request_Card card = new Mobilpay_Payment_Request_Card();

            card = encdecrypt.GetCard(decrypt.DecryptedData);

            var panMasked = card.Confirm.PanMasked;

            m_UserToken = card.Confirm.TokenId;
            m_Logger.LogInformation($"User token retrieved: {String.IsNullOrEmpty(card.Confirm.TokenId)}");
            var tokenExpirationDate = card.Confirm.TokenExpirationDate;

            m_Logger.LogInformation($"Rezultatul tranzactiei este: {card.Confirm.Action}");

            switch (card.Confirm.Action)
            {
            case "confirmed": //plata efectuata
            case "paid":      //bani blocati
            {
                decimal paidAmount = card.Confirm.Original_Amount;
                result.ErrorMessage = card.Confirm.Crc;
                if (card.Confirm.Action == "confirmed" && card.Confirm.Error.Code == "0")
                {
                    //var invoice = m_Repository.All<Invoice>().Where(i => i.Id == nMessage.InvoiceId).First();
                    //if (!invoice.IsPaid)
                    //{
                    //    m_NetopiaSystem.RecordInvoicePayment(invoice.Id, paidAmount);
                    //}
                }
                break;
            }

            default:
            {
                result.ErrorType    = "0x02";
                result.ErrorCode    = "0x300000f6";
                result.ErrorMessage = "mobilpay_refference_action paramaters is invalid";
                break;
            }
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Confirm payment
        /// </summary>
        /// <param name="textXml"></param>
        /// <param name="envKey"></param>
        /// <returns></returns>
        public async Task <MobilPayPaymentResponse> ConfirmPaymentAsync(string textXml, string envKey)
        {
            var rootPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
            var filePath = Path.GetFullPath(Path.Combine(rootPath, _configuration.PathToPrivateKey));

            var result = new MobilPayPaymentResponse
            {
                ErrorCode = "0"
            };

            var encryptDecrypt = new MobilpayEncryptDecrypt.MobilpayEncryptDecrypt();
            var decrypt        = new MobilpayDecrypt
            {
                Data               = textXml,
                EnvelopeKey        = envKey,
                PrivateKeyFilePath = filePath
            };

            encryptDecrypt.Decrypt(decrypt);
            var card         = encryptDecrypt.GetCard(decrypt.DecryptedData);
            var orderId      = ExtractOrderId(card.OrderId);
            var orderRequest = await _orderProductService.GetOrderByIdAsync(orderId);

            if (!orderRequest.IsSuccess)
            {
                return(new MobilPayPaymentResponse
                {
                    ErrorType = "0x02",
                    ErrorCode = "0x300000f6",
                    ErrorMessage = "mobilpay_refference_action paramaters is invalid"
                });
            }

            var order   = orderRequest.Result;
            var payment = new Payment
            {
                PaymentMethodId      = MobilPayResources.MobilPay,
                GatewayTransactionId = card.OrderId,
                PaymentStatus        = PaymentStatus.Failed,
                Total          = order.Total,
                UserId         = order.UserId,
                FailureMessage = card.Card.SerializeAsJson()
            };
            var orderState = order.OrderState;

            switch (card.Confirm.Action)
            {
            case "confirmed":
            case "paid":
            {
                result.ErrorMessage = card.Confirm.Crc;
                if (card.Confirm.Action == "confirmed" && card.Confirm.Error.Code == "0")
                {
                    payment.PaymentStatus = PaymentStatus.Succeeded;
                    orderState            = OrderState.PaymentReceived;
                }
                break;
            }

            default:
            {
                result.ErrorType    = "0x02";
                result.ErrorCode    = "0x300000f6";
                result.ErrorMessage = "mobilpay_refference_action paramaters is invalid";
                orderState          = OrderState.PaymentFailed;
                break;
            }
            }

            var addPaymentRequest = await _paymentService.AddPaymentAsync(orderId, payment);

            if (addPaymentRequest.IsSuccess)
            {
                await _orderProductService.ChangeOrderStateAsync(orderId, orderState);
            }

            return(result);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Create payment
        /// </summary>
        /// <param name="hostingDomain"></param>
        /// <param name="orderId"></param>
        /// <returns></returns>
        public async Task <ResultModel <MobilpayEncrypt> > CreatePaymentAsync(string hostingDomain, Guid?orderId)
        {
            var response     = new ResultModel <MobilpayEncrypt>();
            var encrypt      = new MobilpayEncrypt();
            var encDec       = new MobilpayEncryptDecrypt.MobilpayEncryptDecrypt();
            var orderRequest = await _orderProductService.GetOrderByIdAsync(orderId);

            if (!orderRequest.IsSuccess)
            {
                return(response);
            }

            var isPayedRequest = await _paymentService.IsOrderPayedAsync(orderId);

            if (isPayedRequest.IsSuccess)
            {
                return(response);
            }

            var order          = orderRequest.Result;
            var addressRequest = await _userAddressService.GetAddressByIdAsync(order.BillingAddress);

            var address = addressRequest.IsSuccess ? addressRequest.Result : new Address();
            var user    = await _userManager.UserManager.FindByIdAsync(order.UserId.ToString());

            try
            {
                var card        = new Mobilpay_Payment_Request_Card();
                var invoice     = new Mobilpay_Payment_Invoice();
                var billing     = new Mobilpay_Payment_Address();
                var shipping    = new Mobilpay_Payment_Address();
                var contactInfo = new Mobilpay_Payment_Request_Contact_Info();
                var url         = new Mobilpay_Payment_Request_Url();

                var enc = new MobilpayEncryptDecrypt.MobilpayEncryptDecrypt();
                card.OrderId     = CreateMobilPayOrderId(order.Id);
                card.Type        = "card";
                card.Signature   = _configuration.Signature;
                url.ConfirmUrl   = $"{hostingDomain}/MobilPay/ConfirmCard";
                url.ReturnUrl    = $"{hostingDomain}/MobilPay/ReturnCard";
                card.Url         = url;
                card.TimeStamp   = DateTime.Now.ToString("yyyyMMddhhmmss");
                invoice.Amount   = order.Total;
                invoice.Currency = order.Currency?.Code;
                invoice.Details  = $"#{orderId}";

                billing.FirstName  = user?.FirstName;
                billing.LastName   = user?.LastName;
                billing.Email      = user?.Email;
                billing.MobilPhone = address.Phone;
                billing.Address    = address.AddressLine1;
                billing.ZipCode    = address.ZipCode;
                billing.Country    = address.Country?.Name;
                billing.City       = address.StateOrProvince?.Name;

                contactInfo.Billing             = billing;
                shipping.Sameasbilling          = "1";
                contactInfo.Shipping            = shipping;
                invoice.ContactInfo             = contactInfo;
                card.Invoice                    = invoice;
                encrypt.Data                    = enc.GetXmlText(card);
                encrypt.X509CertificateFilePath = GetPathToCertificate();
                encDec.EncryptWithCng(encrypt);
                await _orderProductService.ChangeOrderStateAsync(orderId, OrderState.PendingPayment);

                response.IsSuccess = true;
                response.Result    = encrypt;
                return(response);
            }
            catch (Exception ex)
            {
                response.Errors.Add(new ErrorModel(string.Empty, ex.Message));
            }

            return(response);
        }
Exemplo n.º 5
0
        public MobilpayEncrypt CreatePaymentForNetopia(int invoiceId)
        {
            MobilpayEncrypt encrypt = new MobilpayEncrypt();

            Mobilpay_Payment_Request_Card            card     = new Mobilpay_Payment_Request_Card();
            Mobilpay_Payment_Invoice                 invoice  = new Mobilpay_Payment_Invoice();
            Mobilpay_Payment_Address                 billing  = new Mobilpay_Payment_Address();
            Mobilpay_Payment_Address                 shipping = new Mobilpay_Payment_Address();
            Mobilpay_Payment_Invoice_Item            itmm     = new Mobilpay_Payment_Invoice_Item();
            Mobilpay_Payment_Invoice_Item            itmm1    = new Mobilpay_Payment_Invoice_Item();
            Mobilpay_Payment_ItemCollection          itmColl  = new Mobilpay_Payment_ItemCollection();
            Mobilpay_Payment_Exchange_RateCollection exColl   = new Mobilpay_Payment_Exchange_RateCollection();
            Mobilpay_Payment_Exchange_Rate           ex       = new Mobilpay_Payment_Exchange_Rate();
            Mobilpay_Payment_Request_Contact_Info    ctinfo   = new Mobilpay_Payment_Request_Contact_Info();
            Mobilpay_Payment_Confirm                 conf     = new Mobilpay_Payment_Confirm();
            Mobilpay_Payment_Request_Url             url      = new Mobilpay_Payment_Request_Url();



            MobilpayEncryptDecrypt.MobilpayEncryptDecrypt encdecr = new MobilpayEncryptDecrypt.MobilpayEncryptDecrypt();
            card.OrderId   = new Random().Next().ToString();
            card.Type      = "card";
            card.Signature = m_PaymentConfiguration.Signature;
            url.ConfirmUrl = m_PaymentConfiguration.ConfirmUrl;
            url.ReturnUrl  = m_PaymentConfiguration.ReturnUrl;
            //card.Service = service;
            card.Url         = url;
            card.TimeStamp   = DateTime.Now.ToString("yyyyMMddhhmmss");
            invoice.Amount   = youramount;
            invoice.Currency = yourcurrency;

            invoice.Details        = yourdetails;
            invoice.Items          = new Mobilpay_Payment_ItemCollection();
            billing.FirstName      = "ceva";
            billing.LastName       = "ceva";
            billing.IdentityNumber = "ceva";
            billing.FiscalNumber   = "ceva";
            billing.MobilPhone     = "ceva";
            billing.Type           = "person";
            billing.ZipCode        = "ceva";
            billing.Iban           = "ceva";
            billing.Address        = "ceva";
            billing.Bank           = "ceva";
            billing.City           = "ceva";
            billing.Country        = "ceva";
            billing.County         = "ceva";
            billing.Email          = "*****@*****.**";

            shipping.Sameasbilling = "1";


            ctinfo.Billing  = billing;
            ctinfo.Shipping = shipping;


            invoice.ContactInfo = ctinfo;


            card.Invoice = invoice;
            if (String.IsNullOrWhiteSpace(m_UserToken) == false)
            {
                card.Invoice.TokenId = m_UserToken;
            }

            encrypt.Data = encdecr.GetXmlText(card);
            encrypt.X509CertificateFilePath = GetPathToCertificate();
            m_Logger.LogInformation($"X509CertificateFilePath= [{encrypt.X509CertificateFilePath}]");
            //encdecr.Encrypt(encrypt);
            //Apelul urmator nu arunca exceptie
            encdecr.EncryptWithCng(encrypt);


            return(encrypt);
        }