#![allow(clippy::all, unused_imports, dead_code)]
use crate::{Actor, ActorBehavior, ClientBuilderExt, Message, Port};
use anyhow::{Error, Result};
use reflow_actor::{message::EncodableValue, ActorContext};
use reflow_actor_macro::actor;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::time::Duration;
const BASE_URL: &str = "https://checkout-test.adyen.com/v70";
const ENV_KEY: &str = "ADYEN_API_KEY";
fn apply_auth(
config: &reflow_actor::ActorConfig,
mut builder: reqwest::RequestBuilder,
) -> Result<reqwest::RequestBuilder> {
let credential = config
.get_config_or_env(ENV_KEY)
.ok_or_else(|| anyhow::anyhow!("Missing env var: {}", ENV_KEY))?;
builder = builder.header("X-API-Key", &credential);
Ok(builder)
}
#[actor(
AdyenCreatePaymentActor,
inports::<100>(amount, merchantAccount, reference),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/payments".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /payments failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenCreatePaymentSessionActor,
inports::<100>(amount, merchantAccount, reference),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment_session(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/sessions".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /sessions failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenCreatePaymentDetailsActor,
inports::<100>(details, paymentData, sessionData, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment_details(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/payments/details".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("details") {
body.insert("details".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("paymentData") {
body.insert("paymentData".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("sessionData") {
body.insert("sessionData".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /payments/details failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenListPaymentMethodsActor,
inports::<100>(merchantAccount, amount, countryCode, shopperLocale, channel, shopperReference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_list_payment_methods(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/paymentMethods".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("countryCode") {
body.insert("countryCode".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("shopperLocale") {
body.insert("shopperLocale".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("channel") {
body.insert("channel".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("shopperReference") {
body.insert("shopperReference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /paymentMethods failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenCreatePaymentCaptureActor,
inports::<100>(paymentPspReference, merchantAccount, amount, reference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment_capture(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/payments/{paymentPspReference}/captures".to_string();
if let Some(val) = inputs.get("paymentPspReference") {
endpoint = endpoint.replace("{{paymentPspReference}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"POST /payments/{{paymentPspReference}}/captures failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
AdyenCreatePaymentRefundActor,
inports::<100>(paymentPspReference, merchantAccount, amount, reference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment_refund(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/payments/{paymentPspReference}/refunds".to_string();
if let Some(val) = inputs.get("paymentPspReference") {
endpoint = endpoint.replace("{{paymentPspReference}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"POST /payments/{{paymentPspReference}}/refunds failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
AdyenCreatePaymentReversalActor,
inports::<100>(paymentPspReference, merchantAccount, reference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment_reversal(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/payments/{paymentPspReference}/reversals".to_string();
if let Some(val) = inputs.get("paymentPspReference") {
endpoint = endpoint.replace("{{paymentPspReference}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"POST /payments/{{paymentPspReference}}/reversals failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
AdyenCreatePaymentCancellationActor,
inports::<100>(paymentPspReference, merchantAccount, reference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment_cancellation(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/payments/{paymentPspReference}/cancels".to_string();
if let Some(val) = inputs.get("paymentPspReference") {
endpoint = endpoint.replace("{{paymentPspReference}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"POST /payments/{{paymentPspReference}}/cancels failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
AdyenUpdatePaymentAmountActor,
inports::<100>(paymentPspReference, merchantAccount, amount, reference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_update_payment_amount(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/payments/{paymentPspReference}/amountUpdates".to_string();
if let Some(val) = inputs.get("paymentPspReference") {
endpoint = endpoint.replace("{{paymentPspReference}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.patch(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!(
"PATCH /payments/{{paymentPspReference}}/amountUpdates failed: {}",
e
)
.into(),
),
);
}
}
Ok(output)
}
#[actor(
AdyenCreatePaymentLinkActor,
inports::<100>(merchantAccount, amount, reference, expiresAt, shopperReference, description, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_payment_link(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/paymentLinks".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("expiresAt") {
body.insert("expiresAt".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("shopperReference") {
body.insert("shopperReference".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("description") {
body.insert("description".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /paymentLinks failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenReadPaymentLinkActor,
inports::<100>(linkId, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_read_payment_link(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/paymentLinks/{linkId}".to_string();
if let Some(val) = inputs.get("linkId") {
endpoint = endpoint.replace("{{linkId}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /paymentLinks/{{linkId}} failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenUpdatePaymentLinkActor,
inports::<100>(linkId, status, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_update_payment_link(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/paymentLinks/{linkId}".to_string();
if let Some(val) = inputs.get("linkId") {
endpoint = endpoint.replace("{{linkId}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.patch(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("status") {
body.insert("status".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("PATCH /paymentLinks/{{linkId}} failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenCreateOrderActor,
inports::<100>(merchantAccount, amount, reference, expiresAt, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_order(context: ActorContext) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/orders".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("expiresAt") {
body.insert("expiresAt".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /orders failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenDeleteOrderActor,
inports::<100>(merchantAccount, order, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_delete_order(context: ActorContext) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/orders/cancel".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("order") {
body.insert("order".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /orders/cancel failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenReadPaymentResultActor,
inports::<100>(payload, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_read_payment_result(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/payments/result".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("payload") {
body.insert("payload".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /payments/result failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenCreateStoredPaymentMethodActor,
inports::<100>(merchantAccount, shopperReference, paymentMethod, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_stored_payment_method(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/storedPaymentMethods".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("shopperReference") {
body.insert("shopperReference".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("paymentMethod") {
body.insert("paymentMethod".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /storedPaymentMethods failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenListStoredPaymentMethodsActor,
inports::<100>(merchantAccount, shopperReference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_list_stored_payment_methods(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/storedPaymentMethods".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.get(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("merchantAccount") {
query_pairs.push(("merchantAccount", super::message_to_str(val)));
}
if let Some(val) = inputs.get("shopperReference") {
query_pairs.push(("shopperReference", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("GET /storedPaymentMethods failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenDeleteStoredPaymentMethodActor,
inports::<100>(recurringId, merchantAccount, shopperReference, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_delete_stored_payment_method(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let mut endpoint = "/storedPaymentMethods/{recurringId}".to_string();
if let Some(val) = inputs.get("recurringId") {
endpoint = endpoint.replace("{{recurringId}}", &super::message_to_str(val));
}
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.delete(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
let mut query_pairs: Vec<(&str, String)> = Vec::new();
if let Some(val) = inputs.get("merchantAccount") {
query_pairs.push(("merchantAccount", super::message_to_str(val)));
}
if let Some(val) = inputs.get("shopperReference") {
query_pairs.push(("shopperReference", super::message_to_str(val)));
}
if !query_pairs.is_empty() {
builder = builder.query(&query_pairs);
}
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(
format!("DELETE /storedPaymentMethods/{{recurringId}} failed: {}", e).into(),
),
);
}
}
Ok(output)
}
#[actor(
AdyenCreateDonationActor,
inports::<100>(merchantAccount, amount, reference, donationToken, donationAccount, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_donation(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/donations".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("amount") {
body.insert("amount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("reference") {
body.insert("reference".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("donationToken") {
body.insert("donationToken".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("donationAccount") {
body.insert("donationAccount".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /donations failed: {}", e).into()),
);
}
}
Ok(output)
}
#[actor(
AdyenCreateApplePaySessionActor,
inports::<100>(merchantAccount, displayName, domainName, validationUrl, X_API_Key),
outports::<50>(response, error),
state(MemoryState)
)]
pub async fn adyen_create_apple_pay_session(
context: ActorContext,
) -> Result<HashMap<String, Message>, Error> {
let inputs = context.get_payload();
let actor_config = context.get_config();
let endpoint = "/applePay/sessions".to_string();
let url = format!("{}{}", BASE_URL.trim_end_matches('/'), endpoint);
let client = reqwest::Client::builder()
.timeout_compat(Duration::from_secs(30))
.build()?;
let mut builder = client.post(&url);
builder = builder.header("Content-Type", "application/json");
builder = apply_auth(actor_config, builder)?;
if let Some(val) = inputs.get("X_API_Key") {
builder = builder.header("X-API-Key", super::message_to_str(val));
}
let mut body = serde_json::Map::new();
if let Some(val) = inputs.get("merchantAccount") {
body.insert("merchantAccount".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("displayName") {
body.insert("displayName".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("domainName") {
body.insert("domainName".to_string(), val.clone().into());
}
if let Some(val) = inputs.get("validationUrl") {
body.insert("validationUrl".to_string(), val.clone().into());
}
if !body.is_empty() {
builder = builder.json(&serde_json::Value::Object(body));
}
let mut output = HashMap::new();
match builder.send().await {
Ok(resp) => {
let status = resp.status().as_u16();
let headers: HashMap<String, String> = resp
.headers()
.iter()
.filter_map(|(k, v)| v.to_str().ok().map(|val| (k.to_string(), val.to_string())))
.collect();
let body_text = resp.text().await.unwrap_or_default();
let body_value: Value =
serde_json::from_str(&body_text).unwrap_or(Value::String(body_text));
output.insert(
"response".to_string(),
Message::object(EncodableValue::from(json!({
"status": status,
"headers": headers,
"body": body_value,
}))),
);
}
Err(e) => {
output.insert(
"error".to_string(),
Message::Error(format!("POST /applePay/sessions failed: {}", e).into()),
);
}
}
Ok(output)
}