PHP code example of tomasmanueltm / apypayment
1. Go to this page and download the library: Download tomasmanueltm/apypayment 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/ */
tomasmanueltm / apypayment example snippets
return [
'default_currency' => 'AOA',
'default_payment_method' => 'REF',
'prefixes' => [
'default' => 'PS',
'subscription' => 'SUB',
],
];
use TomasManuelTM\ApyPayment\Services\ApyService;
$service = app('ApyService');
// ou
$service = app(ApyService::class);
try {
$payment = $service->createPayment([
'amount' => 100.00,
'description' => 'Pagamento do pedido #12345',
'reference' => 'REF-001' // opcional, será gerado automaticamente
]);
if ($payment['success']) {
echo "Pagamento criado: {$payment['merchantTransactionId']}";
echo "Referência: {$payment['reference']}";
}
} catch (\Exception $e) {
echo "Erro: {$e->getMessage()}";
}
$payments = $service->getPayments();
foreach ($payments['data'] ?? [] as $payment) {
echo "ID: {$payment['merchantTransactionId']} - Status: {$payment['status']}";
}
$methods = $service->getApplications();
foreach ($methods['data'] ?? [] as $method) {
echo "Método: {$method['name']} - Tipo: {$method['type']}";
}
$status = $service->getPaymentStatus('PT000000001');
if ($status['success']) {
echo "Status: {$status['status']}";
echo "Valor: {$status['amount']}";
}
$payment = $service->createPayment([
'amount' => 1500.00,
'description' => 'Compra de produto #123',
'reference' => 'ORDER-2025-001'
]);
[
'success' => true,
'merchantTransactionId' => 'PT000000001',
'reference' => 'ORDER-2025-001',
'amount' => 1500.00,
'status' => 'pending',
'expiration' => '2025-01-15T10:30:00Z',
'paymentUrl' => 'https://...' // URL para pagamento
]
$result = $service->capturePayment('PT000000001');
// Retorna: ['success' => true, 'data' => [...]]
// Reembolso total
$refund = $service->refundPayment('PT000000001');
// Reembolso parcial
$refund = $service->refundPayment('PT000000001', 500.00);
$status = $service->getPaymentStatus('PT000000001');
$payments = $service->getPayments();
$methods = $service->getApplications();
use TomasManuelTM\ApyPayment\Application\Services\PaymentService;
use TomasManuelTM\ApyPayment\Domain\Payment\ValueObjects\Amount;
// Injeção de dependência
$paymentService = app(PaymentService::class);
// Criar pagamento com validação de domínio
$payment = $paymentService->createPayment([
'amount' => 100.00,
'description' => 'Teste DDD'
]);
// Capturar usando regras de negócio
$captured = $paymentService->capturePayment('PT000000001');
ApyPayment::addUpdateRule(
'orders',
'transaction_id',
'merchantTransactionId',
'TEST123',
'completed'
);
// No arquivo config/apypayment.php
'prefixes' => [
'default' => 'PS',
'renewal' => 'PC',
'custom' => 'CX'
],
try {
$payment = $service->createPayment($data);
} catch (\TomasManuelTM\ApyPayment\Exceptions\PaymentCreationException $e) {
// Erro na criação - verificar dados
Log::error('Pagamento falhou', [
'data' => $data,
'error' => $e->getMessage()
]);
} catch (\TomasManuelTM\ApyPayment\Exceptions\InvalidRequestException $e) {
// Dados inválidos - mostrar erro ao usuário
return back()->withErrors(['payment' => 'Dados de pagamento inválidos']);
} catch (\TomasManuelTM\ApyPayment\Exceptions\PaymentNotFoundException $e) {
// Pagamento não existe
return response()->json(['error' => 'Pagamento não encontrado'], 404);
} catch (\Exception $e) {
// Erro genérico - log e notificar admin
Log::critical('Erro crítico no pagamento', [
'exception' => $e,
'trace' => $e->getTraceAsString()
]);
}
// Monitorar estes eventos
Log::info('Payment created', ['id' => $merchantId]);
Log::warning('Payment retry', ['attempt' => $attempt]);
Log::error('Payment failed', ['error' => $error]);
// Validação robusta
class PaymentRequest extends FormRequest
{
public function rules()
{
return [
'amount' => ' ];
}
}