public async Task <HttpResponse> AuthorizeOrder(string OrderId, bool debug = false)
        {
            var request = new OrdersAuthorizeRequest(OrderId);

            request.Prefer("return=representation");
            request.RequestBody(new AuthorizeRequest());
            //3. Call PayPal to authorization an order
            var response = await PayPalClient.Client(_configuration).Execute(request);

            //4. Save the authorization ID to your database. Implement logic to save the authorization to your database for future reference.
            if (debug)
            {
                var result = response.Result <Order>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Authorization Id: {0}",
                                  result.PurchaseUnits[0].Payments.Authorizations[0].Id);
                Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
                Console.WriteLine("Links:");
                foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel,
                                      link.Href,
                                      link.Method);
                }
                AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
                Console.WriteLine("Buyer:");
                Console.WriteLine("\tEmail Address: {0}", result.Payer.Email);
                Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
            }

            return(response);
        }
        //2. Set up your server to receive a call from the client

        /*
         * Method to create order
         *
         * @param debug true = print response data
         * @return HttpResponse<Order> response received from API
         * @throws IOException Exceptions from API if any
         */
        public async Task <HttpResponse> CreateOrder(bool debug = false)
        {
            var request = new OrdersCreateRequest();

            request.Prefer("return=representation");
            request.RequestBody(BuildRequestBody());
            //3. Call PayPal to set up a transaction
            var response = await PayPalClient.Client(_configuration).Execute(request);

            if (debug)
            {
                var result = response.Result <Order>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
                Console.WriteLine("Links:");
                foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
                Console.WriteLine("Total Amount: {0} {1}", amount.CurrencyCode, amount.Value);
            }

            return(response);
        }
Beispiel #3
0
        public async Task <DataResponse <string> > CreatePayment(int userId, int packageId, int subscriptionType, decimal amount)
        {
            try
            {
                var order = new OrderRequest
                {
                    CheckoutPaymentIntent = "CAPTURE",
                    PurchaseUnits         = new List <PurchaseUnitRequest>
                    {
                        new PurchaseUnitRequest
                        {
                            AmountWithBreakdown = new AmountWithBreakdown
                            {
                                Value        = 100.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture),
                                CurrencyCode = "EUR",
                            }
                        }
                    },
                    ApplicationContext = new ApplicationContext
                    {
                        ReturnUrl = _configuration["PayPal:SuccessUrl"],
                        CancelUrl = _configuration["PayPal:FailUrl"]
                    }
                };

                var request = new OrdersCreateRequest();
                request.Prefer("return=representation");
                request.RequestBody(order);

                var responsepp = await PayPalClient.Client(_configuration["PayPal:ClientId"], _configuration["PayPal:ClientSecret"]).Execute(request);

                var result = responsepp.Result <Order>();

                var response = new DataResponse <string>
                {
                    Data      = result.Links.FirstOrDefault(link => link.Rel.Equals("approve"))?.Href,
                    Succeeded = true
                };

                var subscribeModel = new PackageSubscribeDto
                {
                    Id               = 0,
                    UserId           = userId,
                    PackageId        = packageId,
                    SubscriptionType = subscriptionType,
                    Amount           = amount,
                    PaymentId        = result.Id
                };

                _packageService.SubscribeForPackage(subscribeModel);

                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"On create paypal payment: Exception thrown with error {ex.Message}");
                throw new ApplicationException("Due to technical issues we are not able to create the payment!");
            }
        }
        public async Task <Result <Order> > GetOrder(string orderId, bool debug = false)
        {
            var request  = new OrdersGetRequest(orderId);
            var response = await _payPalClient.Client().Execute(request);

            var result = response.Result <Order>();

            //todo: Save things to DB

            return(result != null ?
                   Result <Order> .Ok(result) :
                   Result <Order> .Error($"There are no payments with given Id."));
        }
Beispiel #5
0
        public async Task <DataResponse <Order> > CapturePayPalPayment(string token, string payerId)
        {
            var response = new DataResponse <Order>()
            {
                Succeeded = false, Data = null
            };

            try
            {
                var request = new OrdersCaptureRequest(token);
                request.RequestBody(new OrderActionRequest());

                var httpResponse = await PayPalClient.Client(_configuration["PayPal:ClientId"], _configuration["PayPal:ClientSecret"]).Execute(request);

                if (httpResponse.StatusCode == System.Net.HttpStatusCode.Created)
                {
                    var order = httpResponse.Result <Order>();

                    var saved = _packageService.MarkSubscriptionAsPayed(order.Id);

                    if (saved)
                    {
                        response.Succeeded = true;
                        response.Data      = order;
                    }
                    else
                    {
                        _logger.LogError($"PayPal Payment capture failure, Status: '{httpResponse.StatusCode}'");
                        response.ErrorMessage = "Update subscription payment statys failed";
                    }
                }
                else
                {
                    _logger.LogError($"PayPal Payment capture failure, Status: '{httpResponse.StatusCode}'");
                    response.ErrorMessage = "PayPal payment couldn't be captured, please contact administrators!";
                }

                return(response);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, $"On capture paypal payment: Exception thrown with error {ex.Message}");
                response.ErrorMessage = "Due to technical issues we are not able to capture the paypal payment";
            }

            return(response);
        }
        public async Task <HttpResponse> CaptureAuth(string authorizationId, bool debug = false)
        {
            var request = new AuthorizationsCaptureRequest(authorizationId);

            request.Prefer("return=representation");
            request.RequestBody(new CaptureRequest());
            var response = await PayPalClient.Client(_configuration).Execute(request);

            if (debug)
            {
                var result = response.Result <PayPalCheckoutSdk.Orders.Capture>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Links:");
                foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
            }

            return(response);
        }
        public async Task <HttpResponse> CaptureOrder(string OrderId, bool debug = false)
        {
            var request = new OrdersCaptureRequest(OrderId);

            request.Prefer("return=representation");
            request.RequestBody(new OrderActionRequest());
            //3. Call PayPal to capture an order
            var response = await PayPalClient.Client(_configuration).Execute(request);

            //4. Save the capture ID to your database. Implement logic to save capture to your database for future reference.

            if (debug)
            {
                var result = response.Result <Order>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
                Console.WriteLine("Links:");
                foreach (PayPalCheckoutSdk.Orders.LinkDescription link in result.Links)
                {
                    Console.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                Console.WriteLine("Capture Ids: ");
                foreach (PurchaseUnit purchaseUnit in result.PurchaseUnits)
                {
                    foreach (PayPalCheckoutSdk.Orders.Capture capture in purchaseUnit.Payments.Captures)
                    {
                        Console.WriteLine("\t {0}", capture.Id);
                    }
                }
                AmountWithBreakdown amount = result.PurchaseUnits[0].AmountWithBreakdown;
                Console.WriteLine("Buyer:");
                Console.WriteLine("\tEmail Address: {0}\n\tName: {1}\n\tPhone Number: {2}{3}", result.Payer.Email, result.Payer.Name.FullName, result.Payer.PhoneWithType.PhoneNumber.CountryCallingCode, result.Payer.PhoneWithType.PhoneNumber.NationalNumber);
            }

            return(response);
        }