WinDev
WinDev

Reputation: 37

Asp MVC PayPal Transaction

I am trying to make a paypal handler. You type a money amount into an input, than you press donate button, you will be forwarded to the paypal page, you can login and than press continue button... The problem after I press the continue button its forward me back to the https://www.example.org/Account/PayPalHandler/ page, and thats it all. What is missing from my code to complete the paypal transaction?

[HttpPost]
        public ActionResult DoPaymentPaypall(UserModel User_)
        {
            ResultModel<ManageAccountListModel> res_ = new ResultModel<ManageAccountListModel>();
            res_.DataSelect = new ManageAccountListModel();

            if (SessionManagement.LoginnedUser != null)
            {
                var config = ConfigManager.Instance.GetProperties();
                var accessToken = new OAuthTokenCredential(config).GetAccessToken();
                var apiContext = new APIContext(accessToken);
                string moneyCount_ = User_.moneycount.ToString();
                var payment = Payment.Create(apiContext, new Payment
                {
                    intent = "sale",
                    payer = new Payer
                    {
                        payment_method = "paypal"
                    },
                    transactions = new List<Transaction>
                                    {
                                        new Transaction
                                        {
                                            description = "Donation",
                                            invoice_number = "001",
                                            amount = new Amount
                                            {
                                                currency = "USD",
                                                total = moneyCount_,
                                                details = new Details
                                                {
                                                    tax = "0",
                                                    shipping = "0",
                                                    subtotal = moneyCount_
                                                }
                                            },
                                            item_list = new ItemList
                                            {
                                                items = new List<Item>
                                                {
                                                    new Item
                                                    {
                                                        name = "Donation",
                                                        currency = "USD",
                                                        price = moneyCount_,
                                                        quantity = "1",
                                                        sku = "Custom Package"
                                                    }
                                                }
                                            }
                                        }
                                    },
                    redirect_urls = new RedirectUrls
                    {
                        return_url = "https://www.example.org/Account/PayPalHandler/",
                        cancel_url = "https://www.example.org/"
                    }
                });

                res_.SuccessMessage = payment.links.ToList()[1].href;

            }
            res_.Success = true;
            return new JsonResult { Data = res_, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }

Upvotes: 0

Views: 266

Answers (1)

EdSF
EdSF

Reputation: 12341

Your code above seems to only show the initial/start of the process...so assuming you mean after the user has approved your request to use Paypal for payment (don't confuse this as "the payment" - this step only indicates that the user as agreed to use Paypal for payment) you'll need to Execute the payment.

The link shows the full flow

  • Create payment (this is what your code above maps to) - you'll get a payment id in this step
  • redirect user to Paypal approval_url for approval (of the details in the Create above) using the id you recieved
  • Paypal sends user back to your site with info on how to Execute payment (if user approves)
  • Execute payment (this is done at your site/app)

Hth

Upvotes: 1

Related Questions