PayPal Integration
#1 Install Packages for Paypal Payment Gateway
composer require srmklive/paypal
#2 Make PayPal account
#3 Create App in developer section of PayPal
we need client_id and secret_key for PayPal integration, so we need to enter in PayPal developer mode and create a new app
#4 Create sandbox account for test payment
#5 Update the PayPal config in .env file
PAYPAL_MODE=sandbox
#Paypal sandbox credential
PAYPAL_SANDBOX_CLIENT_ID=AXELAz06GFLR.............................QNu7zyjuYpFLu1g
PAYPAL_SANDBOX_CLIENT_SECRET=EA9dinW1.............................PUzgVQCz7fK4tqe1-jLZCyHzZ0tDTRAx-6qJdIY933Q
#6 Get config file in config/paypal.php
php artisan vendor:publish --provider "Srmklive\PayPal\Providers\PayPalServiceProvider"
#7 Setup the controller
use Illuminate\Http\Request;
use Srmklive\PayPal\Services\PayPal as PayPalClient;
class PayPalController extends Controller
{
# create transaction.
public function createTransaction()
{
return view('transaction');
}
# process transaction.
public function processTransaction(Request $request)
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
$paypalToken = $provider->getAccessToken();
$response = $provider->createOrder([
"intent" => "CAPTURE",
"application_context" => [
"return_url" => route('successTransaction'),
"cancel_url" => route('cancelTransaction'),
],
"purchase_units" => [
0 => [
"amount" => [
"currency_code" => "USD",
"value" => "1000.00"
]
]
]
]);
if (isset($response['id']) && $response['id'] != null) {
// redirect to approve href
foreach ($response['links'] as $links) {
if ($links['rel'] == 'approve') {
return redirect()->away($links['href']);
}
}
return redirect()
->route('createTransaction')
->with('error', 'Something went wrong.');
} else {
return redirect()
->route('createTransaction')
->with('error', $response['message'] ?? 'Something went wrong.');
}
}
# success transaction.
public function successTransaction(Request $request)
{
$provider = new PayPalClient;
$provider->setApiCredentials(config('paypal'));
$provider->getAccessToken();
$response = $provider->capturePaymentOrder($request['token']);
if (isset($response['status']) && $response['status'] == 'COMPLETED') {
return redirect()
->route('createTransaction')
->with('success', 'Transaction complete.');
} else {
return redirect()
->route('createTransaction')
->with('error', $response['message'] ?? 'Something went wrong.');
}
}
# cancel transaction
public function cancelTransaction(Request $request)
{
return redirect()
->route('createTransaction')
->with('error', $response['message'] ?? 'You have canceled the transaction.');
}
}
#8 create Routes to make Payment
Route::get('create-transaction', [PayPalController::class, 'createTransaction'])->name('createTransaction');
Route::get('process-transaction', [PayPalController::class, 'processTransaction'])->name('processTransaction');
Route::get('success-transaction', [PayPalController::class, 'successTransaction'])->name('successTransaction');
Route::get('cancel-transaction', [PayPalController::class, 'cancelTransaction'])->name('cancelTransaction');
#9 Setup the blade file to make Payment
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 5.8 PayPal Integration Tutorial - ItSolutionStuff.com</title>
<!-- Fonts -->
<link href="https://fonts.googleapis.com/css?family=Nunito:200,600" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha256-YLGeXaapI0/5IgZopewRJcFXomhRMlYYjugPLSyNjTY=" crossorigin="anonymous" />
<!-- Styles -->
<style>
html, body {
background-color: #fff;
color: #636b6f;
font-family: 'Nunito', sans-serif;
font-weight: 200;
height: 100vh;
margin: 0;
}
.content {
margin-top: 100px;
text-align: center;
}
</style>
</head>
<body>
<div class="flex-center position-ref full-height">
<div class="content">
<h1>Laravel PayPal Integration</h1>
@if(Session::has('success'))
<p class="alert alert-success">{{Session::get('success')}}</p>
@endif
@if(Session::has('error'))
<p class="alert alert-danger">{{Session::get('error')}}</p>
@endif
<table cellpadding="10" cellspacing="0" ><tr><td ></td></tr><tr><td ><a href="https://www.paypal.com/in/webapps/mpp/paypal-popup" title="How PayPal Works" onclick="javascript:window.open('https://www.paypal.com/in/webapps/mpp/paypal-popup','WIPaypal','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=1060, height=700'); return false;"><img src="https://www.paypalobjects.com/webstatic/mktg/Logo/pp-logo-200px.png" alt="PayPal Logo"></a></td></tr></table>
<a href="{{ route('processTransaction') }}" class="btn btn-success">Pay $1000 from Paypal</a>
</div>
</div>
</body>
</html>
0 Comments