PHP code example of sassydigitizing / whop-integration

1. Go to this page and download the library: Download sassydigitizing/whop-integration 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/ */

    

sassydigitizing / whop-integration example snippets


use SassyDigitizing\WhopIntegration\Logging\Logger;
use SassyDigitizing\WhopIntegration\Services\CurlHttpTransport;
use SassyDigitizing\WhopIntegration\Services\WhopApiClient;
use SassyDigitizing\WhopIntegration\Services\WhopWebhookVerifier;
use SassyDigitizing\WhopIntegration\Services\WhopProductService;
use SassyDigitizing\WhopIntegration\Services\WhopPlanService;
use SassyDigitizing\WhopIntegration\Services\WhopCheckoutService;
use SassyDigitizing\WhopIntegration\Services\WhopPaymentLookup;
use SassyDigitizing\WhopIntegration\Services\WhopInvoiceService;

$logger = new Logger('/path/to/whop.log');
$client = new WhopApiClient($apiKey, new CurlHttpTransport(), $logger);

// 1. Create a product once per item/category (cache the id yourself).
$products = new WhopProductService($client, $apiBase, $companyId);
$product  = $products->createProduct('Invoice #123');   // $product['id']

// 2. Dynamic price, no pre-made plan needed.
$plans = new WhopPlanService($client, $apiBase);
$plan  = $plans->createOneTimePlan($product['id'], 25.50, 'usd');

// 3. Create the hosted checkout session.
$checkout = new WhopCheckoutService($client, $checkoutBase);
$session  = $checkout->createCheckoutSession($plan['id'], $yourOrderRef, $returnUrl);
header('Location: ' . $session['purchase_url']);

// 4. In your webhook endpoint: verify before trusting anything.
$verifier = new WhopWebhookVerifier($webhookSecret);
if (!$verifier->verify($rawRequestBody, $lowercasedHeaders)) {
    http_response_code(401);
    exit;
}

// 5. Optional: re-confirm a specific payment server-side (e.g. on return).
$lookup  = new WhopPaymentLookup($client, $apiBase);
$payment = $lookup->getPayment($paymentId);
$isPaid  = ($payment['status'] ?? null) === 'paid';

// 6. Need an itemized breakdown instead of one price? Use WhopInvoiceService
// (Whop's native Invoices API) instead of steps 1-3 — it supports a real
// line_items array; checkout_sessions is always exactly one plan.
$invoices = new WhopInvoiceService($client, $apiBase, $companyId);
$invoice  = $invoices->createInvoice(
    lineItems: [
        ['label' => 'Order #4021', 'quantity' => 1, 'unit_price' => 25.00],
        ['label' => 'Order #4022', 'quantity' => 1, 'unit_price' => 40.00],
    ],
    currency: 'usd',
    emailAddress: $customerEmail,
    customerName: $customerName,
    dueDate: new DateTimeImmutable('+14 days'),
    productTitle: 'Orders #4021, #4022',
);
// $invoice['pay_online_url'] to send the customer; $invoice['id'] to check
// status later via $invoices->getInvoice($invoice['id'])['status'].
bash
SECRET="whsec_dGVzdHNlY3JldGtleWJ5dGVzMTIzNDU2"   # matches .env's WHOP_WEBHOOK_SECRET
BODY='{"id":"evt_1","type":"payment.succeeded","data":{"id":"pay_999","status":"paid","total":9.99,"currency":"usd","plan":{"id":"plan_demo1"},"membership":{"id":"mem_999"},"metadata":{"order_ref":"demo-order-1"}}}'
ID="wh_demo1"
TS=$(date +%s)
KEY=$(php -r "echo base64_decode(substr('$SECRET',6));")
SIG="v1,$(php -r "echo base64_encode(hash_hmac('sha256', '$ID.$TS.$BODY', '$KEY', true));")"

curl -i -X POST http://localhost:8080/webhooks/whop.php \
  -H "webhook-id: $ID" -H "webhook-timestamp: $TS" -H "webhook-signature: $SIG" \
  -H "Content-Type: application/json" -d "$BODY"