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, 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 dependenciesimport { createAuthenticatedClient } from '@interledger/open-payments'
// Initialize clientconst client = await createAuthenticatedClient({ walletAddressUrl: WALLET_ADDRESS, privateKey: PRIVATE_KEY_PATH, keyId: KEY_ID})
// Get wallet address informationconst walletAddress = await client.walletAddress.get({ url: WALLET_ADDRESS})
// Create outgoing paymentconst outgoingPayment = await client.outgoingPayment.create( { url: walletAddress.resourceServer, accessToken: OUTGOING_PAYMENT_ACCESS_TOKEN }, { walletAddress: WALLET_ADDRESS, quoteId: QUOTE_URL })
// Outputconsole.log('OUTGOING_PAYMENT_URL = ', outgoingPayment.id)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 dependenciesuse 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;
// Initialize clientlet client = create_authenticated_client()?;
// Prepare outgoing payment requestlet 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,};
// Create outgoing paymentprintln!( "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?;
// Outputprintln!("Created outgoing payment: {payment:#?}");With quote
Section titled “With quote”// Import dependenciesuse OpenPayments\AuthClient;use OpenPayments\Config\Config;
// Initialize client$config = new Config( $WALLET_ADDRESS, $PRIVATE_KEY, $KEY_ID);$opClient = new AuthClient($config);
// 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' ] ], ]);
// Outputecho 'OUTGOING_PAYMENT_URL: '.$newOutgoingPayment->id . PHP_EOL;echo 'OUTGOING_PAYMENT OBJECT: ' . PHP_EOL . print_r($newOutgoingPayment, true) . PHP_EOL;With incoming payment
Section titled “With incoming payment”// Import dependenciesuse OpenPayments\AuthClient;use OpenPayments\Config\Config;
// Initialize client$config = new Config( $WALLET_ADDRESS, $PRIVATE_KEY, $KEY_ID);$opClient = new AuthClient($config);
// 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 dependenciesimport ( "context" "encoding/json" "fmt" "log"
op "github.com/interledger/open-payments-go" rs "github.com/interledger/open-payments-go/generated/resourceserver"
)
func main() { // Initialize 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)}// Import dependenciesimport org.interledger.openpayments.httpclient.OpenPaymentsHttpClient;import org.interledger.openpayments.IOpenPaymentsClient;
// Initialize clientvar client = OpenPaymentsHttpClient.defaultClient("WalletAddress","PrivateKeyPEM","KeyId");
// Get wallet address informationvar receiverWallet = client.getWalletAddress("https://cloudninebank.example.com/merchant");var senderWallet = client.getWalletAddress("https://cloudninebank.example.com/customer");
// Create incoming paymentvar grantRequest = this.client.createGrantIncomingPayment(receiverWallet);var incomingPayment = this.client.createIncomingPayment(receiverWallet, grantRequest, BigDecimal.valueOf(11.25));
// Create quotevar quoteRequest = this.client.createGrantQuote(senderWallet);var quote = client.createQuote(quoteRequest.getAccess().getToken(), senderWallet, incomingPayment, Optional.empty(), Optional.empty());
// Create outing payment from quotevar urlToOpen = "http://localhost:%d?paymentId=1234".formatted(port);
var opContinueInteract = this.client.createGrantContinuation(senderWallet,quote.getDebitAmount(),URI.create(urlToOpen),"test");
// USER APPROVES REQUEST
// Grant: Finalize/Continuevar finalized = this.client.createGrantFinalize(opContinueInteract, "Reference from USER interaction.");
// Outgoing payment (The token will be extracted from [finalized])var finalizedOutgoingPayment = this.client.createFinalizePayment(finalized, senderWallet, quote);
// Outputlog.info("OUTGOING_PAYMENT: {}", finalizedOutgoingPayment);