Skip to main content

Creating Payments

When a customer completes checkout in WordPress and selects the Coinsnap payment method, the plugin creates a Coinsnap invoice and redirects the customer to the hosted payment page.


What the plugin does

  1. Customer clicks Place Order in WooCommerce checkout
  2. WooCommerce calls the plugin's process_payment() method
  3. Plugin calls the Coinsnap API to create an invoice
  4. Plugin redirects the customer to invoice.checkoutLink

The API call

From AbstractGateway::createInvoice() in the plugin source:

$client = new Invoice($this->apiHelper->url, $this->apiHelper->apiKey);

$invoice = $client->createInvoice(
$this->apiHelper->storeId, // Store ID from coinsnap_store_id option
strtoupper($order->get_currency()),
PreciseNumber::parseFloat((float) $order->get_total(), 2),
(string) $order->get_id(), // orderId
$order->get_billing_email(), // buyerEmail
$order->get_billing_first_name() . ' ' . $order->get_billing_last_name(), // buyerName
$this->get_return_url($order), // redirectUrl (WC thank-you page)
$metadata, // includes taxIncluded, orderNumber, coinsnapDiscount
$redirectAutomatically, // from coinsnap_autoredirect option (default: true)
$walletMessage
);

// Store invoice ID and checkout link against the order
$order->update_meta_data('Coinsnap_id', $invoice->getData()['id']);
$order->update_meta_data('Coinsnap_redirect', $invoice->getData()['checkoutLink']);
$order->save();

// Redirect customer to Coinsnap payment page
return [
'result' => 'success',
'redirect' => $invoice->getData()['checkoutLink'],
];

Required parameters

ParameterValueSource
amountOrder total$order->get_total()
currencyStore currencyget_woocommerce_currency()
orderIdWooCommerce order ID$order->get_id()
buyerEmailCustomer email$order->get_billing_email()
redirectUrlWooCommerce thank-you page$this->get_return_url($order)

Invoice ID storage

The plugin stores two meta values on the WooCommerce order after invoice creation:

Meta keyValue
Coinsnap_idThe Coinsnap invoice ID — used to match incoming webhook events
Coinsnap_redirectThe checkoutLink URL — used if the customer needs to be redirected again
Coinsnap_transactionurlLink to the invoice in the Coinsnap dashboard

The webhook handler looks up orders by Coinsnap_id:

$orders = wc_get_orders([
'meta_key' => 'Coinsnap_id',
'meta_value' => $postData->invoiceId,
]);

Order status after invoice creation

The order stays in its existing status (usually pending) until Coinsnap sends a webhook. Status transitions:

Webhook eventWooCommerce status (default)
Newpending — no change
Processingon-hold — full payment received, waiting for settlement
Settledcompleted — payment confirmed
Expiredcancelled — no payment received

All mappings are configurable per-store in WooCommerce → Settings → Coinsnap → Order States.


Custom integration (without a plugin)

If you need to create payments programmatically in a custom WordPress plugin or theme:

<?php
function coinsnap_create_invoice(WC_Order $order): array {
$api_key = get_option('coinsnap_api_key');
$store_id = get_option('coinsnap_store_id');

$response = wp_remote_post(
"https://app.coinsnap.io/api/v1/stores/{$store_id}/invoices",
[
'headers' => [
'x-api-key' => $api_key,
'Content-Type' => 'application/json',
],
'body' => json_encode([
'amount' => $order->get_total(),
'currency' => get_woocommerce_currency(),
'orderId' => (string) $order->get_id(),
'buyerEmail' => $order->get_billing_email(),
'redirectUrl' => wc_get_checkout_url(),
]),
]
);

if (is_wp_error($response)) {
throw new RuntimeException('Coinsnap API error: ' . $response->get_error_message());
}

return json_decode(wp_remote_retrieve_body($response), true);
}