Change PayPal Sandbox to Live Mode Credentials

Posted by

There are multiple phases involved in converting your Laravel application’s PayPal integration from Sandbox to Live. It is necessary for you to make sure your application is set up for production use and update your PayPal credentials and endpoints. This is a thorough how-to instruction for doing it:

Step 1: Get Live PayPal Credentials

  • Log in to your PayPal Developer account at developer.paypal.com.
  • Go to the Dashboard and select Live under My Apps & Credentials.
  • Create a new app (or use an existing one) and get your Client ID and Secret.

Step 2 : Update Environment Variables

  • Open your .env file in the Laravel project.
  • Replace the sandbox credentials with your live credentials
PAYPAL_CLIENT_ID=your-live-client-id
PAYPAL_SECRET=your-live-secret
PAYPAL_MODE=live

Step 3: Update PayPal Configuration

  • Open the config/paypal.php configuration file (or create it if it doesn’t exist).
  • Ensure it reads the environment variables correctly:
return [
    'mode'    => env('PAYPAL_MODE', 'sandbox'),
    'sandbox' => [
        'client_id'     => env('PAYPAL_SANDBOX_CLIENT_ID'),
        'client_secret' => env('PAYPAL_SANDBOX_SECRET'),
        'app_id'        => env('PAYPAL_SANDBOX_APP_ID'),
    ],
    'live' => [
        'client_id'     => env('PAYPAL_CLIENT_ID'),
        'client_secret' => env('PAYPAL_SECRET'),
        'app_id'        => env('PAYPAL_APP_ID'),
    ],

    'payment_action' => 'Sale',
    'currency'       => env('PAYPAL_CURRENCY', 'USD'),
    'notify_url'     => env('PAYPAL_NOTIFY_URL'),
    'locale'         => env('PAYPAL_LOCALE', 'en_US'),
    'validate_ssl'   => true,
];

Step 4: Clear Config Cache

  • Run the following command to clear the config cache and ensure Laravel uses the updated environment variables:
php artisan config:cache

Step 5: Update PayPal Endpoints

  • Ensure your application uses the correct PayPal endpoints based on the environment.
  • Typically, this is handled by the PayPal SDK you’re using, but you might have some custom code that specifies endpoints.
  • The PayPal endpoints are:
  • Sandbox: https://api.sandbox.paypal.com
  • Live: https://api.paypal.com

Step 6: Test the Live Integration

  • Before going live, you might want to test the live credentials with a small transaction to ensure everything is configured correctly.
  • Make sure to set up error logging and handling to catch any issues that might arise during transactions.

Example Controller Update

  • If you have a controller handling the PayPal integration, ensure it reads from the environment variables:
namespace App\Http\Controllers;

use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;

class PayPalController extends Controller
{
    private $apiContext;

    public function __construct()
    {
        $this->apiContext = new ApiContext(
            new OAuthTokenCredential(
                config('paypal.live.client_id'),
                config('paypal.live.client_secret')
            )
        );

        $this->apiContext->setConfig([
            'mode' => config('paypal.mode'),
        ]);
    }

    // Your PayPal methods...
}

Update Payment Logic

  • Make sure your payment logic uses the live environment and correct API context.
use PayPal\Api\Amount;
use PayPal\Api\Transaction;
use PayPal\Api\Payment;
use PayPal\Api\PaymentExecution;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Payer;

// Create a new payment instance
$payer = new Payer();
$payer->setPaymentMethod('paypal');

// Set the payment amount
$amount = new Amount();
$amount->setCurrency('USD')
       ->setTotal('10.00'); // Total amount

// Set the transaction details
$transaction = new Transaction();
$transaction->setAmount($amount)
            ->setDescription('Description of the payment');

// Set the redirect URLs
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl(route('paypal.success'))
             ->setCancelUrl(route('paypal.cancel'));

// Create the payment
$payment = new Payment();
$payment->setIntent('sale')
        ->setPayer($payer)
        ->setTransactions([$transaction])
        ->setRedirectUrls($redirectUrls);

try {
    $payment->create($this->apiContext);
    return redirect($payment->getApprovalLink());
} catch (\PayPal\Exception\PayPalConnectionException $ex) {
    // Handle PayPal connection exception
    return redirect()->route('paypal.error')->with('error', $ex->getMessage());
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x