# Payeny PHP SDK (v1)

Official zero-dependency single-file PHP client and HMAC-SHA256 webhook signature verifier for the Payeny Unified Payment Orchestration API (`https://payeny.com/api/v1`).

## Downloads

- **Single-File Source (`PayenyClient.php`):** `https://payeny.com/sdk/php/PayenyClient.php`
- **ZIP Package (`payeny-php-sdk.zip`):** `https://payeny.com/sdk/php/payeny-php-sdk.zip`
- **README (`README.md`):** `https://payeny.com/sdk/php/README.md`

```bash
# Download single-file PHP SDK directly (no Composer registry package required)
curl -fsSL https://payeny.com/sdk/php/PayenyClient.php -o PayenyClient.php

# Or download the complete ZIP archive (PayenyClient.php + README.md)
curl -fsSL https://payeny.com/sdk/php/payeny-php-sdk.zip -o payeny-php-sdk.zip
```

## Quick Start

```php
<?php
require_once __DIR__ . '/PayenyClient.php';

use Payeny\PayenyClient;

$payeny = new PayenyClient(getenv('PAYENY_SECRET_KEY')); // sec_test_...

// 1. Customers
$customer = $payeny->customers->create([
    'name'  => 'Alex Rivera',
    'email' => 'alex@example.com',
]);

// 2. Payments & Refunds (test mode uses pm_card_* or tok_* tokens only)
$payment = $payeny->payments->create([
    'amount_cents'   => 1900,
    'currency'       => 'usd',
    'payment_method' => 'pm_card_visa',
    'customer_id'    => $customer['data']['id'],
]);
$refund = $payeny->refunds->create([
    'payment_id'   => $payment['data']['id'],
    'amount_cents' => 1900,
]);

// 3. Subscriptions: Create, Change Plan (Upgrade/Downgrade), Cancel & Resume
$sub = $payeny->subscriptions->create([
    'customer_id'       => $customer['data']['id'],
    'price_id'          => 'price_16467d816a24760887b4',
    'payment_method'    => 'pm_card_visa',
    'trial_period_days' => 14,
]);

$upgraded = $payeny->subscriptions->changePlan($sub['data']['id'], [
    'price_id'           => 'price_484a157e04a31b5a87d7',
    'proration_behavior' => 'always_invoice',
]);

$canceledAtPeriodEnd = $payeny->subscriptions->cancel($sub['data']['id'], [
    'cancel_at_period_end' => true,
]);

$resumed = $payeny->subscriptions->resume($sub['data']['id']);

$canceledNow = $payeny->subscriptions->cancel($sub['data']['id'], [
    'cancel_at_period_end' => false,
]);

// 4. Checkout Sessions & Invoices
$session = $payeny->checkoutSessions->create([
    'mode'         => 'payment',
    'amount_cents' => 2500,
    'currency'     => 'usd',
    'success_url'  => 'https://elept.com/billing/success',
    'cancel_url'   => 'https://elept.com/billing/cancel',
]);
$invoices = $payeny->invoices->list(['customer_id' => $customer['data']['id']]);

// 5. Webhook Endpoints & Signature Verification
$endpoint = $payeny->webhookEndpoints->create([
    'url'            => 'https://elept.com/webhooks/payeny',
    'enabled_events' => ['*'],
]);

$rawPayload = file_get_contents('php://input');
$sigHeader  = $_SERVER['HTTP_PAYENY_SIGNATURE'] ?? '';
$isValid    = PayenyClient::verifyWebhookSignature($rawPayload, $sigHeader, $endpoint['data']['secret']);
```
