PHP code example of devkussema / acapapay-codeigniter
1. Go to this page and download the library: Download devkussema/acapapay-codeigniter 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/ */
devkussema / acapapay-codeigniter example snippets
namespace Config;
use AcapaPay\CodeIgniter\Config\AcapaPay as BaseAcapaPay;
class AcapaPay extends BaseAcapaPay
{
// sobrepõe ou adiciona o que precisares
}
namespace App\Services;
use AcapaPay\CodeIgniter\Contracts\PlanoProviderInterface;
class AcapaPayPlanoProvider implements PlanoProviderInterface
{
public function listarPlanos(): array
{
$model = new \App\Models\ProdutoModel(); // ou o teu model de preços/planos
return $model->where('activo', true)->findAll();
// cada item precisa de: reference_code, name, price
// opcional: description, currency, billing_cycle, features
}
public function marcarSincronizado(string $referenceCode, string $acapapayPlanId): void
{
(new \App\Models\ProdutoModel())
->where('reference_code', $referenceCode)
->set('acapapay_plan_id', $acapapayPlanId)
->update();
}
}
public ?string $planoProvider = \App\Services\AcapaPayPlanoProvider::class;
namespace App\Controllers;
use AcapaPay\CodeIgniter\AcapaPay;
use CodeIgniter\Controller;
class PagamentoController extends Controller
{
public function comprarPlano()
{
$userId = session()->get('user_id');
$plano = 'PRO_YEARLY';
try {
$acapapay = new AcapaPay();
$urlDePagamento = $acapapay->checkoutSession(
$userId,
$plano,
['minha_metadata' => '123'],
site_url('pagamento/sucesso'),
site_url('pagamento/cancelado')
);
return view('pagamento/checkout', ['urlDePagamento' => $urlDePagamento]);
} catch (\Throwable $e) {
return redirect()->back()->with('error', $e->getMessage());
}
}
}
$dados = $acapapay->criarSessaoPagamento([
'user_id' => $userId,
'plan_reference_code' => 'PRO_YEARLY',
'amount' => 15000, // opcional: sobrepõe o preço do plano, em cêntimos
'currency' => 'AOA',
'success_url' => site_url('pagamento/sucesso'),
'cancel_url' => site_url('pagamento/cancelado'),
'metadata' => ['local_user_id' => $userId],
]);
$urlDePagamento = $dados['url'];
<?= (new \AcapaPay\CodeIgniter\AcapaPay())->renderIframe($urlDePagamento)
use CodeIgniter\Events\Events;
Events::on('acapapay:invoice.paid', function (array $data) {
$metadata = $data['data']['metadata'] ?? [];
$localUserId = $metadata['local_user_id'] ?? null;
$invoiceId = $data['data']['invoice_id'] ?? null;
// Atualiza a tua Base de Dados local
});
bash
php spark acapapay:test-connection
bash
php spark acapapay:sync-plans
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.