Laravel Google Pay Integration Example
In this post, we'll walk through the process of integrating Google Pay in a Laravel application.
Step 1: Install Required Packages
First, ensure your project is set up with the necessary packages. You can use the following command to install a payment gateway package like Stripe or Braintree if required:
composer require stripe/stripe-php
Step 2: Setup Google Pay in Frontend
Next, add Google Pay JavaScript in your Blade template or HTML page:
<script async src="https://pay.google.com/gp/p/js/pay.js"></script>
Step 3: Initialize Google Pay
Add the following JavaScript code to initialize Google Pay:
<script>
const baseRequest = {
apiVersion: 2,
apiVersionMinor: 0
};
const allowedCardNetworks = ["MASTERCARD", "VISA"];
const allowedCardAuthMethods = ["PAN_ONLY", "CRYPTOGRAM_3DS"];
const tokenizationSpecification = {
type: 'PAYMENT_GATEWAY',
parameters: {
'gateway': 'stripe', // or your gateway name
'stripe:version': '2020-08-27',
'stripe:publishableKey': 'your-publishable-key'
}
};
const cardPaymentMethod = {
type: 'CARD',
parameters: {
allowedAuthMethods: allowedCardAuthMethods,
allowedCardNetworks: allowedCardNetworks
},
tokenizationSpecification: tokenizationSpecification
};
const paymentsClient = new google.payments.api.PaymentsClient({ environment: 'TEST' });
const paymentDataRequest = Object.assign({}, baseRequest);
paymentDataRequest.allowedPaymentMethods = [cardPaymentMethod];
paymentDataRequest.transactionInfo = {
totalPriceStatus: 'FINAL',
totalPrice: '10.00',
currencyCode: 'USD'
};
paymentDataRequest.merchantInfo = {
merchantId: 'your-merchant-id',
merchantName: 'Your Merchant Name'
};
function onGooglePayLoaded() {
paymentsClient.isReadyToPay({ allowedPaymentMethods: [cardPaymentMethod] })
.then(function(response) {
if (response.result) {
createAndAddButton();
}
})
.catch(function(err) {
console.error(err);
});
}
function createAndAddButton() {
const button = paymentsClient.createButton({ onClick: onGooglePayButtonClicked });
document.getElementById('container').appendChild(button);
}
function onGooglePayButtonClicked() {
paymentsClient.loadPaymentData(paymentDataRequest)
.then(function(paymentData) {
// Handle payment data
console.log(paymentData);
})
.catch(function(err) {
console.error(err);
});
}
</script>
Step 4: Backend Integration
On the server side, you can use a package like stripe/stripe-php
to process the payment:
<?php
namespace App\\Http\\Controllers;
use Illuminate\\Http\\Request;
use Stripe\\Stripe;
use Stripe\\Charge;
class PaymentController extends Controller
{
public function processPayment(Request $request)
{
Stripe::setApiKey('your-stripe-secret-key');
try {
$charge = Charge::create([
'amount' => $request->amount,
'currency' => 'usd',
'source' => $request->token,
'description' => 'Payment Description'
]);
return response()->json(['success' => true, 'data' => $charge]);
} catch (\\Exception $e) {
return response()->json(['success' => false, 'message' => $e->getMessage()]);
}
}
}
Step 5: Testing
Make sure to test your integration in the Google Pay TEST environment before switching to production.
With this setup, you have a working integration of Google Pay with Laravel!
0 Comments