Beispiel #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);
        }
        //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"));
        }