Reputation: 11
I followed some dudes tutorial on how to Charge People with PayPal using the PHP SDK. It all works like a charm as long as I'm in Testmode, but if I cange my API Keys to the "live" ones, I only get an HTTP Error 401. I understand that this is because I have to set the Application to "Live".
I followed the Guide on PayPals GitHub Page. (https://github.com/paypal/PayPal-PHP-SDK/wiki/Going-Live)
Dont worry, I dont do this for the acutal Web, I just want to get back into PHP and it drives me nuts that I am not able to configure this script.
I have the following "charge.php":
require 'app/start.php';
use PayPal\Api\Payer;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Details;
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payment;
if(!isset($_POST['product'])){
echo $_POST['product'];
die("Nope");
}
$product="Premium";
$price="1.00";
$shipping="0.00";
$total = $price+$shipping;
$payer = new Payer();
$payer->setPaymentMethod('paypal');
$item = new Item();
$item->setName($product)
->setCurrency('EUR')
->setQuantity(1)
->setPrice($price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$details = new Details();
$details->setShipping($shipping)
->setSubtotal($price);
$amount = new Amount();
$amount->setCurrency('EUR')
->setTotal($total)
->setDetails($details);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription($product)
//UserID
->setInvoiceNumber(rand());
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('/paid.php?sucess=true')
->setCancelUrl('/paid.php?sucess=false');
$payment = new Payment();
$payment->setIntent('sale')
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions([$transaction]);
try{
$payment->create($paypal);
} catch (Exception $e){
die($e);
}
$approvalUrl=$payment->getApprovalLink();
header("Location: {$approvalUrl}");
And the following "start.php" with my API Credentials in it:
require 'vendor/autoload.php';
define('SITE_URL', '/charge.php');
$paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'xx',
'xx'
)
);
Can someone give me a hint, where I need to confige the App to use PayPals Live API? I tried it in the carge.php, but I only get the PHP Error, that I use an undefined variable.
Im talking about the following Snipit from PayPals Github:
$apiContext->setConfig(
array(
...
'mode' => 'live',
...
)
);
Any help would be much appreciated. Cheers, Flo
Upvotes: 0
Views: 811
Reputation: 11
Okay, I figured it out. For anyone who has the same issue:
For the config array to work, you also have to put the Nameclass in there. So the correct "start.php" should look like this:
<?php
use \PayPal\Rest\ApiContext;
use \PayPal\Auth\OAuthTokenCredential;
require 'vendor/autoload.php';
define('SITE_URL', 'charge_paypal.php');
$paypal = new \PayPal\Rest\ApiContext(
new \PayPal\Auth\OAuthTokenCredential(
'x',
'x'
)
);
$paypal->setConfig([
'mode' => 'live',
'log.LogEnabled' => true,
'log.FileName' => 'PayPal.log',
'log.LogLevel' => 'FINE'
]);
?>
Upvotes: 1