2017-04-17 7 views
0

におけるマルチ項目の設定こんにちは、私はペイパルPHP SDKの初心者です、私は本当に私はjQueryのを使用のPayPalのPHP SDK

、私はPayPalのPHPのRESTのSDKに複数のアイテムの製品名、数量&価格を取得する方法に出て助けを必要とカートにマルチアイテムを追加すると、以下の私のソースコード である第一これは

<div class="product clearfix" style="padding:8px;"> 
<div class="card-block"> 
    <div class="product-image"> 
     <a href="product.php?id={$row['product_id']}"><img src="../resources/{$product_image}" alt="{$row['product_title']}" class="selected"></a> 
     <a href="product.php?id={$row['product_id']}"><img src="../resources/{$product_image2}" alt="{$row['product_title']}"></a> 
    </div> 
    <div class="product-desc center"> 
     <a href="product.php?id={$row['product_id']}"> 
     <div class="product-title"><h3 style="font-size:15px;">{$row['product_title']}</h3></div> 
     </a> 
     <a href="include/ajax/add_cart_message.php?id={$row['product_id']}" id="btn1" class="productItem button button-rounded button-reveal button-medium button-border tright" data-lightbox="ajax" data-name="{$row['product_title']}" data-s="{$row['quantity_diff']}" data-price="{$row['product_price']}" data-id="{$row['product_id']}01" data-itemcode="{$row['item_number']}">&#36;{$row['product_price']} <i class="icon-shopping-cart"></i>&nbsp; &nbsp; <span id="variab1">{$row['quantity_diff']}</span></a> 
     <a href="include/ajax/add_cart_message.php?id={$row['product_id']}" id="btn2" class="productItem button button-rounded button-reveal button-medium button-border tright" data-lightbox="ajax" data-name="{$row['product_title']}" data-s="{$row['quantity_diff2']}" data-price="{$row['product_price2']}" data-id="{$row['product_id']}02" data-itemcode="{$row['item_number']}">&#36;{$row['product_price2']} <i class="icon-shopping-cart"></i>&nbsp; &nbsp; <span id="variab2">{$row['quantity_diff2']}</span></a> 
     <a href="include/ajax/add_cart_message.php?id={$row['product_id']}" id="btn3" class="productItem button button-rounded button-reveal button-medium button-border tright" data-lightbox="ajax" data-name="{$row['product_title']}" data-s="{$row['quantity_diff3']}" data-price="{$row['product_price3']}" data-id="{$row['product_id']}03" data-itemcode="{$row['item_number']}">&#36;{$row['product_price3']} <i class="icon-shopping-cart"></i>&nbsp; &nbsp; <span id="variab3">{$row['quantity_diff3']}</span></a> 

     </div> 
    </div> 
</div> 
</div> 

はjQueryのにデータを送信しますクリックした各ボタンは、それを収集し、出力がmodalにfunction.phpで私のコードでHTMLのような

<!-- modals --> 
<div class="modal fade" id="cart" tabindex="-1"> 
    <div class="modal-dialog"> 
     <div class="modal-content"> 
      <div class="modal-header"><button type="button" class="close" data-dismiss="modal">&times;</button><h4 class="modal-title">Shopping Cart</h4></div> 
      <div class="modal-body" style="padding:0px;"> 
       <form action="order_process.php" method="post"> 
       <table class="table table-hover"> 
        <thead style="background:#1ABC9C; color:white;"> 
         <tr> 
          <th>Item Image</th> 
          <th>Item Name</th> 
          <th>Cost</th> 
          <th>Qty</th> 
          <th style="text-align:right;">Subtotal</th> 
          <th style="text-align:center;">Remove</th> 
         </tr> 
        </thead> 
        <tbody id="output"> 

        </tbody> 
       </table> 

      </div> 
      <div class="modal-footer"><input type="submit" class="btn btn-primary btn-block" value="Checkout with PayPal"><button type="button" class="btn btn-warning" data-dismiss="modal">Continue Shopping</button></div> 
     </div> 
    </div> 
</div> 
0123以下

と以下のjQueryのスクリプト出力がorder_process.phpに

</script> 
    <script> 
     var shopcart =[]; 
     jQuery(document).ready(function($){ 
      outputCart(); 

      $('#output').on('click','.remove-item',function(){ 
       var itemToDelete = $('.remove-item').index(this); 
       shopcart.splice(itemToDelete,1); 
       sessionStorage['sca'] = JSON.stringify(shopcart); 
       outputCart(); 
      }) 

      $('.productItem').click(function (e){ 
       e.preventDefault(); 
       var imgpath = $(this).closest('.product').find('img' + '.selected').attr('src'); 
       if (!imgpath) { 
        imgpath = 'default.jpg'; 
       } 
       console.log(imgpath); 
       var iteminfo = $(this.dataset)[0]; 
       iteminfo.qty = 1; 
       var itemincart = false; 
       $.each(shopcart, function (index, value) { 
        ///console.log(index + ' ' + value.id); 
        if(value.id == iteminfo.id) { 
         iteminfo.imgpath = imgpath; 
         value.qty = parseInt(value.qty) + parseInt(iteminfo.qty); 
         itemincart = true; 
        } 
       }) 
       if(!itemincart){ 
        iteminfo.imgpath = imgpath; 
        shopcart.push(iteminfo); 
       } 
       console.log(shopcart); 
       sessionStorage["sca"] = JSON.stringify(shopcart); 
       outputCart(); 
       /// 
      }) 


      function outputCart() { 
       if (sessionStorage["sca"] != null) { 
        shopcart = JSON.parse(sessionStorage["sca"].toString()); 
        $('#checkoutdiv').show(); 
       } 
       var holderHTML = ''; 
       var total = 0; 
       var itemCnt = 0; 
       $.each(shopcart, function (index, value) { 
        var stotal = value.qty * value.price; 
        total += stotal; 
        itemCnt += parseInt(value.qty); 
        holderHTML += '<tr><td><a href=product.php?id=' + formatId(value.id) + '><img src="' + value.imgpath + '" width="100px"></a></td><td><input type="hidden" name="item_name_' + a + '" value="' + value.name + ' ' + value.s + '"><a href=product.php?id=' + formatId(value.id) + '>' + value.name + '(' + value.s + ')<br>#' + value.itemcode + '</a></td><td class="center"><input type="hidden" name="amount_' + a + '" value="' + formatMoney(value.price) + '"> ' + formatMoney(value.price) + ' </td><td><input size="5" type="number" class="dynquantity" name="quantity_' + a + '" value="' + value.qty + '" data-id="' + value.id + '"></td><td class="center"> ' + formatMoney(stotal) + '</td><td class="center"><span class="btn btn-danger remove-item">x</span></td></tr>'; 

       }) 

       holderHTML += '<tr><td colspan="4" style="text-align:right;">Total: </td><td style="text-align:right;">' + formatMoney(total) +'</td><td>'; 
       $('#output').html(holderHTML); 
       $('.total').html(formatMoney(total)); 
       $('.items').html(itemCnt); 

      } 
      function formatMoney(m){ 
       return '$' + (m); 
      } 
      function formatId(n){ 
       return (n/100).toFixed(0); 
      } 
     }) 
    </script> 

を#outputするためのデータで、どのように私はPayPalのPHPのSDKにこれらの件のデータを取得するのですか?私は一生懸命やっています。助けてください。

<?php 

use PayPal\Api\Payer; 
use PayPal\Api\Item; 
use PayPal\Api\ItemList; 
use PayPal\Api\Details; 
use PayPal\Api\Amount; 
use PayPal\Api\Transaction; 
use PayPal\Api\RedirectUrls; 
use PayPal\Api\Payment; 

require 'app/startpp.php'; 

if (!isset($_POST['item_name_[]'], $_POST['amount_[]'], $_POST['quantity_[]'])) { 

} 

$product = $_POST['item_name_[]']; 
$price = $_POST['amount_[]']; 
$quantity = $_POST['quantity_[]']; 

$total = $price * $quantity; 

$payer = new Payer(); 
$payer->setPaymentMethod('paypal'); 

$item1 = new Item(); 
$item1->setName($product) 
    ->setCurrency('CAD') 
    ->setQuantity($quantity) 
    ->setPrice($price); 

$item2 = new Item(); 
$item2->setName($product) 
    ->setCurrency('CAD') 
    ->setQuantity($quantity) 
    ->setPrice($price); 

$item3 = new Item(); 
$item3->setName($product) 
    ->setCurrency('CAD') 
    ->setQuantity($quantity) 
    ->setPrice($price); 

$item4 = new Item(); 
$item4->setName($product) 
    ->setCurrency('CAD') 
    ->setQuantity($quantity) 
    ->setPrice($price); 

$item5 = new Item(); 
$item5->setName($product) 
    ->setCurrency('CAD') 
    ->setQuantity($quantity) 
    ->setPrice($price); 

$item6 = new Item(); 
$item6->setName($product) 
    ->setCurrency('CAD') 
    ->setQuantity($quantity) 
    ->setPrice($price); 

$item7 = new Item(); 
$item7->setName($product) 
    ->setCurrency('CAD') 
    ->setQuantity($quantity) 
    ->setPrice($price); 

$itemList = new ItemList(); 
$itemList->setItems([$item1, $item2, $item3, $item4, $item5, $item6, $item7]); 


$amount = new Amount(); 
$amount->setCurrency('CAD') 
    ->setTotal($total) 
    ->setDetail($detail); 

$transaction = new Transaction(); 
$transaction->setAmount($amount) 
    ->setItmeList($itemList) 
    ->setDescription('PayForSomething Payment') 
    ->setInvoiceNumber(uniqid()); 

$redirectUrls = new RedirectUrls(); 
$redirectUrls->setReturnUrl(SITE_URL . '/pay.php?success=true') 
    ->setCancelUrl(SITE_URL . '/pay.php?success=false'); 

$payment = new Payment(); 
$payment->setIntent('sale') 
    ->setPayer($payer) 
    ->setRedirectUrls($redirectUrls) 
    ->setTransactions([$transaction]); 

try { 
    $payment->create($paypal); 
} catch (Exception $e) { 
    die($e); 
} 

echo $approvalUrl = $payment->getApprovalLink(); 

答えて

0

2つのものがあります。 setItemList

$transaction = new Transaction(); 
$transaction->setAmount($amount) 
    ->setItmeList($itemList) 
    ->setDescription('PayForSomething Payment') 
    ->setInvoiceNumber(uniqid()); 

変更setItmeList:まず、あなたはあなたのコードのタイプミスを持っています。

これを修正した後、クライアント側では、item_name[]のように、フィールド名に[]を追加します。サーバー側では、$item_names = $_POST['item_name']ですべての項目名を読み取ることができます。サーバー側の識別子に角括弧は必要ありません。つまり、$_POST['item_name_[]']と書いてありますが、間違っています。次に、配列を受け取ったことを確認してから、Paypalコードを実行します。

+0

ありがとう、私はそれを修正しましたが、私のブラウザはまだ表示されますNotice:未定義インデックス:item_name []、amount []、等.... –

+0

あなたの質問を編集し、エラーメッセージが表示されていない間は、コードをすべて掘り下げて調べる必要があるため、間違っているのは非常に難しいです。私は自分の答えを広げた。 – Pharaoh

関連する問題