PHP code example of leodyversemilla07 / paymongo-php

1. Go to this page and download the library: Download leodyversemilla07/paymongo-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

leodyversemilla07 / paymongo-php example snippets




aymongo\PaymongoClient;

$client = new PaymongoClient('sk_test_your_secret_key');

// Create a payment intent
$paymentIntent = $client->paymentIntents->create([
    'amount' => 10000, // Amount in cents (100.00 PHP)
    'currency' => 'PHP',
    'payment_method_allowed' => ['card', 'gcash', 'grab_pay'],
    'description' => 'Order #12345',
]);

echo $paymentIntent->id;
echo $paymentIntent->client_key;

// Create a payment intent
$paymentIntent = $client->paymentIntents->create([
    'amount' => 10000,
    'currency' => 'PHP',
    'payment_method_allowed' => ['card'],
]);

// Retrieve a payment intent
$paymentIntent = $client->paymentIntents->retrieve('pi_xxx');

// Attach a payment method to a payment intent
$paymentIntent = $client->paymentIntents->attach('pi_xxx', [
    'payment_method' => 'pm_xxx',
]);

// Capture a payment intent
$paymentIntent = $client->paymentIntents->capture('pi_xxx', [
    'amount' => 10000,
]);

// Cancel a payment intent
$paymentIntent = $client->paymentIntents->cancel('pi_xxx');

$checkoutSession = $client->checkoutSessions->create([
    'description' => 'Order #12345',
    'line_items' => [
        [
            'name' => 'Product Name',
            'quantity' => 1,
            'amount' => 10000,
            'currency' => 'PHP',
        ],
    ],
    'payment_method_types' => ['card', 'gcash', 'grab_pay'],
    'success_url' => 'https://example.com/success',
    'cancel_url' => 'https://example.com/cancel',
]);

// Redirect user to checkout
header('Location: ' . $checkoutSession->url);

// Create a refund
$refund = $client->refunds->create([
    'amount' => 5000,
    'payment_id' => 'pay_xxx',
    'reason' => 'requested_by_customer',
]);

// List refunds
$refunds = $client->refunds->all(['limit' => 10]);

// Create a webhook
$webhook = $client->webhooks->create([
    'url' => 'https://example.com/webhooks/paymongo',
    'events' => [
        'payment.paid',
        'payment.failed',
    ],
]);

// Verify webhook signature
try {
    $payload = file_get_contents('php://input');
    $signatureHeader = $_SERVER['HTTP_PAYMONGO_SIGNATURE'];
    
    $event = $client->webhooks->constructEvent([
        'payload' => $payload,
        'signature_header' => $signatureHeader,
        'webhook_secret_key' => 'whsec_xxx',
    ]);

    switch ($event->type) {
        case 'payment.paid':
            // Handle successful payment
            break;
        case 'payment.failed':
            // Handle failed payment
            break;
    }

    http_response_code(200);
} catch (\Paymongo\Exceptions\SignatureVerificationException $e) {
    http_response_code(400);
    exit('Invalid signature');
}

// Create a plan
$plan = $client->plans->create([
    'name' => 'Premium Plan',
    'amount' => 99900,
    'currency' => 'PHP',
    'interval' => 'month',
]);

// Create a subscription
$subscription = $client->subscriptions->create([
    'customer_id' => 'cus_xxx',
    'plan_id' => 'plan_xxx',
    'payment_method_id' => 'pm_xxx',
]);

// Cancel a subscription
$subscription = $client->subscriptions->cancel('sub_xxx');

use Paymongo\Exceptions\InvalidRequestException;
use Paymongo\Exceptions\AuthenticationException;
use Paymongo\Exceptions\ResourceNotFoundException;

try {
    $paymentIntent = $client->paymentIntents->create([
        'amount' => 10000,
        'currency' => 'PHP',
        'payment_method_allowed' => ['card'],
    ]);
} catch (AuthenticationException $e) {
    // Invalid API key
    echo 'Authentication failed: ' . $e->getMessage();
} catch (InvalidRequestException $e) {
    // Invalid parameters
    foreach ($e->getError() as $error) {
        echo $error->detail;
    }
} catch (ResourceNotFoundException $e) {
    // Resource not found
    echo 'Resource not found';
}

$client = new PaymongoClient('sk_test_xxx', [
    'timeout' => 30,
    'connect_timeout' => 10,
    'idempotency_key' => 'unique-request-id',
]);

$client = new PaymongoClient('sk_test_xxx', [
    'customer_bearer_token' => 'jwt_customer_token',
]);

use Paymongo\ApiResource;
use Paymongo\Entities\PaymentIntent;
use Paymongo\Exceptions\UnexpectedValueException;

$resource = new ApiResource([
    'data' => [
        'id' => 'pi_test_123',
        'attributes' => [
            // 'amount' is missing here
            'capture_type' => 'automatic',
            'client_key' => 'pi_client_key',
            'currency' => 'PHP',
            'livemode' => false,
            'status' => 'awaiting_payment_method',
            'payment_method_allowed' => ['card'],
            'created_at' => 0,
            'updated_at' => 0,
        ],
    ],
]);

try {
    new PaymentIntent($resource);
} catch (UnexpectedValueException $e) {
    echo $e->getMessage();
}
bash
composer