public async Task <object> CreateOrder([FromBody] dynamic OrderAmount, bool debug = true)
        {
            var oa      = (string)OrderAmount["OrderAmount"];
            var request = new OrdersCreateRequest();

            request.Headers.Add("prefer", "return=representation");
            request.RequestBody(BuildRequestBody(oa));

            var response = await PayPalClient.client().Execute(request);

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

            response.Headers.Add("id", result.Id);
            if (debug)
            {
                Debug.WriteLine("Status: {0}", result.Status);
                Debug.WriteLine("Order Id: {0}", result.Id);
                Debug.WriteLine("Intent: {0}", result.Intent);
                Debug.WriteLine("Links:");
                foreach (LinkDescription link in result.Links)
                {
                    Debug.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
            }
            return(response);
        }
Example #2
0
        //[Route("Payments/CaptureOrder")]
        public async Task <HttpResponse> CaptureOrder([FromBody] dynamic OrderId, bool debug = true)
        {
            var oi      = (string)OrderId["OrderId"];
            var request = new OrdersCaptureRequest(oi);

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

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

            response.Headers.Add("Payee Name", result.Payer.Name.GivenName + " " + result.Payer.Name.Surname);
            //4. Save the capture ID to your database. Implement logic to save capture to your database for future reference.
            if (debug)
            {
                Debug.WriteLine("Status: {0}", result.Status);
                Debug.WriteLine("Order Id: {0}", result.Id);
                Debug.WriteLine("Intent: {0}", result.Intent);
                Debug.WriteLine("Links:");
                foreach (LinkDescription link in result.Links)
                {
                    Debug.WriteLine("\t{0}: {1}\tCall Type: {2}", link.Rel, link.Href, link.Method);
                }
                Debug.WriteLine("Capture Ids: ");
                foreach (PurchaseUnit purchaseUnit in result.PurchaseUnits)
                {
                    foreach (Capture capture in purchaseUnit.Payments.Captures)
                    {
                        Debug.WriteLine("\t {0}", capture.Id);
                    }
                }
                AmountWithBreakdown amount = result.PurchaseUnits[0].Amount;
                Debug.WriteLine("Buyer:");
                Debug.WriteLine("\tEmail Address: {0}\n\tName: {1}\n\tPhone Number: {2}{3}", result.Payer.EmailAddress, result.Payer.Name.GivenName + " " + result.Payer.Name.Surname, result.Payer.Phone.CountryCode, result.Payer.Phone.NationalNumber);
            }
            Debug.WriteLine("************************************************************************************** ");
            //Id of last inserted record in payments table
            int last_payment_id = _context.Payments.Max(item => item.id);

            Debug.WriteLine("Buyer:", last_payment_id);
            Payments payments = await _context.Payments.SingleOrDefaultAsync(m => m.id == last_payment_id);

            Debug.WriteLine("************************************************************************************** ");

            payments.amount        = result.PurchaseUnits[0].Amount.Value;
            payments.transactionId = result.Id;
            payments.payeeEmail    = result.Payer.EmailAddress;

            if (ModelState.IsValid)
            {
                _context.Update(payments);
                await _context.SaveChangesAsync();
            }


            return(response);
        }