Create an outgoing payment
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! Get wallet address information
const walletAddress = await client.walletAddress.get({
url: WALLET_ADDRESS,
}); Copied! Create outgoing payment
const outgoingPayment = await client.outgoingPayment.create(
{
url: walletAddress.resourceServer,
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 dependenciesuse OpenPayments\AuthClient;use OpenPayments\Config\Config;
// Initialize Open Payments client$config = new Config( $WALLET_ADDRESS, $PRIVATE_KEY, $KEY_ID);$opClient = new AuthClient($config);With quote
Section titled “With quote”// Create outgoing payment without amount$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' ] ], ]);With incoming payment
Section titled “With incoming payment”// 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' => '9', 'assetCode' => 'USD', 'assetScale' => 2 ], 'metadata' => [ 'description' => 'Test outgoing payment', 'reference' => '1234567890', 'invoiceId' => '1234567890', 'customData' => [ 'key1' => 'value1', 'key2' => 'value2' ] ], ]);// Outputecho 'OUTGOING_PAYMENT_URL: '.$newOutgoingPayment->id . PHP_EOL;echo 'OUTGOING_PAYMENT OBJECT: ' . PHP_EOL . print_r($newOutgoingPayment, true) . PHP_EOL;package main
import ( "context" "encoding/json" "fmt" "log"
op "github.com/interledger/open-payments-go" rs "github.com/interledger/open-payments-go/generated/resourceserver"
)
func main() { // Initialize Open Payments client client, err := op.NewAuthenticatedClient(WALLET_ADDRESS_URL, PRIVATE_KEY_BASE_64, KEY_ID) if err != nil { log.Fatalf("Error creating authenticated client: %v\n", err) }
// Create outgoing payment var payload rs.CreateOutgoingPaymentRequest if err := payload.FromCreateOutgoingPaymentWithQuote(rs.CreateOutgoingPaymentWithQuote{ WalletAddressSchema: WALLET_ADDRESS_URL, QuoteId: QUOTE_URL, }); err != nil { log.Fatalf("Error creating payload: %v\n", err) }
outgoingPayment, err := client.OutgoingPayment.Create(context.TODO(), op.OutgoingPaymentCreateParams{ BaseURL: RESOURCE_SERVER_URL, AccessToken: OUTGOING_PAYMENT_ACCESS_TOKEN, Payload: payload, }) if err != nil { log.Fatalf("Error creating outgoing payment: %v\n", err) }
// Output outgoingPaymentJSON, err := json.MarshalIndent(outgoingPayment, "", " ") if err != nil { log.Fatalf("Error marshaling outgoing payment: %v\n", err) } fmt.Println("OUTGOING PAYMENT:", string(outgoingPaymentJSON)) fmt.Println("OUTGOING_PAYMENT_URL:", *outgoingPayment.Id)}