Пример #1
0
        public async static Task <HttpResponse> CaptureOrder(string AuthorizationId)
        {
            var request = new AuthorizationsCaptureRequest(AuthorizationId);

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

            return(response);
        }
Пример #2
0
        public async Task <HttpResponse> CapturesRequest(bool debug = false)
        {
            //string orderId = "8TH80554NC7396247";
            string orderId = "23139793XR804084D";

            try
            {
                //RefundsGetRequest
                //CapturesGetRequest


                var request = new CapturesGetRequest(orderId);

                PayPalEnvironment payPalEnvironment = new SandboxEnvironment("Af8jLh10kVlYkx4lunz6GSduOt92LyS_hRCOjzRZaU1SZrr1Eb7xYXBjLn-ue6SqiVlsTCEPPlMmL4bI", "EKQBNJEbuBx-yIx0vdtFTiptyfnnyjQVYwhPAmiuKe19T3Viko-B8E-wYrfQuvGdVCOJ97zH9-arreb4");

                var rr = new AuthorizationsCaptureRequest(orderId);

                var response = await new PayPalHttpClient(payPalEnvironment).Execute(rr);
                var result   = response.Result <Order>();
                Console.WriteLine("Retrieved Order Status");
                //result.Payer.A
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                //Console.WriteLine("Intent: {0}", result.CheckoutPaymentIntent);
                Console.WriteLine("Links:");
                //foreach (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);
                //   Console.WriteLine("Response JSON: \n {0}", PayPalClient.ObjectToJSONString(result));
                return(response);
            }
            catch (Exception e)
            {
                OrdersGetRequest request = new OrdersGetRequest(orderId);

                PayPalEnvironment payPalEnvironment = new SandboxEnvironment("Af8jLh10kVlYkx4lunz6GSduOt92LyS_hRCOjzRZaU1SZrr1Eb7xYXBjLn-ue6SqiVlsTCEPPlMmL4bI", "EKQBNJEbuBx-yIx0vdtFTiptyfnnyjQVYwhPAmiuKe19T3Viko-B8E-wYrfQuvGdVCOJ97zH9-arreb4");

                return(await new PayPalHttpClient(payPalEnvironment).Execute(request));
            }
        }
        //This method  can be used to capture the payment on the approved authorization.
        public async static Task<HttpResponse> CaptureOrder(string AuthorizationId, bool debug = false)
        {
            var request = new AuthorizationsCaptureRequest(AuthorizationId);
            request.Prefer("return=representation");
            request.RequestBody(new CaptureRequest());
            var response = await PayPalClient.client().Execute(request);

            if (debug)
            {
                var result = response.Result<Capture>();
                Console.WriteLine("Status: {0}", result.Status);
                Console.WriteLine("Order Id: {0}", result.Id);
                Console.WriteLine("Links:");
                foreach (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 <ActionResult> AuthorizeCaptureOrderPaypal()
        {
            CartDetailsModel cart = (CartDetailsModel)Session["CartDetails"];

            try
            {
                if (CheckProductInCart(cart))
                {
                    //neu on het thi tien hanh thanh toan
                    string orderId = Request["token"];
                    var    requestAuthorization = new OrdersAuthorizeRequest(orderId);
                    requestAuthorization.Prefer("return=representation");
                    requestAuthorization.RequestBody(new AuthorizeRequest());
                    var responseAuthorization = await PayPalClient.client().Execute(requestAuthorization);

                    var resultAuthorization = responseAuthorization.Result <Order>();
                    if (resultAuthorization.Status.Equals("COMPLETED"))
                    {
                        //if success
                        //get authorizationID
                        string AuthorizationId = resultAuthorization.PurchaseUnits[0].Payments.Authorizations[0].Id;
                        var    requestCapture  = new AuthorizationsCaptureRequest(AuthorizationId);
                        requestCapture.Prefer("return=representation");
                        requestCapture.RequestBody(new CaptureRequest());
                        //execute capture
                        var responseCapture = await PayPalClient.client().Execute(requestCapture);

                        var captureOrderResult = responseCapture.Result <PayPalCheckoutSdk.Payments.Capture>();
                        if (captureOrderResult.Status.Equals("PENDING"))//success
                        {
                            //check lai xem con hang trong kho k sau khi customer da thanh toan xong
                            //neu k thi refund tien r tra ve trang shopping-cart
                            if (!CheckProductInCart(cart))
                            {
                                await RefundOrderPaypal(captureOrderResult.Id);

                                return(RedirectToAction("LoadCartDetails", "Cart"));
                            }
                            else
                            {
                                return(FinishOrderPaypal(cart));
                            }
                        }
                        else
                        {
                            TempData["ErrorOrder"] = "Error to order by PayPal!!";
                            return(RedirectToAction("LoadCartDetails", "Cart"));
                        }
                    }
                    else
                    {
                        TempData["ErrorOrder"] = "Error to order by PayPal!!";
                        return(RedirectToAction("LoadCartDetails", "Cart"));
                    }
                }
                else
                {
                    TempData["ErrorOrder"] = "Error to order by PayPal!!";
                    return(RedirectToAction("LoadCartDetails", "Cart"));
                }
            }
            catch (Exception e)
            {
                SendMailSSL.SendErrorToAdmin("Error at AuthorizeCaptureOrderPaypal: " + e.ToString());
            }
            return(RedirectToAction("LoadCartDetails", "Cart"));
        }