PHP Classes

File: payment-example/index.php

Recommend this page to a friend!
  Packages of Rodrigo Faustino   Core Payment   payment-example/index.php   Download  
File: payment-example/index.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Core Payment
Integrate payments with different payment services
Author: By
Last change:
Date: 8 months ago
Size: 1,737 bytes
 

Contents

Class file image Download
<?php

require_once __DIR__ . '/vendor/autoload.php';

use PaymentAdapter\Contracts\PaymentPayload;
use PaymentAdapter\PaymentService;
use Dotenv\Dotenv;

// 1. Carregar variáveis de ambiente (.env)
$dotenv = Dotenv::createImmutable(__DIR__);
$dotenv->safeLoad();

// 2. Montar o Payload Universal (Independente de gateway)
// A grande vantagem do PaymentAdapter é esta assinatura unificada!
$payload = new PaymentPayload(
    amount: 150.00,
    currency: 'BRL',
    description: 'Plano Anual - LanyardAR',
    customerEmail: 'cliente@exemplo.com',
    customerName: 'Cliente Exemplo',
    externalReference: 'ped_' . uniqid(),
    metadata: [
        'returnUrl' => 'http://localhost:8000/callback.php'
    ]
);

echo "Iniciando processamento de pagamento...\n <br>";
echo "========================================\n\n <br>";

try {
    // 3. Inicializar o serviço de pagamento
    // Opcões nativas: 'stripe' ou 'mercadopago' (configurado em config/gateways.php no adapter)
    $gatewayUsado = 'stripe';
    $service = new PaymentService($gatewayUsado);

    // 4. Disparar a transação
    echo "Enviando payload via gateway: {$gatewayUsado}\n <br>";
    $resultado = $service->charge($payload);

    echo "Status: " . $resultado->status . "\n <br>";
    
    // O transactionId é o ID gerado pelo Stripe/MercadoPago, muito importante salvar ele no banco
    echo "ID Transação Externa (Salvar no DB): " . $resultado->transactionId . "\n";

    if ($resultado->paymentUrl) {
        echo "URL de Checkout Gerada (Para redirecionamento): \n>  <br>" . $resultado->paymentUrl . "\n";
    }

    echo "\n--- Simulação finalizada ---\n <br>";

} catch (\Exception $e) {
    echo "Erro inesperado: " . $e->getMessage() . "\n";
}