## Getting Started
### Minimal Setup
1. **Installation**
**Deprecated Warning**: This SDK (v1.14.0) is **archived** and **deprecated**. Migrate to [PayPal’s official PHP SDK v1.15.0+](https://github.com/paypal/PayPal-PHP-SDK) for long-term support.
If forced to use this version, install via Composer:
```bash
composer require paypal/rest-api-sdk-php:1.14.0
Verify composer.json for conflicts with newer PayPal SDKs or Laravel dependencies.
First Use Case: Basic API Call Initialize the client with updated logging and PHP version support (7.1+):
use PayPal\Api\Amount;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\Transaction;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
// Config (use .env for secrets)
$apiContext = new ApiContext(
new OAuthTokenCredential(
config('services.paypal.client_id'),
config('services.paypal.secret'),
config('services.paypal.access_token')
),
config('services.paypal.env') // 'sandbox' or 'live'
);
$apiContext->setConfig([
'log.LogEnabled' => true,
'log.FileName' => storage_path('logs/paypal.log'),
'log.LogLevel' => 'DEBUG', // Updated in 1.14.0 (see #983)
'cache.Directory' => storage_path('paypal_cache'), // Fixed in #1062
]);
// Example: Create a payment with idempotency key
$payment = new Payment();
$payment->setIntent('sale')
->setIdempotencyKey(strtolower(Uuid::generate())) // Best practice
->setPayer((new Payer())->setPaymentMethod('paypal'))
->setTransactions([(new Transaction())
->setAmount((new Amount())->setCurrency('USD')->setTotal('10.00'))
->setDescription('Test Payment')]);
try {
$createdPayment = $payment->create($apiContext);
echo "Payment ID: " . $createdPayment->getId();
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
// Parse error data (now consistently formatted as array in #1034)
$errorData = json_decode($ex->getData(), true);
echo $errorData['name'] ?? 'Unknown error';
}
Key Files to Reference
src/PayPal/Api/: Core models (updated RefundCapture in #998).src/PayPal/Rest/: API context and exceptions.examples/: Deprecated but useful for legacy patterns.tests/ for refactored test cases (#1011).OAuth Flow (Updated)
$credential = new OAuthTokenCredential(
config('services.paypal.client_id'),
config('services.paypal.secret')
);
$apiContext = new ApiContext($credential);
$apiContext->getAccessToken(); // Auto-refreshes if expired
Payment Lifecycle (1.14.0 Fixes)
RefundCapture class (#998) supports:
$sale = Sale::get($saleId, $apiContext);
$refund = new Refund();
$refund->setAmountWithBreakdown($amount);
$sale->refund($refund);
Laravel Integration (Best Practices)
ApiContext with PHP 7.1+ support:
$this->app->singleton('paypal.apiContext', function ($app) {
$credential = new OAuthTokenCredential(
$app['config']['services.paypal.client_id'],
$app['config']['services.paypal.secret']
);
$apiContext = new ApiContext($credential, $app['config']['services.paypal.env']);
$apiContext->setConfig([
'log.LogEnabled' => app()->environment('local'),
'cache.Directory' => storage_path('paypal_cache'),
]);
return $apiContext;
});
PayPal facade with updated error handling:
PayPal::payment()
->setIdempotencyKey($key)
->create();
Logging and Debugging
$apiContext->setConfig(['log.LogLevel' => 'DEBUG']); // or 'INFO', 'WARN', 'ERROR'
storage_path('paypal_cache') is writable.Testing
$mockContext = $this->createMock(ApiContext::class);
$mockContext->method('getConfig')->willReturn(['log' => []]);
Deprecation and Migration
OAuthTokenCredential with PayPal\Auth\OAuthTokenCredential (updated in v1.15.0).RefundCapture usage (#998 in this version).Token and Cache Issues
$apiContext->setConfig(['cache.Directory' => storage_path('paypal_cache')]);
is writable. Fallback to system temp dir if needed.$credential = new OAuthTokenCredential($clientId, $secret);
$apiContext = new ApiContext($credential);
$apiContext->getAccessToken(); // Auto-refreshes
Error Handling (Updated)
catch (\PayPal\Exception\PayPalConnectionException $ex) {
$error = json_decode($ex->getData(), true);
if (isset($error['details'][0]['issue'])) {
Log::error($error['details'][0]['issue']);
}
}
INSTRUMENT_DECLINED: Direct credit card restrictions (#1019).VALIDATION_ERROR: Check error_data for field-specific issues.Idempotency
$payment->setIdempotencyKey(strtolower(Uuid::generate()));
Webhooks
paypal/webhooks-php package.PHP Version Support
.env:How can I help you explore Laravel packages today?