2015-06-09 105 views
0

我需要一些幫助來找出paypal的API,我所要做的就是對我的沙箱帳戶運行一次cc收費,我一直在查看他們的文檔和代碼示例,但我只是可以現在找不到正確的信息,比如在哪裏添加沙箱端點。所以,當我運行了以下功能我得到以下錯誤:Paypal CC直接付款錯誤

string(213) "{"name":"MALFORMED_REQUEST","message":"Incoming JSON request does not map to API request","information_link":" https://developer.paypal.com/webapps/developer/docs/api/#MALFORMED_REQUEST ","debug_id":"6eb15180f8d92"}"

這是我打電話的功能:

public static function ccPayment($paymentInfo, $credentials) 
{ 

$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
     $credentials['clientId'], 
     $credentials['secret'] 
    ) 
); 

$card = new CreditCard(); 
$card->setType("visa") 
    ->setNumber("xxxxxxxxxxxxxxxx") 
    ->setExpireMonth("11") 
    ->setExpireYear("2019") 
    ->setCvv2("012") 
    ->setFirstName("Joe") 
    ->setLastName("Shopper"); 

$fi = new FundingInstrument(); 
$fi->setCreditCard($card); 


$payer = new Payer(); 
$payer->setPaymentMethod("credit_card") 
     ->setFundingInstruments(array($fi)); 

$item1 = new Item(); 
$item1->setName('Ground Coffee 40 oz') 
    ->setDescription('Ground Coffee 40 oz') 
    ->setCurrency('USD') 
    ->setQuantity(1) 
    ->setTax(0.3) 
    ->setPrice(7.50); 

$itemList = new ItemList(); 
$itemList->setItems(array($item1)); 

$transaction = new Transaction(); 
$transaction->setAmount(489) 
    ->setItemList($itemList) 
    ->setDescription("Payment description") 
    ->setInvoiceNumber(uniqid()); 

$payment = new Payment(); 
$payment->setIntent("sale") 
    ->setPayer($payer) 
    ->setTransactions(array($transaction)); 

// $request = clone $payment; 

try 
{ 
    $payment->create($apiContext); 
} 
catch (Exception $ex) 
{ 
    dd($ex->getData()); 
} 

dd($paymentInfo); 

} 

去過編碼從早晨到現在,所以我需要另一套眼睛權現在因爲我看不到我做錯了什麼。我會感謝任何幫助。

PS。道歉,如果它是愚蠢的。例如。一個缺少的逗號。

謝謝。

+0

你可以發佈完整的響應(帶調試ID) – Vimalnath

+0

「debug_id」:「6eb15180f8d92」 –

回答

0

明白了吧!這裏的工作代碼:

$apiContext = new \PayPal\Rest\ApiContext(
    new \PayPal\Auth\OAuthTokenCredential(
     $credentials['clientId'], 
     $credentials['secret'] 
    ) 
); 

$apiContext->setConfig(
    array(
    'log.LogEnabled' => true, 
    'log.FileName' => '../app/storage/paypal/PayPal.log', 
    'log.LogLevel' => 'FINE' 
) 
); 

$card = new CreditCard(); 
$card->setType("visa") 
    ->setNumber("xxxxxxxxxxxxxxxxx") 
    ->setExpireMonth("11") 
    ->setExpireYear("2019") 
    ->setCvv2("012") 
    ->setFirstName("Joe") 
    ->setLastName("Shopper"); 

$fi = new FundingInstrument(); 
$fi->setCreditCard($card); 

$payer = new Payer(); 
$payer->setPaymentMethod("credit_card") 
    ->setFundingInstruments(array($fi)); 

$item1 = new Item(); 
$item1->setName('Ground Coffee 40 oz') 
    ->setDescription('Ground Coffee 40 oz') 
    ->setCurrency('USD') 
    ->setQuantity(1) 
    ->setPrice(49.99); 

$itemList = new ItemList(); 
$itemList->setItems(array($item1)); 

$details = new Details(); 
$details->setSubtotal(49.99); 

$amount = new Amount(); 
$amount->setCurrency("USD") 
    ->setTotal(49.99) 
    ->setDetails($details); 

$transaction = new Transaction(); 
$transaction->setAmount($amount) 
    ->setItemList($itemList) 
    ->setDescription("Payment description") 
    ->setInvoiceNumber(uniqid()); 

$payment = new Payment(); 
$payment->setIntent("sale") 
    ->setPayer($payer) 
    ->setTransactions(array($transaction)); 

try 
{ 
    $payment->create($apiContext); 
} 
catch (Exception $ex) 
{ 
    dd($ex->getData()); 
} 

dd($payment); 

我錯過了新的金額&新的細節。此外,爲了文件的目的,您的貨幣總數必須加起來。例如。稅,小計,總額。