Create an outgoing payment
Esta página aún no está disponible en tu idioma.
After a quote is accepted and the authorized client obtains the requisite grant from the authorization server of the sender’s ASE Account servicing entity , the client can create an outgoing payment resource against the sender’s wallet address.
These code snippets create an outgoing payment to a given wallet address. The amount of the outgoing payment is based on the amounts in the associated quote.
Before you begin
Section titled “Before you begin”We recommend creating a wallet account on the test wallet. Creating an account allows you to test your client against the Open Payments APIs by using an ILP-enabled wallet funded with play money.
Create an outgoing payment resource
Section titled “Create an outgoing payment resource”Initial configuration
If you’re using JavaScript, only do the first step.
- Add
"type": "module"topackage.json. - Add the following to
tsconfig.json{"compilerOptions": {"target": "ES2022","module": "ES2022"}}
Import dependencies
import { createAuthenticatedClient } from "@interledger/open-payments"; Copied! Initialize Open Payments client
const client = await createAuthenticatedClient({
walletAddressUrl: WALLET_ADDRESS,
privateKey: PRIVATE_KEY_PATH,
keyId: KEY_ID,
}); Copied! Create outgoing payment
const outgoingPayment = await client.outgoingPayment.create(
{
url: new URL(WALLET_ADDRESS).origin,
accessToken: OUTGOING_PAYMENT_ACCESS_TOKEN,
},
{
walletAddress: WALLET_ADDRESS,
quoteId: QUOTE_URL,
},
); Copied! Output
console.log("OUTGOING_PAYMENT_URL = ", outgoingPayment.id); Copied! For TypeScript, run tsx path/to/directory/index.ts. View full TS source
For JavaScript, run node path/to/directory/index.js. View full JS source
Import dependencies
use open_payments::client::api::AuthenticatedResources;
use open_payments::client::utils::get_resource_server_url;
use open_payments::snippets::utils::{create_authenticated_client, get_env_var, load_env};
use open_payments::types::OutgoingPaymentRequest; Copied! Initialize Open Payments client
let client = create_authenticated_client()?; Copied! Prepare outgoing payment request
let access_token = get_env_var("OUTGOING_PAYMENT_ACCESS_TOKEN")?;
let quote_url = get_env_var("QUOTE_URL")?;
let wallet_address_url = get_env_var("WALLET_ADDRESS_URL")?;
let resource_server_url = get_resource_server_url(&wallet_address_url)?;
let request = OutgoingPaymentRequest::FromQuote {
wallet_address: wallet_address_url,
quote_id: quote_url,
metadata: None,
}; Copied! Create outgoing payment
println!(
"Outgoing payment create request JSON: {}",
serde_json::to_string_pretty(&request)?
);
let payment = client
.outgoing_payments()
.create(&resource_server_url, &request, Some(&access_token))
.await?; Copied! Output
println!("Created outgoing payment: {payment:#?}"); Copied! Import dependencies
use OpenPayments\AuthClient;
use OpenPayments\Config\Config; Copied! Initialize Open Payments client
$config = new Config($WALLET_ADDRESS, $PRIVATE_KEY, $KEY_ID);
$opClient = new AuthClient($config); Copied! Without amount
Section titled “Without amount”Create outgoing payment
$newOutgoingPayment = $opClient->outgoingPayment()->create(
[
"url" => $wallet->resourceServer,
"access_token" => $OUTGOING_PAYMENT_GRANT_ACCESS_TOKEN,
],
[
"walletAddress" => $config->getWalletAddressUrl(),
"quoteId" => $QUOTE_URL,
"metadata" => [
"description" => "Test outgoing payment",
"reference" => "1234567890",
"invoiceId" => "1234567890",
"customData" => [
"key1" => "value1",
"key2" => "value2",
],
],
],
); Copied! With amount
Section titled “With amount”Create outgoing payment with amount
$newOutgoingPayment = $opClient->outgoingPayment()->create(
[
"url" => $wallet->resourceServer,
"access_token" => $OUTGOING_PAYMENT_GRANT_ACCESS_TOKEN,
],
[
"walletAddress" => $config->getWalletAddressUrl(),
"incomingPayment" => $INCOMING_PAYMENT_URL,
"debitAmount" => [
"value" => "121",
"assetCode" => "USD",
"assetScale" => 2,
],
"metadata" => [
"description" => "Test outgoing payment",
"reference" => "1234567890",
"invoiceId" => "1234567890",
"customData" => [
"key1" => "value1",
"key2" => "value2",
],
],
],
); Copied! Output
$output->writeln("OUTGOING_PAYMENT_URL " . $newOutgoingPayment->id); Copied!