Пример #1
0
        public async Task <Payment> CreatePaymentAsync(PaypalTransaction transaction)
        {
            PayPalEnvironment environment;

            if (isSandboxMode)
            {
                environment = new SandboxEnvironment(clientId, clientSecret);
            }
            else
            {
                environment = new LiveEnvironment(clientId, clientSecret);
            }
            var client = new PayPalHttpClient(environment);

            try
            {
                var payment = CreatePayment(transaction);
                var request = new PaymentCreateRequest().RequestBody(payment);

                var response = await client.Execute(request);

                var statusCode = response.StatusCode;
                var result     = response.Result <Payment>();
                return(result);
            }
            catch (HttpException httpException)
            {
                var statusCode = httpException.StatusCode;
                var debugId    = httpException.Headers.GetValues("PayPal-Debug-Id").FirstOrDefault();
                return(null);
            }
        }
Пример #2
0
        private Payment CreatePayment(PaypalTransaction transaction)
        {
            decimal taxRatio           = 0.19M;
            decimal taxAmount          = Math.Round(transaction.TotalAmount * taxRatio, 2, MidpointRounding.AwayFromZero);
            decimal amountWithoutTaxes = transaction.TotalAmount - taxAmount;

            var    split      = transaction.InternalReference.Split('.');
            string baseUrl    = $"{transaction.RedirectUrl.TrimEnd('/')}/{split[0]}/{split[1]}?guid={split[2]}";
            string successUrl = $"{baseUrl}&payment=success&verification={split[3]}";
            string cancelUrl  = $"{baseUrl}&payment=cancel";

            var usCulture = new CultureInfo("en-US");
            var payment   = new Payment()
            {
                Intent       = "sale",
                Transactions = new List <Transaction>
                {
                    new Transaction
                    {
                        Amount = new Amount
                        {
                            Total    = transaction.TotalAmount.ToString("N2", usCulture),
                            Currency = "EUR",
                            Details  = new AmountDetails
                            {
                                Subtotal = amountWithoutTaxes.ToString("N2", usCulture),
                                Tax      = taxAmount.ToString("N2", usCulture)
                            }
                        },
                        Description = transaction.BookingDescription,
                        ItemList    = new ItemList
                        {
                            Items = new List <Item>
                            {
                                new Item
                                {
                                    Price       = amountWithoutTaxes.ToString("N2", usCulture),
                                    Quantity    = "1",
                                    Description = transaction.ArticleDescription,
                                    Currency    = "EUR"
                                }
                            }
                        }
                    }
                },
                RedirectUrls = new RedirectUrls
                {
                    CancelUrl = cancelUrl,
                    ReturnUrl = successUrl
                },
                Payer = new Payer
                {
                    PaymentMethod = "paypal"
                }
            };

            return(payment);
        }
        public async Task CreatePaymentAsync(decimal totalAmount, string bookingDescription, string articleDescription, string internalReference)
        {
            var transaction = new PaypalTransaction
            {
                TotalAmount = totalAmount,
                BookingDescription = bookingDescription,
                ArticleDescription = articleDescription,
                InternalReference = internalReference,
                RedirectUrl = RedirectUrl
            };

            var response = await httpClient.PostAsync("api/payment/create", transaction.ToHttpContent());
            var payment = await response.Content.ReadAsync<Payment>();

            var approvalUrl = payment.Links.FirstOrDefault(x => x.Rel == "approval_url").Href;

            await jsRuntime.InvokeVoidAsync("blazorPaypal", approvalUrl);
        }