Ravioli87
Ravioli87

Reputation: 835

PayPal SDK: No Server Response when Paying with PayPal Account

I have scoured the docs and this tutorial about 100 times but can't figure out how to go through the payment process using a PayPal account as opposed to a credit card. I have gotten the credit card payment to go through just fine.

In the aforementioned tutorial, it is stated that I am supposed to expect a JSON response from the server after making an OAuth credential:

$sdkConfig = array(
    "mode" => "sandbox"
);

$cred = new OAuthTokenCredential("clientID","clientSecret", $sdkConfig);

I get absolutely no server response despite geting a '200 OK' status from the server.

Initially I had my code set up like a bunch of other tutorials:

$apiContext = new ApiContext(
        new OAuthTokenCredential(
                'clientID',     // ClientID
                'clientSecret'      // ClientSecret
        )
);

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

$item1 = new Item();
$item1->setName("Donation squares.");
$item1->setCurrency('USD');
$item1->setQuantity(1);
$item1->setPrice(1);

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

$amountDetails = new Details();
$amountDetails->setSubtotal('7.41');
$amountDetails->setTax('0.03');
$amountDetails->setShipping('0.03');

$amount = new Amount();
$amount->setCurrency('USD');
$amount->setTotal('7.47');
$amount->setDetails($amountDetails);

$transaction = new Transaction();
$transaction->setAmount($amount);
$transaction->setDescription('This is the payment transaction description.');

$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl("https://devtools-paypal.com/guide/pay_paypal/php?success=true");
$redirectUrls->setCancelUrl("https://devtools-paypal.com/guide/pay_paypal/php?cancel=true");

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

try{
    $payment->create($apiContext);
} catch(Exception $e){
    echo $e
    exit(1);
}

None of this works either - no response from the server whatsoever.

ANSWER / COMMENT You're not supposed to be looking for a JSON response after all. If the payment is successfully created, an approval URL should be generated by running

$payment->getApprovalLink();

The user then follows this link to finalize his/her payment.

Upvotes: 0

Views: 289

Answers (1)

Jay Patel - PayPal
Jay Patel - PayPal

Reputation: 1489

You might be interested in following a very simple instruction here at PayPal-PHP-SDK, that explains how to make calls using PayPal PHP SDK. I know you have already gone through that, and are looking for how to create a payment using PayPal.

I am not sure you are aware of this, but PayPal-PHP-SDK comes along with a lot of samples, that you could run by just one simple command (if you have PHP 5.4 or higher). One of the first sample have the instructions and code to make PayPal Call.

There are two steps involved in that.

  1. Create a Payment. Receive a approval_url link to be used to ask the user to complete Paypal flow over any browser.

  2. Once the user accepts the payment on paypal website, it will be redirected back to your website, where you execute the payment.

Both the code samples are provided here and here.

Let me know if this helps, and you have any more questions. I would be more than happy to help.

enter image description here

Upvotes: 1

Related Questions