Learn how to seamlessly integrate the Airwallex payment gateway into your Laravel application using the redirect method. Follow our step-by-step guide for a smooth implementation.
Airwallex Payment Gateway Integrate in Laravel Using Redirect Method
Step 1: Setup Airwallex Account
- Sign up for an Airwallex account.
- Obtain your API keys (public and secret) from the Airwallex dashboard.
Step 2: Install Dependencies
Use Composer to install the necessary packages for HTTP requests. Guzzle is a popular choice for this.
composer require guzzlehttp/guzzle
Step 3: Create Configuration Files
Create a configuration file for Airwallex to store your API keys and other settings.
// config/airwallex.php
return [
'api_key' => env('AIRWALLEX_API_KEY'),
'api_secret' => env('AIRWALLEX_API_SECRET'),
'base_url' => env('AIRWALLEX_BASE_URL', 'https://api.airwallex.com'),
];
Add the corresponding entries to your .env
file:
AIRWALLEX_API_KEY=your_api_key
AIRWALLEX_API_SECRET=your_api_secret
AIRWALLEX_BASE_URL=https://api.airwallex.com
Step 4: Create Service Class for Airwallex
Create a service class to handle communication with the Airwallex API.
// app/Services/AirwallexService.php
namespace App\Services;
use GuzzleHttp\Client;
class AirwallexService
{
protected $client;
protected $apiKey;
protected $apiSecret;
protected $baseUrl;
public function __construct()
{
$this->client = new Client();
$this->apiKey = config('airwallex.api_key');
$this->apiSecret = config('airwallex.api_secret');
$this->baseUrl = config('airwallex.base_url');
}
public function createPaymentIntent($amount, $currency, $redirectUrl)
{
$response = $this->client->post($this->baseUrl . '/v1/pa/payment_intents/create', [
'json' => [
'amount' => $amount,
'currency' => $currency,
'merchant_order_id' => uniqid(),
'return_url' => $redirectUrl,
'payment_method' => 'redirect',
],
'headers' => [
'Authorization' => 'Bearer ' . $this->apiKey,
],
]);
return json_decode($response->getBody()->getContents(), true);
}
public function confirmPaymentIntent($paymentIntentId)
{
$response = $this->client->post($this->baseUrl . '/v1/pa/payment_intents/' . $paymentIntentId . '/confirm', [
'headers' => [
'Authorization' => 'Bearer ' . $this->apiKey,
],
]);
return json_decode($response->getBody()->getContents(), true);
}
// Add more methods as needed for other payment actions.
}
Step 5: Create Controller
Create a controller to handle the payment process.
// app/Http/Controllers/PaymentController.php
namespace App\Http\Controllers;
use App\Services\AirwallexService;
use Illuminate\Http\Request;
class PaymentController extends Controller
{
protected $airwallexService;
public function __construct(AirwallexService $airwallexService)
{
$this->airwallexService = $airwallexService;
}
public function createPayment(Request $request)
{
$amount = $request->input('amount');
$currency = $request->input('currency', 'USD');
$redirectUrl = route('payment.success');
$paymentIntent = $this->airwallexService->createPaymentIntent($amount, $currency, $redirectUrl);
if (isset($paymentIntent['payment_url'])) {
return redirect($paymentIntent['payment_url']);
}
return response()->json(['error' => 'Unable to create payment intent'], 500);
}
public function success(Request $request)
{
$paymentIntentId = $request->query('payment_intent_id');
$paymentIntent = $this->airwallexService->confirmPaymentIntent($paymentIntentId);
if ($paymentIntent['status'] === 'succeeded') {
return view('payment.success');
}
return view('payment.failure');
}
}
Step 6: Define Routes
Define routes for the payment actions in your routes/web.php
file.
// routes/web.php
use App\Http\Controllers\PaymentController;
Route::post('/create-payment', [PaymentController::class, 'createPayment'])->name('payment.create');
Route::get('/payment-success', [PaymentController::class, 'success'])->name('payment.success');
Step 7: Frontend Integration
Create a frontend form to collect payment details and call the backend API.
<!-- resources/views/payment.blade.php -->
<form action="{{ route('payment.create') }}" method="POST">
@csrf
<label for="amount">Amount</label>
<input type="number" name="amount" id="amount" required>
<label for="currency">Currency</label>
<input type="text" name="currency" id="currency" value="USD">
<button type="submit">Pay</button>
</form>
Step 8: Handling Success and Failure Views
Create views to display success and failure messages.
<!-- resources/views/payment/success.blade.php -->
<h1>Payment Successful</h1>
<p>Your payment was processed successfully.</p>
<!-- resources/views/payment/failure.blade.php -->
<h1>Payment Failed</h1>
<p>There was an issue processing your payment. Please try again.</p>
Step 9: Testing
Test the integration thoroughly in a sandbox environment provided by Airwallex before going live.
Step 10: Go Live
Once testing is successful, switch to the live API keys and endpoint provided by Airwallex.
By following these steps, you should be able to integrate the Airwallex payment gateway using the redirect_method
into your Laravel application.