Skip to content

Commit

Permalink
Implement fiat topup setup
Browse files Browse the repository at this point in the history
  • Loading branch information
danielgranhao committed Nov 25, 2024
1 parent 163d61d commit 785868e
Show file tree
Hide file tree
Showing 4 changed files with 178 additions and 2 deletions.
102 changes: 100 additions & 2 deletions crow/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
use graphql::perro::{ensure, permanent_failure};
use graphql::schema::list_uncompleted_topups::{topup_status_enum, ListUncompletedTopupsTopup};
use graphql::schema::{
hide_topup, list_uncompleted_topups, register_notification_token, register_topup, HideTopup,
ListUncompletedTopups, RegisterNotificationToken, RegisterTopup,
complete_topup_setup, hide_topup, list_uncompleted_topups, register_notification_token,
register_topup, start_topup_setup, CompleteTopupSetup, HideTopup, ListUncompletedTopups,
RegisterNotificationToken, RegisterTopup, StartTopupSetup,
};
use graphql::{build_client, parse_from_rfc3339, post_blocking, ExchangeRate};
use honeybadger::Auth;
use std::sync::Arc;
use std::time::SystemTime;

use graphql::perro::runtime_error;
use graphql::schema::complete_topup_setup::CompleteTopupSetupCompleteTopupSetup;
use graphql::schema::start_topup_setup::StartTopupSetupRequest;
pub use isocountry::CountryCode;
pub use isolanguage_1::LanguageCode;

Expand Down Expand Up @@ -63,6 +66,58 @@ pub struct TopupInfo {
pub error: Option<TopupError>,
}

pub struct FiatTopupSetupChallenge {
pub id: String,
pub challenge: String,
}

/// Information about a fiat top-up registration
#[derive(Debug, Clone, PartialEq)]
pub struct FiatTopupInfo {
pub order_id: String,
/// The user should transfer fiat from this IBAN
pub debitor_iban: String,
/// This reference should be included in the fiat transfer reference
pub creditor_reference: String,
/// The user should transfer fiat to this IBAN
pub creditor_iban: String,
pub creditor_bank_name: String,
pub creditor_bank_street: String,
pub creditor_bank_postal_code: String,
pub creditor_bank_town: String,
pub creditor_bank_country: String,
pub creditor_bank_bic: String,
pub creditor_name: String,
pub creditor_street: String,
pub creditor_postal_code: String,
pub creditor_town: String,
pub creditor_country: String,
pub currency: String,
}

impl From<CompleteTopupSetupCompleteTopupSetup> for FiatTopupInfo {
fn from(value: CompleteTopupSetupCompleteTopupSetup) -> Self {
Self {
order_id: value.id,
debitor_iban: value.debitor_iban,
creditor_reference: value.creditor_reference,
creditor_iban: value.creditor_iban,
creditor_bank_name: value.creditor_bank_name,
creditor_bank_street: value.creditor_bank_street,
creditor_bank_postal_code: value.creditor_bank_postal_code,
creditor_bank_town: value.creditor_bank_town,
creditor_bank_country: value.creditor_bank_country,
creditor_bank_bic: value.creditor_bank_bic,
creditor_name: value.creditor_name,
creditor_street: value.creditor_street,
creditor_postal_code: value.creditor_postal_code,
creditor_town: value.creditor_town,
creditor_country: value.creditor_country,
currency: value.currency,
}
}
}

pub struct OfferManager {
backend_url: String,
auth: Arc<Auth>,
Expand All @@ -73,6 +128,49 @@ impl OfferManager {
Self { backend_url, auth }
}

pub fn start_topup_setup(
&self,
node_pubkey: String,
provider: String,
source_iban: String,
user_currency: String,
email: Option<String>,
) -> graphql::Result<FiatTopupSetupChallenge> {
let variables = start_topup_setup::Variables {
request: StartTopupSetupRequest {
node_pubkey,
provider,
source_iban,
user_currency,
email,
},
};
let access_token = self.auth.query_token()?;
let client = build_client(Some(&access_token))?;
let data = post_blocking::<StartTopupSetup>(&client, &self.backend_url, variables)?;

Ok(FiatTopupSetupChallenge {
id: data.start_topup_setup.id,
challenge: data.start_topup_setup.challenge,
})
}

pub fn complete_topup_setup(
&self,
id: String,
signed_challenge: String,
) -> graphql::Result<FiatTopupInfo> {
let variables = complete_topup_setup::Variables {
id,
signed_challenge,
};
let access_token = self.auth.query_token()?;
let client = build_client(Some(&access_token))?;
let data = post_blocking::<CompleteTopupSetup>(&client, &self.backend_url, variables)?;

Ok(data.complete_topup_setup.into())
}

pub fn register_topup(&self, order_id: String, email: Option<String>) -> graphql::Result<()> {
let variables = register_topup::Variables { order_id, email };
let access_token = self.auth.query_token()?;
Expand Down
28 changes: 28 additions & 0 deletions graphql/schemas/operations.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -190,3 +190,31 @@ mutation DisableLightningAddresses($addresses: [String!]!) {
mutation EnableLightningAddresses($addresses: [String!]!) {
enable_lightning_addresses(addresses: $addresses)
}

mutation StartTopupSetup($request: StartTopupSetupRequest!) {
start_topup_setup(startTopupSetupRequest: $request) {
id
challenge
}
}

mutation CompleteTopupSetup($id: String!, $signedChallenge: String!) {
complete_topup_setup(id: $id, signed_challenge: $signedChallenge) {
id
debitor_iban
creditor_reference
creditor_iban
creditor_bank_name
creditor_bank_street
creditor_bank_postal_code
creditor_bank_town
creditor_bank_country
creditor_bank_bic
creditor_name
creditor_street
creditor_postal_code
creditor_town
creditor_country
currency
}
}
34 changes: 34 additions & 0 deletions graphql/schemas/schema_wallet_read.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -667,6 +667,38 @@ input lightning_address_stream_cursor_value_input {
assignedAt: timestamptz
}

input StartTopupSetupRequest {
node_pubkey: String!
provider: String!
source_iban: String!
user_currency: String!
email: String
}

type StartTopupSetupResponse {
id: String!
challenge: String!
}

type CompleteTopupSetupResponse {
id: String!
debitor_iban: String!
creditor_reference: String!
creditor_iban: String!
creditor_bank_name: String!
creditor_bank_street: String!
creditor_bank_postal_code: String!
creditor_bank_town: String!
creditor_bank_country: String!
creditor_bank_bic: String!
creditor_name: String!
creditor_street: String!
creditor_postal_code: String!
creditor_town: String!
creditor_country: String!
currency: String!
}

"""mutation root"""
type mutation_root {
"""
Expand Down Expand Up @@ -697,6 +729,7 @@ type mutation_root {
accept_terms_conditions_v2(args: AcceptTermsConditionsV2Input): AcceptTermsConditionsV2Response
accept_wallet_acl_by_pk(pk_columns: AcceptWalletPkRequestInput!): WalletAcl
assign_lightning_address: LightningAddressResponse
complete_topup_setup(id: String!, signed_challenge: String!): CompleteTopupSetupResponse!
create_backup(encryptedBackup: String!, schemaName: String!, schemaVersion: String!): CreateBackupResponse
disable_lightning_addresses(addresses: [String!]!): Void
enable_lightning_addresses(addresses: [String!]!): Void
Expand Down Expand Up @@ -735,6 +768,7 @@ type mutation_root {
start_prepared_session_v2(challenge: String!, challengeSignature: String!, challengeSignatureType: ChallengeSignatureType, preparedPermissionToken: String!): SessionPermit
start_session(authPubKey: String!, challenge: String!, challengeSignature: String!, challengeSignatureType: ChallengeSignatureType, signedAuthPubKey: String!, walletPubKey: String!): TokenContainer
start_session_v2(authPubKey: String!, challenge: String!, challengeSignature: String!, challengeSignatureType: ChallengeSignatureType, signedAuthPubKey: String!, walletPubKey: String!): SessionPermit
start_topup_setup(startTopupSetupRequest: StartTopupSetupRequest!): StartTopupSetupResponse!
submit_lnurl_pay_invoice(id: String!, invoice: String): Void
verify_phone_number(number: String!, otp: String!): Void
}
Expand Down
16 changes: 16 additions & 0 deletions graphql/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,3 +230,19 @@ pub struct DisableLightningAddresses;
response_derives = "Debug"
)]
pub struct EnableLightningAddresses;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schemas/schema_wallet_read.graphql",
query_path = "schemas/operations.graphql",
response_derives = "Debug"
)]
pub struct StartTopupSetup;

#[derive(GraphQLQuery)]
#[graphql(
schema_path = "schemas/schema_wallet_read.graphql",
query_path = "schemas/operations.graphql",
response_derives = "Debug"
)]
pub struct CompleteTopupSetup;

0 comments on commit 785868e

Please sign in to comment.