mod common;
use common::*;
use backbone_integrations::application::service::integrations_write_service::*;
use serde_json::json;
use uuid::Uuid;
#[tokio::test]
async fn iseam1_settled_notification_becomes_real_payment() {
let pool = pool().await;
let company = Uuid::new_v4();
let svc = module(pool.clone()).await.integrations_write_service.clone();
let target = RealPaymentTarget::new(pool.clone());
let sink = CapturingSink::new();
let conn = svc.register_connector(NewConnector {
provider: format!("midtrans-{}", Uuid::new_v4()),
kind: "payment_gateway".into(), direction: "inbound".into(),
}).await.unwrap();
let customer = Uuid::new_v4();
let txn = format!("TXN-{}", Uuid::new_v4());
let out = svc.receive_event(InboundEvent {
company_id: company, connector_id: conn, event_type: "payment_settled".into(), external_id: txn.clone(), business_key: format!("{txn}:settled"),
raw: "{...midtrans...}".into(),
payload: json!({"amount": "250000", "customer_id": customer.to_string(), "order_id": "SO-9"}),
}, &target, &sink).await.unwrap();
assert_eq!(out.status, "mapped");
let payment_id = out.mapped_ref_id.expect("mapped to a payment");
let (ptype, amount, reference): (String, rust_decimal::Decimal, Option<String>) = sqlx::query_as(
"SELECT payment_type::text, paid_amount, reference_no FROM payment.payment_entries WHERE id=$1")
.bind(payment_id).fetch_one(&pool).await.unwrap();
assert_eq!(ptype, "receive", "a customer receipt");
assert_eq!(amount, rust_decimal::Decimal::new(250000, 0));
assert_eq!(reference.as_deref(), Some(txn.as_str()), "the gateway txn id is the payment reference");
let mapped_ref: Option<Uuid> = sqlx::query_scalar(
"SELECT mapped_ref_id FROM integrations.integration_events WHERE id=$1")
.bind(out.event_id).fetch_one(&pool).await.unwrap();
assert_eq!(mapped_ref, Some(payment_id));
}