private Payment Payment_PayPal(TransactionProcessParamsItems requestParams) { var result = _TransactionsService.GetTransactionById(requestParams.TransactionProcess.ID.ToString()); var payment = new Payment(); if (result == null) { return(payment); } payment = _PaypalServices.CreatePayment(requestParams); result.Status = payment.state; result.ReferenceId = payment.id; _TransactionsService.UpdateTransactions(result); var PaymentLogs = new PaymentLogs() { Data = Newtonsoft.Json.JsonConvert.SerializeObject(payment), Error = "Success", PaymentType = "PAYPAL", FunctionName = "ProcessPayment", TransID = requestParams.TransactionProcess.ID.ToString() }; _PaymentLogsService.InsertPaymentLogs(PaymentLogs); return(payment); }
public Payment CreatePayment(TransactionProcessParamsItems trans, string intent = "sale") { var conf = new Dictionary <string, string>() { { BaseConstants.ClientId, _options.PayPalClientId }, { BaseConstants.ClientSecret, _options.PayPalClientSecret } }; var apiContext = new APIContext(new OAuthTokenCredential(_options.PayPalClientId, _options.PayPalClientSecret, conf).GetAccessToken()); var payment = new Payment() { intent = intent, payer = new Payer() { payment_method = "paypal" }, transactions = GetTransactionsList(trans), redirect_urls = new RedirectUrls() { cancel_url = !string.IsNullOrEmpty(trans.TransactionProcess.CancelUrl) ? trans.TransactionProcess.CancelUrl : "https://www.paypal.com/us/smarthelp/article/can-i-cancel-a-paypal-payment-faq637", return_url = trans.TransactionProcess.BackUrl } }; var createdPayment = payment.Create(apiContext); return(createdPayment); }
private List <Transaction> GetTransactionsList(TransactionProcessParamsItems Trans) { var transactionList = new List <Transaction>(); var currency = Trans.TransactionProcess.Currency; var itemsList = new List <Item>(); foreach (var item in Trans.ItemsPayment) { itemsList.Add(new Item() { name = item.ItemName, currency = currency, price = item.Amount.ToString(), quantity = "1", sku = "sku" }); } transactionList.Add(new Transaction() { description = Trans.TransactionProcess.Description, invoice_number = Trans.TransactionProcess.OrderId, amount = new Amount() { currency = currency, total = Trans.TransactionProcess.OrderAmount.ToString(), details = new Details() { tax = "0", shipping = "0", subtotal = Trans.TransactionProcess.OrderAmount.ToString() } }, item_list = new ItemList() { items = itemsList } //payee = new Payee //{ // // TODO.. Enter the payee email address here // email = !string.IsNullOrEmpty(Trans.TransactionProcess.EmailPayment) ? Trans.TransactionProcess.EmailPayment : _options.PayPalEmail, // //email = "*****@*****.**", // // TODO.. Enter the merchant id here // merchant_id = "" //} }); return(transactionList); }
public IActionResult ProcessPayment([FromBody] TransactionProcessParamsItems request, string PaymentType) { var requestParams = request.TransactionProcess; Transactions model = new Transactions(); model.BackUrl = requestParams.BackUrl; model.Currency = requestParams.Currency; model.Description = requestParams.Description; model.ID = requestParams.ID; model.OrderAmount = requestParams.OrderAmount; model.OrderId = requestParams.OrderId; model.PaymentMethod = requestParams.PaymentMethod; model.PaymentCardToken = requestParams.PaymentCardToken; APIResponseData responseData = new APIResponseData(); responseData.StatusCode = 0; responseData.Message = "Failed."; if (model == null) { responseData.StatusCode = 2; responseData.Message = "Value is required."; goto skipToReturn; } try { var trans = _TransactionsService.GetTransactionById(model.ID.ToString()); if (trans == null) { responseData.Message = "Notfound."; responseData.StatusCode = 2; goto skipToReturn; } #region PAYPAL if (PaymentType.ToUpper() == "PAYPAL") // Paypal { var paypal = Payment_PayPal(request); trans.ModifiedDate = DateTime.Now; trans.PaymentTypeAPI = PaymentType.ToUpper(); _TransactionsService.UpdateTransactions(trans); responseData.Message = "Successfully."; responseData.StatusCode = 1; responseData.Result.Data = new { TransactionId = model.ID, PaymenID = paypal.id, PaymentStatus = paypal.state, FailureMessage = paypal.failed_transactions }; return(Ok(responseData)); //return new JsonResult(paypal); } #endregion #region Stripe else { Stripe.Charge charge = null; //var amount = charge.Amount.ToString(); decimal payAmount = 0; if (trans.PaymentMethod == Common.DTO.PaymentMethod.Full.ToUpper()) { payAmount = model.OrderAmount.Value; charge = StripeHelpers.RequestCharge(model, payAmount, model.Description); trans.Status = charge.Status; trans.ReferenceId = charge.Id; } else { var transactionItems = _TransactionsService.GetTransactionItems(trans.ID).ToList() .Where(x => x.Status == null).OrderBy(x => x.OrderNo).ToList(); var nextPayItem = transactionItems.FirstOrDefault(); payAmount = nextPayItem.PayAmount.Value; string description = string.Format("Charge {0}% of OrderId: {1}. {2}", nextPayItem.PayPercent, model.OrderId, model.Description); charge = StripeHelpers.RequestCharge(model, payAmount, description); nextPayItem.Status = charge.Status; nextPayItem.ReferenceId = charge.Id; _TransactionsService.UpdateTransactionItem(nextPayItem); trans.Status = PaymentStatus.Processing; } trans.ModifiedDate = DateTime.Now; trans.PaymentCardToken = model.PaymentCardToken; trans.StripeCustomerId = charge.CustomerId; trans.OrderAmountRemaining = trans.OrderAmountRemaining.Value - payAmount; trans.PaymentTypeAPI = PaymentType.ToUpper(); _TransactionsService.UpdateTransactions(trans); responseData.Message = "Successfully."; responseData.StatusCode = 1; responseData.Result.Data = new { TransactionId = model.ID, PaymenID = charge.Id, PaymentStatus = charge.Status, FailureMessage = charge.FailureMessage }; var PaymentLogs = new PaymentLogs() { Data = Newtonsoft.Json.JsonConvert.SerializeObject(charge), Error = "Success", PaymentType = PaymentType, FunctionName = "ProcessPayment", TransID = requestParams.ID.ToString() }; _PaymentLogsService.InsertPaymentLogs(PaymentLogs); } #endregion } catch (Exception ex) { responseData.Message = "Something went wrong, please try again."; responseData.StatusCode = 0; var typeEx = ex.GetType(); if (typeEx.FullName == "PayPal.PayPalException") { var excep = ex as PayPal.PaymentsException; StringBuilder sb = new StringBuilder(); sb.AppendLine("Error: " + excep.Details.name); sb.AppendLine("Message: " + excep.Details.message); sb.AppendLine("URI: " + excep.Details.information_link); sb.AppendLine("Debug ID: " + excep.Details.debug_id); foreach (var errorDetails in excep.Details.details) { sb.AppendLine("Details: " + errorDetails.field + " -> " + errorDetails.issue); } var PaymentLogs = new PaymentLogs() { Data = Newtonsoft.Json.JsonConvert.SerializeObject(request), Error = sb.ToString(), PaymentType = PaymentType, FunctionName = "ProcessPayment", TransID = requestParams.ID.ToString() }; _PaymentLogsService.InsertPaymentLogs(PaymentLogs); } else { var PaymentLogs = new PaymentLogs() { Data = Newtonsoft.Json.JsonConvert.SerializeObject(request), Error = ex.InnerException != null?Newtonsoft.Json.JsonConvert.SerializeObject(ex.InnerException.Message) : Newtonsoft.Json.JsonConvert.SerializeObject(ex.Message), PaymentType = PaymentType, FunctionName = "ProcessPayment", TransID = requestParams.ID.ToString() }; _PaymentLogsService.InsertPaymentLogs(PaymentLogs); } EmailHelpers.SendEmail(new Common.DTO.ErrorInfo() { Section = $"AQ ProcessPayment PaymentType: {PaymentType} <br /> TransactionID : {requestParams.ID}", Exception = ex }); } skipToReturn: //label use to force return return(Ok(responseData)); }