Chuyển đến nội dung chính
SDK PHP cung cấp một cách mạnh mẽ và linh hoạt để tích hợp Dodo Payments vào các ứng dụng PHP của bạn. Được xây dựng theo các tiêu chuẩn PHP hiện đại với autoloading PSR-4, nó cung cấp độ phủ kiểm tra rộng rãi và tài liệu chi tiết.
Thư viện API PHP Dodo Payments hiện đang ở beta. Chúng tôi rất hào hứng cho bạn thử nghiệm với nó! Vui lòng chia sẻ bất kỳ đề xuất, báo cáo lỗi hoặc yêu cầu tính năng nào bằng cách gửi một vấn đề.

Cài đặt

Cài đặt SDK bằng Composer:
composer require "dodopayments/client:^3.5.0"
SDK yêu cầu PHP 8.1.0 hoặc cao hơn và Composer để quản lý phụ thuộc.

Bắt đầu nhanh

Khởi tạo client và tạo một phiên giao dịch thanh toán:
<?php

use Dodopayments\Client;

$client = new Client(
  bearerToken: getenv("DODO_PAYMENTS_API_KEY") ?: "My Bearer Token",
  environment: "test_mode",
);

$checkoutSessionResponse = $client->checkoutSessions->create(
  productCart: [["productID" => "product_id", "quantity" => 1]]
);

var_dump($checkoutSessionResponse->session_id);
Lưu trữ các khóa API của bạn một cách an toàn bằng cách sử dụng biến môi trường. Không bao giờ để lộ chúng trong mã nguồn của bạn hoặc cam kết chúng vào hệ thống kiểm soát phiên bản.

Tính năng chính

Tuân thủ PSR-4

Tuân theo các Khuyến nghị về Tiêu chuẩn PHP cho phát triển PHP hiện đại

PHP hiện đại

Được xây dựng cho PHP 8.1+ với khai báo kiểu và kiểu nghiêm ngặt

Kiểm tra toàn diện

Độ phủ kiểm tra toàn diện cho độ tin cậy và ổn định

Xử lý ngoại lệ

Các loại ngoại lệ rõ ràng cho các kịch bản lỗi khác nhau

Đối tượng giá trị

SDK sử dụng các tham số có tên để chỉ định các đối số tùy chọn. Bạn có thể khởi tạo các đối tượng giá trị bằng cách sử dụng hàm khởi tạo tĩnh with:
<?php

use Dodopayments\Customers\AttachExistingCustomer;

// Recommended: Use static 'with' constructor with named parameters
$customer = AttachExistingCustomer::with(customerID: "customer_id");
Các builder cũng có sẵn như một mẫu thay thế:
<?php

use Dodopayments\Customers\AttachExistingCustomer;

// Alternative: Use builder pattern
$customer = (new AttachExistingCustomer)->withCustomerID("customer_id");

Cấu hình

Cấu hình thử lại

Một số lỗi sẽ tự động được thử lại 2 lần theo mặc định với một khoảng thời gian chờ ngắn. Các lỗi sau đây kích hoạt thử lại tự động:
  • Lỗi kết nối (vấn đề kết nối mạng)
  • 408 Thời gian yêu cầu hết hạn
  • 409 Xung đột
  • 429 Giới hạn tỷ lệ
  • 500+ Lỗi nội bộ
  • Thời gian chờ
Cấu hình hành vi thử lại toàn cục hoặc theo yêu cầu:
<?php

use Dodopayments\Client;
use Dodopayments\RequestOptions;

// Configure default for all requests (disable retries)
$client = new Client(maxRetries: 0);

// Or, configure per-request
$result = $client->checkoutSessions->create(
  productCart: [["productID" => "product_id", "quantity" => 1]],
  requestOptions: RequestOptions::with(maxRetries: 5),
);

Các thao tác phổ biến

Tạo một phiên giao dịch thanh toán

Tạo một phiên giao dịch thanh toán:
$session = $client->checkoutSessions->create(
  productCart: [
    ["productID" => "prod_123", "quantity" => 1]
  ],
  returnUrl: "https://yourdomain.com/return"
);

header('Location: ' . $session->url);

Quản lý khách hàng

Tạo và truy xuất thông tin khách hàng:
// Create a customer
$customer = $client->customers->create(
  email: "[email protected]",
  name: "John Doe",
  metadata: [
    "user_id" => "12345"
  ]
);

// Retrieve customer
$customer = $client->customers->retrieve("cus_123");
echo "Customer: {$customer->name} ({$customer->email})";

Xử lý đăng ký

Tạo và quản lý các đăng ký định kỳ:
// Create a subscription
$subscription = $client->subscriptions->create(
  customerID: "cus_123",
  productID: "prod_456",
  priceID: "price_789"
);

// Cancel subscription
$client->subscriptions->cancel($subscription->id);

Phân trang

Làm việc với các phản hồi danh sách phân trang:
$page = $client->payments->list();

var_dump($page);

// Fetch items from the current page
foreach ($page->getItems() as $item) {
  var_dump($item->brand_id);
}

// Auto-paginate: fetch items from all pages
foreach ($page->pagingEachItem() as $item) {
  var_dump($item->brand_id);
}

Xử lý lỗi

Khi thư viện không thể kết nối với API hoặc nhận được mã trạng thái không thành công (4xx hoặc 5xx), một lớp con của APIException sẽ được ném ra:
<?php

use Dodopayments\Core\Exceptions\APIConnectionException;
use Dodopayments\Core\Exceptions\RateLimitException;
use Dodopayments\Core\Exceptions\APIStatusException;

try {
  $checkoutSessionResponse = $client->checkoutSessions->create(
    productCart: [["productID" => "product_id", "quantity" => 1]]
  );
} catch (APIConnectionException $e) {
  echo "The server could not be reached", PHP_EOL;
  var_dump($e->getPrevious());
} catch (RateLimitException $_) {
  echo "A 429 status code was received; we should back off a bit.", PHP_EOL;
} catch (APIStatusException $e) {
  echo "Another non-200-range status code was received", PHP_EOL;
  echo $e->getMessage();
}

Các loại lỗi

Nguyên nhânLoại lỗi
HTTP 400BadRequestException
HTTP 401AuthenticationException
HTTP 403PermissionDeniedException
HTTP 404NotFoundException
HTTP 409ConflictException
HTTP 422UnprocessableEntityException
HTTP 429RateLimitException
HTTP >= 500InternalServerException
Lỗi HTTP khácAPIStatusException
Thời gian chờAPITimeoutException
Lỗi mạngAPIConnectionException
Luôn bọc các cuộc gọi API trong các khối try-catch để xử lý các lỗi tiềm ẩn một cách duyên dáng và cung cấp phản hồi có ý nghĩa cho người dùng.

Sử dụng nâng cao

Các điểm cuối không tài liệu

Thực hiện các yêu cầu đến các điểm cuối không tài liệu:
<?php

$response = $client->request(
  method: "post",
  path: '/undocumented/endpoint',
  query: ['dog' => 'woof'],
  headers: ['useful-header' => 'interesting-value'],
  body: ['hello' => 'world']
);

Các tham số không tài liệu

Gửi các tham số không tài liệu đến bất kỳ điểm cuối nào hoặc đọc các thuộc tính phản hồi không tài liệu:
<?php

use Dodopayments\RequestOptions;

$checkoutSessionResponse = $client->checkoutSessions->create(
  productCart: [["productID" => "product_id", "quantity" => 1]],
  requestOptions: RequestOptions::with(
    extraQueryParams: ["my_query_parameter" => "value"],
    extraBodyParams: ["my_body_parameter" => "value"],
    extraHeaders: ["my-header" => "value"],
  ),
);
Các tham số extra* có cùng tên sẽ ghi đè lên các tham số đã được tài liệu.

Tích hợp Framework

Laravel

Tạo một dịch vụ cho các ứng dụng Laravel:
<?php

namespace App\Services;

use Dodopayments\Client;

class PaymentService
{
    protected $client;

    public function __construct()
    {
        $this->client = new Client(
            bearerToken: config('services.dodo.api_key')
        );
    }

    public function createCheckout(array $items)
    {
        return $this->client->checkoutSessions->create(
            productCart: $items,
            returnUrl: route('checkout.return')
        );
    }
}
Thêm cấu hình vào config/services.php:
'dodo' => [
    'api_key' => env('DODO_API_KEY'),
    'environment' => env('DODO_ENVIRONMENT', 'sandbox')
],

Symfony

Tạo một dịch vụ trong Symfony:
<?php

namespace App\Service;

use Dodopayments\Client;

class DodoPaymentService
{
    private Client $client;

    public function __construct(string $apiKey)
    {
        $this->client = new Client(bearerToken: $apiKey);
    }

    public function createPayment(int $amount, string $currency, string $customerId): object
    {
        return $this->client->payments->create(
            amount: $amount,
            currency: $currency,
            customerID: $customerId
        );
    }
}
Đăng ký trong config/services.yaml:
services:
    App\Service\DodoPaymentService:
        arguments:
            $apiKey: '%env(DODO_API_KEY)%'

Tài nguyên

Hỗ trợ

Cần trợ giúp với SDK PHP?

Đóng góp

Chúng tôi hoan nghênh các đóng góp! Kiểm tra hướng dẫn đóng góp để bắt đầu.