Code Examples
Practical and functional code snippets for common API operations.
Webpay Plus: Complete Flow
1. Create Transaction
Initiate the transaction to get a URL and token, then redirect the user to Transbank's payment portal.
use Illuminate\Support\Facades\Http;
$apiKey = 'YOUR_SECRET_API_KEY';
$apiUrl = 'https://payments.jupipay.app/api/v1/webpay/transactions';
$response = Http::withToken($apiKey)->post($apiUrl, [
'amount' => 15000,
'buy_order' => 'ORDER-12345',
'session_id' => 'SESSION-abcde',
'return_url' => 'https://your-store.com/webpay/callback', // Transbank will return user here
]);
if ($response->successful()) {
// Note: The response data is nested under a 'data' key.
$url = $response->json('data.url');
$token = $response->json('data.token');
// Redirect the user to Transbank's payment portal.
// The token is automatically appended by Transbank to the return_url.
return redirect()->to($url);
} else {
// Handle error. All responses follow the standard error structure.
$error = $response->json();
}
2. Commit Transaction on Callback
After the user returns, Transbank will include a `token_ws` in the request. Use this token to commit the transaction and verify the payment status.
use Illuminate\Support\Facades\Http;
use Illuminate\Http\Request;
public function handleWebpayCallback(Request $request)
{
// Transbank sends the token in the POST request body after redirection.
$token = $request->input('token_ws');
if (!$token) {
// User might have cancelled the payment on Transbank's page.
// The TBK_TOKEN key might be present if the user cancels.
if ($request->has('TBK_TOKEN')) {
return view('payment.cancelled');
}
return view('payment.error', ['message' => 'Token not found.']);
}
$apiKey = 'YOUR_SECRET_API_KEY';
$apiUrl = "https://payments.jupipay.app/api/v1/webpay/transactions/{$token}/commit";
$response = Http::withToken($apiKey)->put($apiUrl);
// Check for success and that the transaction is authorized.
if ($response->successful() && $response->json('data.status') === 'AUTHORIZED') {
$transaction = $response->json('data');
// Payment was successful! Fulfill order, show success page.
return view('payment.success', compact('transaction'));
} else {
// Payment failed or was rejected.
return view('payment.failed', ['response' => $response->json()]);
}
}
PatPass by Transbank: Subscription and Charge
1. Inscribe a Subscription
Start the subscription process. The user will be redirected to their bank to authorize the mandate (PAT).
use Illuminate\Support\Facades\Http;
$apiKey = 'YOUR_SECRET_API_KEY';
$apiUrl = 'https://payments.jupipay.app/api/v1/patpass/subscriptions';
$response = Http::withToken($apiKey)->post($apiUrl, [
'name' => 'John',
'last_name' => 'Doe',
'rut' => '11111111-1',
'person_email' => 'john.doe@example.com',
'phone' => '+56912345678',
'patpass_name' => 'Subscription Service',
'max_amount' => 50000,
'commerce_email' => 'billing@your-company.com',
'final_url' => 'https://your-app.com/patpass/callback',
'return_url' => 'https://your-app.com/patpass/return',
]);
if ($response->successful()) {
// Data is nested under a 'data' key
$url = $response->json('data.url');
// Redirect user to authorize the subscription
return redirect()->to($url);
}
2. Charge an Active Subscription
Once a subscription is confirmed (typically via webhook) and active, you can charge it on a recurring basis.
use Illuminate\Support\Facades\Http;
use Illuminate\Support\Facades\Log;
// The subscription_id (tbk_user) obtained after a successful inscription confirmation.
$subscriptionId = 'usr_A1b2C3d4E5';
$buyOrder = 'SAAS-CHARGE-' . uniqid();
$apiKey = 'YOUR_SECRET_API_KEY';
$apiUrl = "https://payments.jupipay.app/api/v1/patpass/subscriptions/{$subscriptionId}/charge";
$response = Http::withToken($apiKey)->post($apiUrl, [
'amount' => 29990,
'buy_order' => $buyOrder,
]);
if ($response->successful()) {
$charge = $response->json('data');
// Charge was successful. Log the transaction details.
Log::info('PatPass charge successful:', $charge);
} else {
// Handle charge failure
Log::error('PatPass charge failed:', $response->json());
}