2016-04-25 5 views
1

Ich habe meine Website funktioniert so kann ein Benutzer über Karte bezahlen, aber jetzt muss ich auch PayPal verwenden, kann ich nicht scheinen, um die Produkte aus dem Warenkorb an die PayPal-Controller senden, jedes einzelne Produkt benötigt in der Reihenfolge gesendet werden.MVC PayPal intergration

Hier ist mein PayPal-Controller;

namespace T_shirt_Company_v3.Controllers 
{ 
    public class PayPalController : Controller 
    { 
     public ActionResult RedirectFromPaypal() 
     { 
      return View(); 
     } 
     public ActionResult CancelFromPaypal() 
     { 
      return View(); 
     } 
     public ActionResult NotifyFromPaypal() 
     { 
      return View(); 
     } 



     public ActionResult ValidateCommand(string RecordId, string CartTotal) 
     { 



      bool useSandbox = Convert.ToBoolean(ConfigurationManager.AppSettings["IsSandbox"]); 
      var paypal = new PayPal(useSandbox); 


      paypal.item_name = RecordId; 
      paypal.amount = CartTotal; 
      return View(paypal); 
     } 
    } 
} 

Und meine Ansicht zur Kasse, die ich brauche die Details aus;

@model T_shirt_Company_v3.ViewModels.ShoppingCartViewModel 

@{ 
    ViewBag.Title = "Shopping Cart"; 
} 
<script src="/Scripts/jquery-1.4.4.min.js" 
     type="text/javascript"></script> 
<script type="text/javascript"> 
    $(function() { 
     // Document.ready -> link up remove event handler 
     $(".RemoveLink").click(function() { 
      // Get the id from the link 
      var recordToDelete = $(this).attr("data-id"); 
      if (recordToDelete != '') { 
       // Perform the ajax post 
       $.post("/ShoppingCart/RemoveFromCart", {"id": recordToDelete }, 
        function (data) { 
         // Successful requests get here 
         // Update the page elements 
         if (data.ItemCount == 0) { 
          $('#row-' + data.DeleteId).fadeOut('slow'); 
         } else { 
          $('#item-count-' + data.DeleteId).text(data.ItemCount); 
         } 
         $('#cart-total').text(data.CartTotal); 
         $('#update-message').text(data.Message); 
         $('#cart-status').text('Cart (' + data.CartCount + ')'); 
        }); 
      } 
     }); 
    }); 
</script> 
<center> 
    <h3> 
     Review your cart: 
    </h3> 
    <p class="button"> 
     @using (Html.BeginForm("ValidateCommand", "PayPal")) 
      { 
       <input type="submit" name="btnConfirm" value="Check Out with Paypal" /> 
      } 


     @Html.ActionLink((string)ViewBag.CartStatus, (string)ViewBag.Link, (string)ViewBag.Link2) 

     @Html.ActionLink("Continue Shopping ", "Index", "Store") 



</p> 
<div id="update-message"> 
</div> 
<table> 
    <tr> 
     <th> 
      Product Name 
     </th> 
     <th> 
      Price (each) 
     </th> 
     <th> 
      Quantity 
     </th> 
     <th></th> 
    </tr> 
    @foreach (var item in 
       Model.CartItems) 
    { 
     <tr id="[email protected]"> 
      <td> 
       @Html.ActionLink(item.Product.Title, 
"Details", "Store", new { id = item.ProductId }, null) 
      </td> 
      <td> 
       @item.Product.Price 
      </td> 
      <td id="[email protected]"> 
       @item.Count 
      </td> 
      <td> 
       <a href="#" class="RemoveLink" 
        data-id="@item.RecordId"> 
        Remove 
        from cart 
       </a> 
      </td> 
     </tr> 
    } 
    <tr> 
     <td> 
      Total 
     </td> 
     <td></td> 
     <td></td> 
     <td id="cart-total"> 
      @Model.CartTotal 
     </td> 
    </tr> 
</table> 
</center> 

Und der Controller;

namespace T_shirt_Company_v3.Controllers 
{ 
    public class ShoppingCartController : Controller 
    { 
     TshirtStoreDB storeDB = new TshirtStoreDB(); 
     // 
     // GET: /ShoppingCart/ 
     public ActionResult Index() 
     { 

      var cart = ShoppingCart.GetCart(this.HttpContext); 

       // Set up the ViewModel 
       ShoppingCartViewModel viewModel = new ShoppingCartViewModel 
       { 
        CartItems = cart.GetCartItems(), 
        CartTotal = cart.GetTotal() 
       }; 


      if (viewModel.CartItems.Any()) 
      { 
       ViewBag.CartStatus = "Proceed to checkout or "; 
       ViewBag.Link = "AddressAndPayment"; 
       ViewBag.Link2 = "Checkout"; 

      } 
      else 
      { 
       ViewBag.CartStatus = "Cart is empty please "; 
       ViewBag.Link = "Index"; 
       ViewBag.Link2 = "Store"; 
      } 

      // Return the view 
      return View(viewModel); 
     } 


     // 
     // GET: /Store/AddToCart/5(ID) 
     public ActionResult AddToCart(int id) 
     { 
      // Retrieve the Product from the database 
      var addedProduct = storeDB.Products 
       .Single(product => product.ProductId == id); 

      // Add it to the shopping cart 
      var cart = ShoppingCart.GetCart(this.HttpContext); 

      cart.AddToCart(addedProduct); 

      // Go back to the main store page for more shopping 
      return RedirectToAction("Index"); 
     } 
     // 
     // AJAX: /ShoppingCart/RemoveFromCart/5(ID) 
     [HttpPost] 
     public ActionResult RemoveFromCart(int id) 
     { 
      // Remove the item from the cart 
      var cart = ShoppingCart.GetCart(this.HttpContext); 

      // Get the name of the product to display confirmation 
      string productName = storeDB.Carts 
       .Single(item => item.RecordId == id).Product.Title; 

      // Removes item from cart 
      int itemCount = cart.RemoveFromCart(id); 

      // Display the confirmation message saying removed from cart 
      var results = new ShoppingCartRemoveViewModel 
      { 
       Message = Server.HtmlEncode(productName) + 
        " has been removed from your shopping cart.", 
       CartTotal = cart.GetTotal(), 
       CartCount = cart.GetCount(), 
       ItemCount = itemCount, 
       DeleteId = id 
      }; 
      return Json(results); 
     } 
     // 
     // GET: /ShoppingCart/CartSummary 
     [ChildActionOnly] 
     public ActionResult CartSummary() 
     { 
      var cart = ShoppingCart.GetCart(this.HttpContext); 

      ViewData["CartCount"] = cart.GetCount(); 
      return PartialView("CartSummary"); 
     } 


     //test close connection when done 
     protected override void Dispose(bool disposing) 
     { 
      storeDB.Dispose(); 
     } 
    } 
} 

Antwort

0

Als PayPal Api auf einer Reihe von Operationen abhängen, die vor der Verwendung zu verstehen sind, sollten Entwickler darauf konzentrieren, die Operationen Zusammenfassung zu verstehen, bevor Sie die API, auch eine Sandbox zum Testen zur Verfügung, bevor Sie den Api offiziell mit .

Sie sollten wissen, wie Sie die RESTful-Endpunktstruktur von PayPal verwenden.

können Sie überprüfen die PayPal-API-Dokumentation für Details, wie Ihre Web-Anwendung mit Hilfe von PayPal zu integrieren REST API Reference.

1

mit Angenommen, ich Ihren Code richtig gelesen, Sie sind nur die Daten zu machen, Sie senden es nicht (überhaupt).

  • Ihre form hat nur button (es ist die einzige "Daten" gesendet)
  • müssen Sie die Daten, die Sie als form Felder (<input />)

HTH gemacht aufzunehmen.