2016-04-25 7 views
1

私は自分のサイトを運営しているので、ユーザーはカードで支払うことができますが、今でもPayPalを使用する必要があります。ショッピングカートからPayPalコントローラに商品を送ることはできません。順番に送信されます。MVC PayPal intergration

ここは私のPayPalコントローラです。

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); 
     } 
    } 
} 

私が詳細を必要とするチェックアウトのビュー。

@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> 

コントローラです。

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(); 
     } 
    } 
} 

答えて

0

ペイパルAPIは以前使用理解されなければならない操作のセットに依存しているように、開発者は、APIを使用する前に、操作の概要を理解することに焦点を当てる必要があり、また、サンドボックスは正式にAPIを使用する前に、テストのために利用可能です。

PayPalのRESTfulエンドポイント構造を使用する方法を理解しておく必要があります。あなたは私が正しくあなたのコードを読むと仮定REST API Reference.

1

を使用してPayPalでWebアプリケーションを統合する方法の詳細については、PayPalのAPIドキュメントを確認することができます

、あなただけ(あなたがそれを送信していない、データをレンダリングしていますまったく)。

  • あなたformだけあなたがformフィールド(<input />

第Hとしてレンダリングされたデータを含める必要がbutton(それが送られただけで、 "データ" です)

  • を持っています。

  • 関連する問題