Hi am setting up an automatic billing agreement on PayPal
The code works:
$apiContext = new \PayPal\Rest\ApiContext(new \PayPal\Auth\OAuthTokenCredential($this->CLIENT_ID, $this->CLIENT_SECRET));
$agreement = new Agreement();
$agreement->setName('Agreement')
->setDescription('Base Plan')
->setStartDate('2015-06-01T12:00:00Z');
$plan = new Plan();
$plan->setId($planID);
$agreement->setPlan($plan);
// Add Payer
$payer = new Payer();
$payer->setPaymentMethod('credit_card')->setPayerInfo(new PayerInfo(array(
'email' => $email
)));
// Add Credit Card to Funding Instruments
$creditCard = new CreditCard();
$creditCard->setType($cctype)
->setNumber($ccnumber)
->setExpireMonth($ccexpirymonth)
->setExpireYear($ccexpiryyear)
->setCvv2($ccv);
$fundingInstrument = new FundingInstrument();
$fundingInstrument->setCreditCard($creditCard);
$payer->setFundingInstruments(array(
$fundingInstrument
));
// Add Payer to Agreement
$agreement->setPayer($payer);
$shippingAddress = new ShippingAddress();
$shippingAddress->setLine1('111 First Street')
->setCity('Saratoga')
->setState('CA')
->setPostalCode('95070')
->setCountryCode('US');
$agreement->setShippingAddress($shippingAddress);
try {
$createagreement = $agreement->create($apiContext);
} catch (Exception $ex) {
exit(1);
}
What I am trying to get is the credit card ID that PayPal should have so that some information about the credit card can be used later for updating the credit card at a later time etc.
I thought:
$creditCard->getId();
would work, but it does not return anything,
How can I get the Credit Card ID?
Related
I'm using Paypal's PHP SDK, but I'm having trouble with executing payments (400, error code: PAYER_CANNOT_PAY ).
Sandbox setup:
Created a business sandbox account
Created a developer account
Created an App
Created sandbox buyer accounts with balance and linked credit card
Code:
Create a payment with Paypal SDK
$cred = new OAuthTokenCredential("$clientID", "$secret", $sdkConfig);
$cred->accessToken = $cred->getAccessToken($sdkConfig);
$apiContext = new ApiContext($cred);
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setSku("123123") // Similar to `item_number` in Classic API
->setPrice(7.5);
$item2 = new Item();
$item2->setName('Granola bars')
->setCurrency('USD')
->setQuantity(5)
->setSku("321321")
->setPrice(2);
$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));
$details = new Details();
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(17.50);
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(20)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("http://test.dev/paypal?success=true")
->setCancelUrl("http://test.dev/paypal?success=false");
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
$request = clone $payment;
$payment->create($apiContext);
App redirects user to Paypal sandbox login page
Login with sandbox account (Pay with section is empty)
User is redirected back with token, paymentID, payerID and status(success)
Execute the payment <-- response from API 400, PAYER_CANNOT_PAY
$paymentId = $request->all()['paymentId'];
$payment = Payment::get($paymentId, $apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($request->all()['PayerID']);
$payment->execute($execution, $apiContext);
Error:
{
"status": 400,
"duration_time": 156,
"body": {
"message": "The combination of payer and payee settings mean that this buyer can't pay this seller.",
"debug_id": "debugid",
"details": [],
"name": "PAYER_CANNOT_PAY",
"information_link": "https://developer.paypal.com/docs/api/payments/#errors"
},
}
Any idea why this is failing?
Thank you in advance!
Perhaps payment is declined by buyer's card bank. There is no integration issue. Try by another account to confirm your integration.
Are you sure you aren't trying to login and pay with the same developer account that is acting as the seller? You need to make sure you're using separate sandbox accounts for buyer and seller.
I'm a slight bit confused about the Paypal API. I'm using Paypal Payments to have a customer pay on the spot with the PHP SDK.
$apiContext = new ApiContext(
new OAuthTokenCredential(
$this->paypalid,
$this->paypalsecret
)
);
$payer = $_GET['payerId'];
$paymentid = $_GET['paymentId'];
$payment = Payment::get($paymentid, $apiContext);
$transaction = $payment->getTransactions();
$execution = new PaymentExecution();
$execution->setPayerId($payer)
->setTransactions($transaction);
try{
$result = $payment->execute($execution, $apiContext);
} catch (Exception $ex) {
echo "Couldn't process payment";
exit;
}
I would like to generate an invoice based on the payment done for tax reasons. Currently all this does is send a notification to both the seller/buyer concerning the transaction being successful. Paypal's API seems to heavily imply that invoices are used to process payments, which in this case, the payment is already done; I don't want the customer to pay twice.
I imagine that I need to set the status property on an Invoice object to "PAID" and set the amount paid equal to the total amount present on the payment, and then use the send method along with the apiContext.
Am I wrong?
What is the correct way of passing in multiple products into a php paypal payment?
Currect working testcode for single product:
$api = new ApiContext(
new OAuthTokenCredential(
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
)
);
$api->setConfig([
'mode' => 'sandbox',
'log.LogEnabled' => false,
'log.FileName' => '',
'log.LogLevel' => 'FINE',
'validation.level' => 'log'
]);
$payer = new Payer();
$details = new Details();
$amount = new Amount();
$transaction = new Transaction();
$payment = new Payment();
$redirecturls = new RedirectUrls();
$payer->setPaymentMethod('paypal');
$details->setShipping('2.00')
->setTax('0.00')
->setSubtotal('20.00');
$amount->setCurrency('EUR')
->setTotal('22.00')
->setDetails($details);
$transaction->setAmount($amount)
->setDescription('Test');
$payment->setIntent('sale')
->setPayer($payer)
->setTransactions([$transaction]);
$redirecturls->setReturnUrl('RETURN URL')
->setCancelUrl('CANCEL URL');
$payment->setRedirectUrls($redirecturls);
try {
$payment->create($api);
} catch (PPConnectionException $e) {
header('Location: ERROR URL');
}
header('Location: '.$payment->getApprovalLink());
Keep in mind, this is not how I will implement it, this is just proof of concept!
I made a little CodeIgniter library with which I can easily call a payment!
If anyone is interested, don't hesitate to message me!
$this->load->library('Paypal');
$this->load->model('Config_model');
$ClientID = $this->Config_model->get('paypal.ClientID');
$Secret = $this->Config_model->get('paypal.Secret');
$defaultCurrency = $this->Config_model->get('transactions.currency');
$this->paypal->setSandbox(true);
$this->paypal->setMerchant($ClientID, $Secret);
$this->paypal->setDefaultCurrency($defaultCurrency);
$this->paypal->addItem('test', 5.3, 2);
$this->paypal->addItem('test2', 3.3, 5);
$this->paypal->setShipping(2);
$this->paypal->setTax(3);
$this->paypal->setDescription("Betalingetje");
$this->paypal->setCancelUrl('CANCEL URL HERE');
$this->paypal->setReturnUrl('RETURN URL HERE');
$this->paypal->execute();
PS: It works in conjunction with another tiny 'library' which is autoloaded. This library only includes the composer autoload.php like this:
class MyComposer
{
function __construct()
{
include("application/composer/vendor/autoload.php");
}
}
You can find the source code here: http://paypal.github.io/PayPal-PHP-SDK/sample/doc/payments/CreatePaymentUsingPayPal.html
There are so many more samples that comes packaged with the SDK itself. You can follow the instructions provided here at https://github.com/paypal/PayPal-PHP-SDK/wiki/Samples to execute samples in your local machine.
Let me know if I could be of any more help.
Here is the sample code snippet:
<?php
// # Create Payment using PayPal as payment method
// This sample code demonstrates how you can process a
// PayPal Account based Payment.
// API used: /v1/payments/payment
require __DIR__ . '/../bootstrap.php';
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
// ### Payer
// A resource representing a Payer that funds a payment
// For paypal account payments, set payment method
// to 'paypal'.
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// ### Itemized information
// (Optional) Lets you specify item wise
// information
$item1 = new Item();
$item1->setName('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setPrice(7.5);
$item2 = new Item();
$item2->setName('Granola bars')
->setCurrency('USD')
->setQuantity(5)
->setPrice(2);
$itemList = new ItemList();
$itemList->setItems(array($item1, $item2));
// ### Additional payment details
// Use this optional field to set additional
// payment information such as tax, shipping
// charges etc.
$details = new Details();
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(17.50);
// ### Amount
// Lets you specify a payment amount.
// You can also specify additional details
// such as shipping, tax.
$amount = new Amount();
$amount->setCurrency("USD")
->setTotal(20)
->setDetails($details);
// ### Transaction
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it.
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
// ### Redirect urls
// Set the urls that the buyer must be redirected to after
// payment approval/ cancellation.
$baseUrl = getBaseUrl();
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("$baseUrl/ExecutePayment.php?success=true")
->setCancelUrl("$baseUrl/ExecutePayment.php?success=false");
// ### Payment
// A Payment Resource; create one using
// the above types and intent set to 'sale'
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
// For Sample Purposes Only.
$request = clone $payment;
// ### Create Payment
// Create a payment by calling the 'create' method
// passing it a valid apiContext.
// (See bootstrap.php for more on `ApiContext`)
// The return object contains the state and the
// url to which the buyer must be redirected to
// for payment approval
try {
$payment->create($apiContext);
} catch (Exception $ex) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printError("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", null, $request, $ex);
exit(1);
}
// ### Get redirect url
// The API response provides the url that you must redirect
// the buyer to. Retrieve the url from the $payment->getApprovalLink()
// method
$approvalUrl = $payment->getApprovalLink();
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printResult("Created Payment Using PayPal. Please visit the URL to Approve.", "Payment", "<a href='$approvalUrl' >$approvalUrl</a>", $request, $payment);
return $payment;
I'm using the PayPal PHP SDK. I cannot enable the credit card payment option. Here are the relevant parts of my code and screenshots from the PayPal payment pages.
I set up my test merchant account and upgraded to pro. The test buyer account has a credit card attached to their acccount.
What can I do?
Screenshot 1
Screenshot 2
Screenshot 3
Relevant code
$payer = new Payer();
$payer->setPaymentMethod("paypal");
// Specify the payment amount.
$amount = new Amount();
$amount->setCurrency($currency);
$amount->setTotal($total);
$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription($paymentDesc);
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl($returnUrl);
$redirectUrls->setCancelUrl($cancelUrl);
$payment = new Payment();
$payment->setRedirectUrls($redirectUrls);
$payment->setIntent("sale");
$payment->setPayer($payer);
$payment->setTransactions(array($transaction));
$payment->create($this->getApiContext());
I'm coding in PHP using the Paypal REST SDK. I've set up my Sandbox account to use AUD. I worked this out after realising that my initial transactions were in USD and the transactions were getting held.
Using my revised code I'm trying to create payment - I assume I'll get back a URL which will allow me to redirect the user to approve the payment.
I'm getting a message which says:
Exception: Got Http response code 403 when accessing https://api.sandbox.paypal.com/v1/payments/payment. Retried 0 times.
string(215) "{"name":"REQUIRED_SCOPE_MISSING","message":"Access token does not have required scope","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#REQUIRED_SCOPE_MISSING","debug_id":"34683601f5dcd"}"
My code is:
$apiContext = new ApiContext(new OAuthTokenCredential(
'xxxxxx',
'xxxxxx'));
//### FundingInstrument
// A resource representing a Payer's funding instrument.
// For direct credit card payments, set the CreditCard
// field on this object.
$fi = new FundingInstrument();
$creditCardToken = new CreditCardToken();
$creditCardToken->setCreditCardId($creditcard->cardToken);
$fi->setCreditCardToken($creditCardToken);
// ### Payer
// A resource representing a Payer that funds a payment
// For direct credit card payments, set payment method
// to 'credit_card' and add an array of funding instruments.
$payer = new Payer();
$payer->setPaymentMethod("credit_card")
->setFundingInstruments(array($fi));
// ### Itemized information
// (Optional) Lets you specify item wise
// information
$paymentItems=Yii::app()->session['paymentitems'];
$items=array();
$total=0;
foreach ($paymentItems as $item)
{
$pp_item = new Item();
$pp_item->setName("Donation to ".$item->organisation->organisationName)
->setCurrency('AUD')
->setQuantity(1)
->setPrice($item->amount);
array_push($items,$pp_item);
$total+=(float)$item->amount;
}
$itemList = new ItemList();
$itemList->setItems($items);
// ### Amount
// Lets you specify a payment amount.
// You can also specify additional details
// such as shipping, tax.
$amount = new Amount();
$amount->setCurrency("AUD")
->setTotal($total);
// ### Transaction
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it.
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description");
// ### Payment
// A Payment Resource; create one using
// the above types and intent set to sale 'sale'
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setTransactions(array($transaction));
// ### Create Payment
// Create a payment by calling the payment->create() method
// with a valid ApiContext (See bootstrap.php for more on `ApiContext`) href
// The return object contains the state.
try {
$response=$payment->create($apiContext);
var_dump($response);
//$this->redirect($response->links[0]->href);
} catch (PayPal\Exception\PPConnectionException $ex) {
echo "Exception: " . $ex->getMessage() . PHP_EOL;
var_dump($ex->getData());
exit(1);
}
Any thoughts as to what this message means. It would seem that direct credit card payments are not supported by Paypal in Australia, but I don't think that's the problem.
In your Paypal developer account, under My Apps > [Your App], make sure the feature you are trying to use is enabled in the APP SETTINGS section.