How to integration googlepay in codeigniter 3 - deesoft service

How to integration googlepay in codeigniter 3

Deepak Tailor Image
Deepak Tailor - Feb 10 2023
How to integration googlepay in codeigniter 3

Here is a general outline for integrating Google Pay into a CodeIgniter 3 application:

1. Set up a Google Pay merchant account and obtain the necessary API keys.

2. Install the Google Pay API client library for PHP using composer.

3. In your CodeIgniter 3 application, create a controller for handling payment processing.

4. In the controller, use the Google Pay API client library to create a PaymentDataRequest object, which will contain information about the transaction.

5. Use the PaymentDataRequest object to generate a Google Pay payment button, which can be displayed on your website.

6. When the user clicks the button, they will be prompted to select their preferred payment method and enter their shipping information.

7. The payment information will be sent to the Google Pay server, where it will be processed.

8. If the payment is successful, the Google Pay server will return a PaymentData object, which contains information about the transaction.

9. Use the PaymentData object to verify the payment and complete the transaction in your CodeIgniter 3 application.

10. Store the transaction information in your database for future reference.

This is just a general outline, and the actual implementation will depend on your specific requirements and the details of your CodeIgniter 3 application. Please refer to the Google Pay API documentation for more information.

Add the following code to your controller, which creates the PaymentDataRequest object:

use Google\Payment\Request\PaymentDataRequest;

public function createPaymentDataRequest() {
    $paymentDataRequest = new PaymentDataRequest();
    $paymentDataRequest->apiVersion = 2;
    $paymentDataRequest->apiVersionMinor = 0;
    $paymentDataRequest->merchantInfo = new MerchantInfo();
    $paymentDataRequest->merchantInfo->merchantId = "YOUR_MERCHANT_ID";
    $paymentDataRequest->merchantInfo->merchantName = "YOUR_MERCHANT_NAME";
    $paymentDataRequest->transactionInfo = new TransactionInfo();
    $paymentDataRequest->transactionInfo->totalPriceStatus = "FINAL";
    $paymentDataRequest->transactionInfo->totalPrice = "YOUR_TOTAL_PRICE";
    $paymentDataRequest->transactionInfo->currencyCode = "YOUR_CURRENCY_CODE";
    return $paymentDataRequest;
}

Add the following code to your controller, which generates the Google Pay payment button:

use Google\Payment\PaymentMethod;

public function generateGooglePayButton() {
    $paymentDataRequest = $this->createPaymentDataRequest();
    $allowedPaymentMethods = [
        PaymentMethod::CARD,
        PaymentMethod::TOKENIZED_CARD
    ];
    $paymentButton = new PaymentButton();
    $paymentButton->paymentDataRequest = $paymentDataRequest;
    $paymentButton->allowedPaymentMethods = $allowedPaymentMethods;
    $paymentButton->buttonColor = "default";
    $paymentButton->buttonType = "long";
    return $paymentButton->toJson();
}

Add the following code to your view, which displays the Google Pay payment button:

<button id="google-pay-button"></button>

<script src="https://pay.google.com/gp/p/js/pay.js" onload="onGooglePayLoaded()" async></script>

<script>
function onGooglePayLoaded() {
    const paymentDataRequest = <?php echo $this->generateGooglePayButton(); ?>;
    const paymentsClient = new google.payments.api.PaymentsClient({
        environment: 'TEST'
    });
    paymentsClient.loadPaymentData(paymentDataRequest)
        .then(function(paymentData) {
            // Process the payment data here
            console.log(paymentData);
        })
        .catch(function(err) {
            console.error(err);
        });
}
</script>

Add the following code to your controller, which verifies the payment and completes the transaction:

use Google\Payment\Response\PaymentData;

public function processPayment() {
    $paymentData = new PaymentData($_POST['paymentData']);
    $signature = base64_decode($paymentData->signature);
    $signedMessage = $paymentData->signedMessage;
    $publicKey = "YOUR_PUBLIC_KEY";

    $verificationStatus = openssl_verify($signedMessage, $signature, $publicKey, OPENSSL_ALGO_SHA256);
    if ($verificationStatus === 1) {
        // The payment data is valid
        // Perform actions to complete the transaction
        // Store the transaction information in your database
    } else {
        // The payment data is not valid
        // Handle the error
    }
}
Deepak Tailor Image
Deepak Tailor

My name is Deepak tailor as a fullstack developer. I have been in the IT industry (PHP, Nodejs, flutter) for the last 5 years. For professional and customize web development & app development, you can send inquiry on our email.
----
You can contact him at deepaktailor10@yahoo.in