PHP code example of devmohy / creem-laravel

1. Go to this page and download the library: Download devmohy/creem-laravel 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/ */

    

devmohy / creem-laravel example snippets


return [
    'api_key' => env('CREEM_API_KEY'),
    'webhook_secret' => env('CREEM_WEBHOOK_SECRET'),
    'api_base_url' => env('CREEM_API_BASE_URL', 'https://api.creem.io/v1'),
];

use Creem\CreemLaravel\Facades\Creem;

$response = Creem::createCheckout([
    'product_id' => 'prod_12345',
    'success_url' => route('checkout.success'),
    'cancel_url' => route('checkout.cancel'),
    'customer' => [
        'email' => '[email protected]',
    ],
]);

if ($response->successful()) {
    $checkoutUrl = $response->json('checkout_url');
    return redirect($checkoutUrl);
}

$product = Creem::getProduct('prod_12345');
$allProducts = Creem::getProducts();

$products = Creem::searchProducts([
    'limit' => 10,
    'page' => 1,
    // other filters supported by CREEM API
]);

// Get a customer
$customer = Creem::getCustomer('cus_123');

// List customers
$customers = Creem::listCustomers([
    'page_size' => 10,
    'page_number' => 1,
]);

// Generate Billing Portal Link
$response = Creem::createPortalLink([
    'customer_id' => 'cus_123',
]);

return redirect($response->json('customer_portal_link'));

// Retrieve a subscription
$subscription = Creem::getSubscription('sub_123');

// Cancel a subscription
Creem::cancelSubscription('sub_123');

// Pause a subscription
Creem::pauseSubscription('sub_123');

// Update a subscription (e.g., change quantity)
Creem::updateSubscription('sub_123', [
    'items' => [
        ['id' => 'item_123', 'units' => 5]
    ]
]);

// Upgrade a subscription
Creem::upgradeSubscription('sub_123', [
    'product_id' => 'prod_premium',
    'update_behavior' => 'proration-charge-immediately',
]);

// Validate a license key
$license = Creem::validateLicense('ABC-123-XYZ', 'instance_unique_id');

if ($license->json('status') === 'active') {
    // Grant access
}

// Activate a license
Creem::activateLicense('ABC-123-XYZ', 'My Macbook Pro');

// Deactivate a license
Creem::deactivateLicense('ABC-123-XYZ', 'instance_unique_id');

// Get a transaction
$transaction = Creem::getTransaction('txn_123');

// List transactions
$transactions = Creem::listTransactions([
    'customer_id' => 'cus_123',
    'status' => 'succeeded',
]);

// Retrieve a discount by ID or Code
$discount = Creem::getDiscount('SAVE10');

// Create a discount
Creem::createDiscount([
    'name' => 'Black Friday',
    'code' => 'BF2024',
    'type' => 'percentage',
    'percentage' => 20,
    'duration' => 'once',
    'applies_to_products' => ['prod_123'],
]);

// Delete a discount
Creem::deleteDiscount('disc_123');

Route::post('/creem/webhook', \Creem\CreemLaravel\Http\Controllers\WebhookController::class)
    ->middleware(\Creem\CreemLaravel\Http\Middleware\VerifyWebhookSignature::class);

protected $listen = [
    \Creem\CreemLaravel\Events\CheckoutSucceeded::class => [
        \App\Listeners\HandleSuccessfulPayment::class,
    ],
];

namespace App\Listeners;

use Creem\CreemLaravel\Events\CheckoutSucceeded;
use Illuminate\Support\Facades\Log;

class HandleSuccessfulPayment
{
    public function handle(CheckoutSucceeded $event): void
    {
        $payload = $event->payload;
        $customerId = $payload['customer_id'] ?? null;

        Log::info("Payment received for customer: {$customerId}");

        // Grant access, update database, email user, etc.
    }
}
bash
php artisan vendor:publish --tag="creem-config"
bash
php artisan creem:webhook-secret
bash
php artisan creem:sync-products
bash
cd demo
composer install
cp .env.example .env
php artisan key:generate
php artisan serve