PHP code example of aliff / chip-in

1. Go to this page and download the library: Download aliff/chip-in 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/ */

    

aliff / chip-in example snippets




namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Log;

class PaymentController extends Controller
{
    public function handleChipInCallback(Request $request)
    {
        $data = $request->all();
        Log::info('💳 CHIP callback received', ['data' => $data]);

        // ✅ Extract Entity ID (stored in reference)
        $entityId = Arr::get($data, 'reference');

        // Fallback: try metadata
        if (! $entityId) {
            $metadata = Arr::get($data, 'metadata', []);
            $entityId = $metadata['entity_id'] ?? null;
        }

        if (! $entityId) {
            Log::warning('⚠️ Missing Entity ID in callback', ['data' => $data]);
            return response()->json(['error' => 'Missing Entity ID'], 400);
        }

        // ✅ Find your entity record (e.g., WillWasiat, Order, etc.)
        $entity = YourModel::find($entityId);

        if (! $entity) {
            Log::warning("⚠️ Entity not found for ID {$entityId}");
            return response()->json(['error' => 'Entity not found'], 404);
        }

        // ✅ Create or update Payment record
        $payment = Payment::updateOrCreate(
            ['chip_id' => Arr::get($data, 'id')],
            [
                'payable_id'   => $entity->id,
                'payable_type' => YourModel::class,
                'status'       => Arr::get($data, 'status'),
                'email'        => Arr::get($data, 'email', Arr::get($data, 'client.email')),
                'amount'       => Arr::get($data, 'payment.amount', Arr::get($data, 'amount')),
                'currency'     => Arr::get($data, 'payment.currency', 'MYR'),
                'reference'    => Arr::get($data, 'reference'),
                'brand_id'     => Arr::get($data, 'brand_id'),
                'payload'      => $data,
                'checkout_url' => Arr::get($data, 'checkout_url'),
                'paid_at'      => now(),
            ]
        );

        // ✅ Update entity status
        $entity->update([
            'payment_status' => $payment->status ?? 'pending',
        ]);

        Log::info("✅ Payment stored & linked", [
            'payment_id' => $payment->id,
            'entity_id' => $entity->id,
            'status' => $payment->status,
        ]);

        // ✅ If callback came from browser (user flow), redirect back to app
        if ($request->expectsJson() === false) {
            return redirect()
                ->route('entity.completed', ['id' => $entity->id])
                ->with('success', 'Your payment has been successfully processed!');
        }

        // ✅ For API/callback endpoint
        return response()->json(['success' => true]);
    }
}



namespace App\Http\Controllers;

use Aliff\ChipIn\Http\Client;
use Aliff\ChipIn\Http\Endpoints\Purchase;
use Illuminate\Support\Facades\Crypt;

class OrderController extends Controller
{
    public function proceed()
    {
        $payload = [
            'client' => [
                'email' => $this->entity->user->email ?? auth()->user()->email,
            ],
            'purchase' => [
                'products' => [
                    [
                        'name' => 'Your Product Name',
                        'price' => 13424,  // Amount in cents (134.24)
                        'quantity' => 1,
                    ],
                ],
                'due_strict' => false,
            ],
            'reference' => $this->entity->id,  // ← Store your entity ID here
            'success_redirect' => route('entity.completed', [
                'id' => Crypt::encrypt($this->entity->id),
                'status' => 'success'
            ]),
            'failed_redirect' => route('entity.completed', [
                'id' => Crypt::encrypt($this->entity->id),
                'status' => 'failed'
            ]),
        ];

        $purchase = new Purchase(app(Client::class));

        return $purchase->createAndRedirect($payload);
    }
}

$purchase = new Purchase(new Client());
$response = $purchase->create([
    'amount' => 10000,
    'reference' => 'ORDER-123',
    'client' => ['email' => '[email protected]'],
]);

$checkoutUrl = $response['checkout_url'];

return $purchase->createAndRedirect([
    'amount' => 10000,
    'reference' => 'ORDER-123',
    'client' => ['email' => '[email protected]'],
]);

$normalized = $purchase->handleCallback($chipWebhookData);
// Returns: ['id', 'status', 'email', 'amount', 'reference', 'checkout_url', 'raw']

$client = new Client();
$response = $client->post('purchases/', $payload);

$response = $client->get('purchases/{id}/');

[
    'id' => 'chip_payment_id',
    'status' => 'paid|failed|cancelled',
    'email' => '[email protected]',
    'amount' => 13424,  // in cents
    'currency' => 'MYR',
    'reference' => 'your_entity_id',  // Your stored reference
    'brand_id' => 'your_brand_id',
    'payment' => [
        'amount' => 13424,
        'currency' => 'MYR',
    ],
    'client' => [
        'email' => '[email protected]',
    ],
    'checkout_url' => 'https://...',
    'metadata' => [
        // Your custom metadata
    ],
]

Schema::create('payments', function (Blueprint $table) {
    $table->id();
    $table->string('chip_id')->unique();
    $table->morphs('payable');  // payable_id & payable_type
    $table->enum('status', ['pending', 'paid', 'failed', 'cancelled'])->default('pending');
    $table->string('email')->nullable();
    $table->decimal('amount', 12, 2);
    $table->string('currency', 3)->default('MYR');
    $table->string('reference')->nullable();
    $table->uuid('brand_id')->nullable();
    $table->json('payload')->nullable();
    $table->text('checkout_url')->nullable();
    $table->timestamp('paid_at')->nullable();
    $table->timestamps();
});
bash
php artisan vendor:publish --tag=chipin-config
bash
php artisan vendor:publish --tag=chipin-views