-1

I have a PayPal smart button on my checkout page. I want to log the transaction after the payment is approved. The alert in "onApprove" works as it is suppose to. It only pops up when the payment is approved. However, the C# card is running as if it does not belong to the JS code. Here is the PayPal code tutorial.

<script>
    paypal.Buttons({
        createOrder: function (data, actions) {
            // This function sets up the details of the transaction, including the amount and line item details.
            return actions.order.create({
                purchase_units: [{
                    amount: {
                        value: '@Model.Price'
                    }
                }]
            });
        },
        onApprove: function (data, actions) {
            // This function captures the funds from the transaction.
            return actions.order.capture().then(function (details) {
                // This function shows a transaction success message to your buyer.
                alert('Transaction completed by ' + details.payer.name.given_name);
                @{
                    var userId = UserManager.GetUserId(User);

                    var transaction = new Transaction
                    {
                        Price = Model.Price,
                        BuyerId = userId,
                        BuyerName = User.Identity.Name,
                        SellerId = Model.AuthorId,
                        SellerName = Model.AuthorName,
                        TimeOccured = DateTime.Now,
                        ForumId = Model.ForumId,
                        ForumName = Model.ForumName
                    };

                    await _postService.AddTransaction(transaction);
                }
            });
        },
         onCancel: function (data) {
            // Show a cancel page, or return to cart
            alert('You have canceled the order');
          }

    }).render('#paypal-button-container');
      //This function displays Smart Payment Buttons on your web page.
</script>

1 Answers1

1

The C# code indeed does not belong to the JavaScript code. It runs server-side before the HTML page (which includes the JavaScript) is returned to the web browser.

Your onApprove JavaScript event should perform a call (e.g. Ajax call) back to the server. Your C# code block should run in that callback.

Eric J.
  • 147,927
  • 63
  • 340
  • 553