Paypal CC Direct Payment error

115 views Asked by At

I need some help to figure out paypal's API, all I'm trying to do is run a cc charge to my sandbox account, I've been looking at their documentation and code samples but I just can't find the right information right now, like where to add the sandbox endpoint. So, when I run the function below I get the following error:

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"}"

This is the function I'm calling:

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

}

Been coding since the morning so I need another set of eyes right now because I just can't see what I'm doing wrong. I will appreciate any help.

PS. Apologies if it's something dumb. Eg. a missing comma.

Thanks.

1

There are 1 answers

0
angelcool.net On BEST ANSWER

Got it working !! Here's the working code:

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

I was missing new Amount & new Details. Also, for the sake of documentation, your monetary totals must add up. Eg. tax, subtotal, total.