PHP code example of tepuilabs / payment-processors

1. Go to this page and download the library: Download tepuilabs/payment-processors 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/ */

    

tepuilabs / payment-processors example snippets




return [

    'mercadopago' => [
        'class' => \Tepuilabs\PaymentProcessors\Services\MercadoPagoService::class,
    ],

    'paypal' => [
        'class' => \Tepuilabs\PaymentProcessors\Services\PayPalService::class,
    ],

    'stripe' => [
        'class' => \Tepuilabs\PaymentProcessors\Services\StripeService::class,
    ],

];

// usa el facade
use Tepuilabs\PaymentProcessors\Facades\PaymentProcessors;

// luego crea la instancia de la clase a usar
$params = [
    'base_uri' => 'https://api.mercadopago.com',
    'key' => '',
    'secret' => '',
    'base_currency' => '',
    'integrator_id' => '', // optional
];

$mercadopago = PaymentProcessors::resolveService('mercadopago', $params);

// necesitamos:
// $cardNetwork: visa / mastercard
// $cardToken: token de la tarjeta
// $amount: monto a cobrar
// $userEmail: correo del usuario
// $installments: default 1

$mercadopago->handlePayment('visa', 'ff8080814c11e237014c1ff593b57b4d', 177, '[email protected]');



$preference = [
    'items' => [
        [
            "title" => "Dummy Title",
            "description" => "Dummy description",
            "picture_url" => "http://project.dev/product-image.jpg",
            "category_id" => "cat123",
            "quantity" => 1,
            "currency_id" => "UYU",
            "unit_price" => 10
        ]
    ],
    'payer' => [
        'name' => 'John',
        'surname' => 'Doe',
        'email' => '[email protected]',
        'identification' => [
            'type' => '',
            'number' => ''
        ],
        'date_created' => ''
    ],
    'payment_methods' => [
        'excluded_payment_methods' => [
            [
                'id' => 'amex'
            ]
        ],
        'excluded_payment_types' => [
            [
                'id' => 'atm'
            ]
        ],
        'installments' => 6,
    ],
    'statement_descriptor' => 'MERCADOPAGO',
    'auto_return' => 'approved',
    'back_urls' => [
        'success' => 'http://project.dev/success_route',
        'failure' => 'http://project.dev/error_route',
        'pending' => 'http://project.dev/waiting_route'
    ]
];

$preference = $mercadopago->createPreference($preference);

// luego se puede usar `sandbox_init_point` para crear el enlace para que el cliente se dirija a Mercadopago a pagar

$preference->sandbox_init_point;


// usa el facade
use Tepuilabs\PaymentProcessors\Facades\PaymentProcessors;

// luego crea la instancia de la clase a usar

$params = [
    'base_uri' => 'https://api.sandbox.paypal.com',
    'client_id' => '',
    'client_secret' => '',
    'return_url' => '',
    'cancel_url' => ''
];

$paypal = PaymentProcessors::resolveService('paypal', $params);

// necesitamos:
// $amoun: el monto a cobrar
// $currency: el id de la moneda a cobrar, por defecto es USD

// este método hace una redirección a paypal

$paypal->handlePayment(200);

// el otro método que debemos usar es

$paypal->handleApproval();

// este método retorna toda la infromación del pago de ser aceptado por el usuario
// o retorna un array vacio


// usa el facade
use Tepuilabs\PaymentProcessors\Facades\PaymentProcessors;

// luego crea la instancia de la clase a usar

$params = [
    'key' => 'pk_test_51IMzM0...',
    'secret' => 'sk_test_51IM...',
];

$stripe = PaymentProcessors::resolveService('stripe', $params);

// Para generar el payment method id te recomiendo leer esto: https://github.com/TepuiLABS/payment-processors/discussions/6
// Para saber que es el payment method te invito a leer la documentación: https://stripe.com/docs/api/payment_methods

// Despues de obtener el payment method id pasamos a generar el pago de la siguiente forma:

$paymentData = [
    'amount' => 500, // integer
    'paymentMethod' => $paymentMethodId, // pm_1IOYCAJcoyM5FfOy0cVbjyuH
];

$intent = $stripe->handlePayment($paymentData);

// Y ya por ultimo, confirmamos el pago:

$confirm = $stripe->confirmPayment($intent['id']);

bash
php artisan vendor:publish --tag="payment-processors-config"