One of the most common use cases when manipulating money is payment. Many services such as Stripe helps you process orders and payments programmatically.
Such solutions integrate well with Dinero.js. If you're building an application that manipulates and charges money, you can use Dinero objects to represent prices and write small connectors for your payment service. Most payment services represent money in minor units, making it straightforward to turn a Dinero object into a payment.
The following code is purely illustrative. Make sure to test it out in your application.
Copy linkIntegrating with Stripe
The Stripe payment platform provides APIs to process payments and manage orders. Like many other platforms, it expects money representations with an amount in minor currency units, and a currency as an ISO 4217 currency code.
When using Stripe's Node.js client, the currency must be in lowercase.
import stripe from 'stripe';
import { dinero, toSnapshot } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
function toStripeMoney(dineroObject) {
const { amount, currency } = toSnapshot(dineroObject);
return { amount, currency: currency.code.toLowerCase() };
}
// ... Stripe client setup
const price = dinero({ amount: 2000, currency: USD });
const response = await client.charges.create({
// ...
...toStripeMoney(price),
});
Copy linkIntegrating with Paypal
The Paypal payment platform provides APIs to process payments and manage orders. Unlike most platforms, it expects a money representation with an amount in major currency units. You can use toFormat
to retrieve this value.
const paypal = require('@paypal/checkout-server-sdk');
const { dinero, toSnapshot, toFormat } = require('dinero.js');
const { USD } = require('@dinero.js/currencies');
function toPaypalMoney(dineroObject) {
const { currency, scale } = toSnapshot(dineroObject);
return {
value: toFormat(dineroObject, ({ decimal }) => decimal),
currency_code: currency.code,
};
}
const price = dinero({ amount: 2000, currency: USD });
let request = new paypal.orders.OrdersCreateRequest();
request.requestBody({
// ...
purchase_units: [
{
amount: toPaypalMoney(price),
},
],
});
Copy linkIntegrating with Adyen
The Adyen payment platform provides APIs to process payments and manage orders. Like many other platforms, it expects money representations with an amount in minor currency units and a currency as an ISO 4217 currency code.
import { Client, Config, CheckoutAPI } from '@adyen/api-library';
import { dinero, toSnapshot } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
function toAdyenMoney(dineroObject) {
const { amount, currency } = toSnapshot(dineroObject);
return { value: amount, currency: currency.code };
};
// ... Adyen client setup
const price = dinero({ amount: 2000, currency: USD });
const response = await checkout.paymentMethods({
// ...
amount: toAdyenMoney(price),
});
Copy linkIntegrating with Square
The Square digital payment platform provides APIs to process payments and manage orders. Like many other platforms, it expects money representations with an amount in minor currency units and a currency as an ISO 4217 currency code.
When using Square's Node.js client, the amount must be of type bigint
. If you're using Dinero.js with the number
calculator (default behavior), you can cast the amount into a bigint
when transforming your Dinero object into a Square Money
object. Otherwise, if you're using Dinero with the bigint
calculator, you can pass the amount directly.
import { Client } from 'square';
import { dinero, toSnapshot } from 'dinero.js';
import { USD } from '@dinero.js/currencies';
function toSquareMoney(dineroObject) {
const { amount, currency } = toSnapshot(dineroObject);
return { amount: BigInt(amount), currency: currency.code };
}
// ... Square client setup
const price = dinero({ amount: 2000, currency: USD });
const response = await client.paymentsApi.createPayment({
// ...
amountMoney: toSquareMoney(price),
});