Reputation: 366
Hi so i am using the php Paypal SDK and the following code works when entering a redirect url. however i am trying to set up direct card payment, does this require a redirect url as all sample code seems to leave it out?
When using redirect url object however my application just redirects me to the Paypal api sandbox and asks for a account login (this is not direct card payment)
Heres my code that returns with :
Fatal error: Uncaught exception 'PayPal\Exception\PayPalConnectionException' with message 'Got Http response code 400 when accessing https://api.sandbox.paypal.com/v1/payments/payment.' in /Applications/MAMP/htdocs/tealtique/library/paypal/rest-api-sdk-php/lib/PayPal/Core/PayPalHttpConnection.php on line 176
my code does not include names spacing because the file is a controller object and there is no problem with the name spacing.
$apiContext = $this->paypal_access_token();
$payment_description = 'Payment to comapany';
$invoice_number = uniqid();
$addr = new BaseAddress();
$addr->setLine1($_POST['adr_line1']);
$addr->setCity($_POST['adr_city']);
$addr->setCountryCode($_POST['adr_country']);
$addr->setPostalCode($_POST['adr_postal_code']);
$addr->setState($_POST['adr_county']);
$card = new CreditCard();
$card->setNumber($_POST['card_number']);
$card->setType($_POST['card_type']);
$card->setExpireMonth($_POST['card_expire_mounth']);
$card->setExpireYear($_POST['card_expire_year']);
$card->setCvv2($_POST['card_cvv2']);
$card->setFirstName($_POST['card_first_name']);
$card->setLastName($_POST['card_last_name']);
$card->setBillingAddress($addr);
$fi = new FundingInstrument();
$fi->setCreditCard($card);
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item = new Item();
$item->setName('Payment Instalment')
->setCurrency(PAYPAL_CURRENCY)
->setQuantity(1)
->setPrice($_POST['payment_amount']);
$itemList = new ItemList();
$itemList->setItems(array($item));
$details = new Details();
$details->setShipping(0)
->setFee(PAYPAL_FEE)
->setTax(0)
->setSubtotal($_POST['payment_amount']);
$amount = new Amount();
$amount->setCurrency(PAYPAL_CURRENCY)
->setTotal($_POST['payment_amount'])
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription($payment_description)
->setInvoiceNumber($invoice_number);
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setTransactions(array($transaction));
try {
$payment->create($apiContext);
} catch (Exception $ex) {
echo 'exception : <pre>';print_r(json_decode($ex->getData()));
exit(1);
}
//$approvalUrl = $payment->getApprovalLink();
//header('location:' . $approvalUrl);
I am also a bit confused on how to capture error message using the sdk as the documentation on the Paypal developer website just states what errors will retrun and what they mean as opposed how to capture them.
Upvotes: 0
Views: 724
Reputation: 1489
First things first :
You can catch the PayPalHttpConnection Exception and print the detailed message, on why it is failing. To do so, add a try catch block around your code.
try {
$creditCard->create($apiContext);
echo $creditCard;
}
catch (\PayPal\Exception\PayPalConnectionException $ex) {
echo $ex->getData();
}
I will add this to one of the wiki pages at https://github.com/paypal/PayPal-PHP-SDK/wiki
Secondly, if you are planning to do direct payment using credit card, you do not need to have a redirect URL. You can follow the sample Payments using credit card information
provide at https://paypal-php-sdk.herokuapp.com/
Also, you could easily setup samples in your local machine by one command, as it is already shipped with the SDK. Just follow the instructions given here
Upvotes: 1