public async Task <IActionResult> Execute(string paymentId, string PayerId) { var environment = new SandboxEnvironment("Acc2-UPp-z25_Olh73h5VZB3XjR16eUKtL2lHoIc27IJn8-2f5R8-Kish229pYjzdy18KR8khHJRQO5Q", "EIb_0hbZQPAEioCGLAzVpn87zRswB7zLAoRtda06Oc4IhrDAmtGYAI2z6xYplX6TdARnsuVh2TC3tHNM"); var client = new PayPalHttpClient(environment); PaymentExecuteRequest request = new PaymentExecuteRequest(paymentId); request.RequestBody(new PaymentExecution() { PayerId = PayerId }); await _context.Orders.Where(m => m.Paymentid == paymentId).ForEachAsync(m => m.IsConfirm = true); await _context.SaveChangesAsync(); try { BraintreeHttp.HttpResponse response = await client.Execute(request); var statusCode = response.StatusCode; Payment result = response.Result <Payment>(); return(Redirect("sold")); } catch (HttpException httpException) { var statusCode = httpException.StatusCode; var debugId = httpException.Headers.GetValues("PayPal-Debug-Id").FirstOrDefault(); return(BadRequest(debugId)); } }
public async Task <IActionResult> ConfirmAsync([FromRoute] string id) { string webroot = HttpContext.Request.Scheme; string webroot1 = HttpContext.Request.Host.ToUriComponent(); string pathReturn = "http://" + webroot1 + "/paypal/execute-payment"; string pathCancel = "http://" + webroot1 + "/paypal/cancel"; var environment = new SandboxEnvironment("Acc2-UPp-z25_Olh73h5VZB3XjR16eUKtL2lHoIc27IJn8-2f5R8-Kish229pYjzdy18KR8khHJRQO5Q", "EIb_0hbZQPAEioCGLAzVpn87zRswB7zLAoRtda06Oc4IhrDAmtGYAI2z6xYplX6TdARnsuVh2TC3tHNM"); var client = new PayPalHttpClient(environment); string idUser = id; var payment = new Payment() { Intent = "sale", Transactions = GetTransactionsList(id), RedirectUrls = new RedirectUrls() { CancelUrl = pathCancel, ReturnUrl = pathReturn }, Payer = new Payer() { PaymentMethod = "paypal" }, }; PaymentCreateRequest request = new PaymentCreateRequest(); request.RequestBody(payment); var path = CreateExcelOrder.Create(_context.Orders.Where(m => m.UserId == id).Where(m => m.IsConfirm == false).ToList(), id, _environment); try { BraintreeHttp.HttpResponse response = await client.Execute(request); var statusCode = response.StatusCode; Payment result = response.Result <Payment>(); LinkDescriptionObject approvalLink = PaypalHelpers.findApprovalLink(result.Links); var orders = _context.Orders.Include(m => m.Product).Where(m => m.UserId == id).Where(m => m.IsConfirm == false); await orders.ForEachAsync(m => m.Paymentid = result.Id); await _context.SaveChangesAsync(); return(Ok(new ConfirmModel { confirmPath = approvalLink.Href.ToString(), excelPath = path })); } catch (HttpException httpException) { var statusCode = httpException.StatusCode; var debugId = httpException.Headers.GetValues("PayPal-Debug-Id").FirstOrDefault(); return(NoContent()); } }