Complete Example

const { TransFiPaymentAPI, TransFiError } = require("transfi-checkout");

const client = new TransFiPaymentAPI({
  publicKey: process.env.TRANSFI_PUBLIC_KEY,
  secretKey: process.env.TRANSFI_SECRET_KEY,
});

async function createInvoice() {
  try {
    const paymentUrl = await client.createPaymentInvoice({
      paymentLinkId: "your-payment-link-id",
      amount: "149.99",
      currency: "USD",
      customerOrderId: "order-" + Date.now(),
      productDetails: {
        name: "Premium Annual Plan",
        description: "12-month access to all premium features",
        imageUrl: "https://example.com/product-image.png",
      },
      individual: {
        firstName: "Jane",
        lastName: "Smith",
        email: "[email protected]",
        phone: "9876543210",
        phoneCode: "+1",
        country: "USA",
        address: {
          street: "123 Main St",
          city: "New York",
          state: "NY",
          postalCode: "10001",
        },
      },
      successRedirectUrl: "https://yourapp.com/payment/success",
      failureRedirectUrl: "https://yourapp.com/payment/failure",
    });

    console.log("Invoice created! Redirect user to:");
    console.log(paymentUrl);
  } catch (error) {
    if (error instanceof TransFiError) {
      console.error(`API Error [${error.statusCode}]: ${error.message}`);
      console.error("Details:", error.responseData);
    } else {
      console.error("Network or unexpected error:", error.message);
    }
  }
}

createInvoice();
from transfi import TransFiPaymentAPI, TransFiError
from transfi.types import PaymentInvoiceRequest, Individual, ProductDetails, Address
import time

api = TransFiPaymentAPI(
    public_key="YOUR_PUBLIC_KEY",
    secret_key="YOUR_SECRET_KEY",
)

request = PaymentInvoiceRequest(
    payment_link_id="your-payment-link-id",
    amount="149.99",
    currency="USD",
    customer_order_id=f"order-{int(time.time())}",
    product_details=ProductDetails(
        name="Premium Annual Plan",
        description="12-month access to all premium features",
        image_url="https://example.com/product-image.png",
    ),
    individual=Individual(
        first_name="Jane",
        last_name="Smith",
        email="[email protected]",
        phone="9876543210",
        phone_code="+1",
        country="US",
        address=Address(
            street="123 Main St",
            city="New York",
            state="NY",
            postal_code="10001",
        ),
    ),
    success_redirect_url="https://yourapp.com/payment/success",
    failure_redirect_url="https://yourapp.com/payment/failure",
)

try:
    payment_url = api.create_payment_invoice(request)
    print("Invoice created! Redirect user to:")
    print(payment_url)
except TransFiError as e:
    print(f"API Error [{e.status_code}]: {e}")
    print("Details:", e.response_data)
except Exception as e:
    print("Network or unexpected error:", e)
paymentInvoiceRequest request = paymentInvoiceRequest.builder()
    .paymentLinkId("YOUR_PAYMENT_LINK_ID")
    .amount("100.00")
    .currency("USD")
    .productDetails(productDetails.builder()
        .name("Premium Subscription")
        .description("Monthly subscription plan")
        .build())
    .individual(individual.builder()
        .firstName("John")
        .lastName("Doe")
        .email("[email protected]")
        .phone("1234567890")
        .phoneCode("+1")
        .country("US")
        .build())
    .successRedirectUrl("https://yoursite.com/success")
    .failureRedirectUrl("https://yoursite.com/failure")
    .customerOrderId("order-" + System.currentTimeMillis())
    .build();

try {
    String paymentUrl = api.createPaymentInvoice(request);
    System.out.println("Payment URL: " + paymentUrl);
    // Redirect user to paymentUrl to complete payment
} catch (transfiError e) {
    System.err.println("Error: " + e.getMessage());
    System.err.println("Status Code: " + e.getStatusCode());
    System.err.println("Response: " + e.getResponseData());
}
$api = new TransFiPaymentAPI([
    'publicKey' => 'YOUR_PUBLIC_KEY',
    'secretKey' => 'YOUR_SECRET_KEY',
]);

$product = new ProductDetails('Premium Plan');
$product->description = 'Monthly subscription';

$individual = new Individual('John', 'Doe', '[email protected]');
$individual->phone     = '1234567890';
$individual->phoneCode = '+1';
$individual->country   = 'US';

$paymentData = new PaymentInvoiceRequest(
    'LINK_ID',
    '100',
    'USD',
    'https://example.com/success',
    'https://example.com/failure'
);
$paymentData->productDetails  = $product;
$paymentData->individual      = $individual;
$paymentData->customerOrderId = 'order-001';

try {
    $paymentUrl = $api->createPaymentInvoice($paymentData);
    echo 'Checkout URL: ' . $paymentUrl . PHP_EOL;
} catch (TransFiError $e) {
    echo 'API Error   : ' . $e->getMessage() . PHP_EOL;
    echo 'Status Code : ' . $e->getStatusCode() . PHP_EOL;
    echo 'Response    : ' . json_encode($e->getResponseData(), JSON_PRETTY_PRINT) . PHP_EOL;
}

Python: Alternative dict syntax

The Python SDK also accepts a plain dict with camelCase keys matching the JSON API directly, if you prefer not to use the dataclasses:

payment_url = api.create_payment_invoice({
    "paymentLinkId": "your-payment-link-id",
    "amount": "100",
    "currency": "USD",
    "productDetails": {
        "name": "Premium Plan",
        "description": "Monthly subscription",
    },
    "individual": {
        "firstName": "John",
        "lastName": "Doe",
        "email": "[email protected]",
        "phone": "1234567890",
        "phoneCode": "+1",
        "country": "US",
    },
    "successRedirectUrl": "https://example.com/success",
    "failureRedirectUrl": "https://example.com/failure",
    "customerOrderId": "order-001",
})

Python: Context manager

The Python client supports the context manager protocol, which ensures the underlying HTTP session is properly closed:

with TransFiPaymentAPI(public_key="...", secret_key="...") as api:
    payment_url = api.create_payment_invoice(request)
    print(payment_url)

SDK Reference Summary

Node.js (transfi-checkout)Java(transfi-checkout)Python (transfi-sdk)PHP (transfi/payment-sdk)
Installnpm install transfi-checkoutMaven/Gradle via JitPack (com.github.Trans-Fi:transfi-checkout-java-sdk:v1.0.0)pip install transfi-sdkcomposer require transfi/payment-sdk
Importrequire("transfi-checkout")No explicit import needed (classes used directly)from transfi import ...use TransFi\TransFiPaymentAPI
Client classTransFiPaymentAPItransfiPaymentApiTransFiPaymentAPITransFiPaymentAPI
Create invoiceclient.createPaymentInvoice(data)api.createPaymentInvoice(request)api.create_payment_invoice(data)$api->createPaymentInvoice($data)
Error classTransFiErrortransfiErrorTransFiErrorTransFiError
Status codeerror.statusCodee.getStatusCode()e.status_code$e->getStatusCode()
Response bodyerror.responseDatae.getResponseData()e.response_data$e->getResponseData()
Context managerNoNoYes (with statement)No
Dict inputYes (camelCase keys)No (Builder pattern)Yes (camelCase keys)Yes (plain array, camelCase keys)
Typed inputNoYes (paymentInvoiceRequest, productDetails, individual builders)Yes (PaymentInvoiceRequest, etc.)Yes (PaymentInvoiceRequest, Individual, ProductDetails, Address)