public async Task<ActionResult> PaypalReturn() { string payerId = Request.Params["PayerID"]; var guid = Request.Params["guid"]; var success = Request.Params["Success"]; if (guid != null && success != null) { var isSuccess = Convert.ToBoolean(Request.QueryString["Success"]); var transactionID = Session[guid] as string; //If client buy succesful, process here if (isSuccess) { var helper = new PaypalHelper(); APIContext apiContext = helper.GetGetAPIContext(); var paymentExecution = new PaymentExecution() { payer_id = payerId }; var payment = new Payment() { id = transactionID }; //We need to execute to submit paypal about client's transaction //If we don't execute paypal, the transaction will be cancel, the buyer don't charge money //-------------------------------------------------------------- try { var executedPayment = payment.Execute(apiContext, paymentExecution); if (executedPayment.state.ToLower() != "approved") { //Save information of transaction into Database //when executedPayment is not approved //--------------------------------------------- var transactionId = Session["transactionId"]; var goEatApi = GoEatApi.Instance; // goEatApi.UpdateMainTransactionStatus(Int32.Parse(transactionId.ToString()), ConstantValues.S_CANCELLED); return View("PaypalFailure"); } else { //Save information of transaction into Database //State of transaction is approved //--------------------------------------------- var goEatApi = GoEatApi.Instance; var transactionId = Session["transactionId"]; var confirmTransactionResult = goEatApi.GetConfirmTokenTransactionById(Int32.Parse(transactionId.ToString())); if (confirmTransactionResult.Succeeded) { if (confirmTransactionResult.Succeeded) { goEatApi.FinishPaypalFinalTransaction( confirmTransactionResult.Data.paypal_payment_id, confirmTransactionResult.Data.id, confirmTransactionResult.Data.customer_id, confirmTransactionResult.Data.token_amount); var model = new PaypalSuccess() { amount = confirmTransactionResult.Data.token_amount }; var customer = goEatApi.GetUserById(confirmTransactionResult.Data.customer_id); //create api recording await goEatApi.RecordTokenTransaction(new GRecordTokenTransaction { username = customer.Data.Profile.account, order_id = confirmTransactionResult.Data.paypal_payment_id, gtoken_transaction_id = string.Empty, token_type = ConstantValues.S_SUGAR_TOKEN, transaction_type = ConstantValues.S_TRANSACTION_TYPE_CONSUMPTION, amount = confirmTransactionResult.Data.token_amount, description = ConstantValues.S_DESCRIPTION_TOPUP_TOKEN }); return View("PaypalSuccess", model); } } } } catch { return View("PaypalFailure"); } } //If client cancel, process here else { //Save information of transaction into Database //When client cancel, Use transactionID to trace //--------------------------------------------- var transactionId = Session["transactionId"]; var goEatApi = GoEatApi.Instance; //goEatApi.UpdateMainTransactionStatus(Int32.Parse(transactionId.ToString()), ConstantValues.S_CANCELLED); return RedirectToAction("buytoken"); } } //All params is not valid, return valid view here return View("PaypalFailure"); }
public ActionResult Paypal(TransactionPayPal modal) { var transactionDesc = "Transaction description."; //var invoiceNumber = "007"; //This is option. The invoiceNumber is unique which generated from our server var api = GoEat.Core.GoEatApi.Instance; var package = api.GetTokenPackageBySKU(modal.sku); if (!package.Succeeded) { return View("buytoken"); } string skuItem = package.Data.sku; string nameItem = package.Data.name; decimal priceItem = package.Data.price; string currency = package.Data.currency.Trim(); int quantityItem = 1; decimal totalAmout = priceItem * quantityItem; var helper = new PaypalHelper(); var guid = Guid.NewGuid(); var basePaypalReturn = GetPaypalReturn(guid.ToString()); var returnUrl = basePaypalReturn + "&Success=True"; var cancelUrl = basePaypalReturn + "&Success=False"; var itemList = new List<Item>(); var item = helper.CreateItem(nameItem, currency, priceItem, quantityItem, skuItem); itemList.Add(item); var amount = helper.CreateAmount(currency, totalAmout); var redirectUrls = helper.CreateRedirectUrls(returnUrl, cancelUrl); var transactionList = new List<PayPal.Api.Transaction>(); var transaction = helper.CreateTransaction(transactionDesc, amount, itemList); transactionList.Add(transaction); try { var createdPayment = helper.CreatePayment(helper.GetGetAPIContext(), transactionList, redirectUrls); var links = createdPayment.links.GetEnumerator(); string paypalRedirectUrl = null; while (links.MoveNext()) { Links lnk = links.Current; if (lnk.rel.ToLower().Trim().Equals("approval_url")) { //saving the payapalredirect URL to which user will be redirected for payment paypalRedirectUrl = lnk.href; //Save information of transaction into Database //State of transaction is pending //--------------------------------------------- var transactionId = api.CreatePaypalPendingTransaction(CreatePaypalTransactionEntity(package.Data, createdPayment), totalAmout, CurrentUser.Id, Urls.GetRestaurantId()); Session.Add("transactionId", transactionId); } } // saving the paymentID in the key guid Session.Add(guid.ToString(), createdPayment.id); return Redirect(paypalRedirectUrl); } catch (Exception ex) { throw ex; } }