Create API method /api/v1/subscriptions/options

This commit is contained in:
silverpill 2022-09-02 18:28:27 +00:00
parent ed98648756
commit f218936caa
3 changed files with 71 additions and 27 deletions

View file

@ -646,6 +646,20 @@ paths:
description: User's wallet address is not known or not verified description: User's wallet address is not known or not verified
418: 418:
description: Blockchain integration is not enabled description: Blockchain integration is not enabled
/api/v1/subscriptions/options:
get:
summary: Get list of subscription options
security:
- tokenAuth: []
responses:
200:
description: Successful operation
content:
application/json:
schema:
type: array
items:
$ref: '#/components/schemas/SubscriptionOption'
/api/v1/subscriptions/enable: /api/v1/subscriptions/enable:
post: post:
summary: Enable subscriptions summary: Enable subscriptions
@ -655,22 +669,7 @@ paths:
content: content:
application/json: application/json:
schema: schema:
type: object $ref: '#/components/schemas/SubscriptionOption'
properties:
type:
description: Subscription type
type: string
enum:
- ethereum
- monero
price:
description: Subscription price (only for Monero)
type: number
payout_address:
description: Payout address (only for Monero)
type: string
required:
- type
responses: responses:
200: 200:
description: Successful operation description: Successful operation
@ -1133,6 +1132,23 @@ components:
description: The date when subscription expires. description: The date when subscription expires.
type: string type: string
format: dateTime format: dateTime
SubscriptionOption:
type: object
properties:
type:
description: Subscription type
type: string
enum:
- ethereum
- monero
price:
description: Subscription price (only for Monero)
type: number
payout_address:
description: Payout address (only for Monero)
type: string
required:
- type
Tag: Tag:
type: object type: object
properties: properties:

View file

@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid; use uuid::Uuid;
use crate::models::invoices::types::DbInvoice; use crate::models::invoices::types::DbInvoice;
use crate::models::profiles::types::PaymentOption;
#[derive(Deserialize)] #[derive(Deserialize)]
pub struct InvoiceData { pub struct InvoiceData {
@ -33,11 +34,23 @@ pub struct SubscriptionQueryParams {
pub price: u64, pub price: u64,
} }
#[derive(Deserialize)] #[derive(Deserialize, Serialize)]
#[serde(tag = "type")] #[serde(tag = "type", rename_all = "kebab-case")]
pub enum SubscriptionSettings { pub enum SubscriptionOption {
#[serde(rename = "ethereum")]
Ethereum, Ethereum,
#[serde(rename = "monero")]
Monero { price: u64, payout_address: String }, Monero { price: u64, payout_address: String },
} }
impl SubscriptionOption {
pub fn from_payment_option(payment_option: PaymentOption) -> Option<Self> {
let settings = match payment_option {
PaymentOption::Link(_) => return None,
PaymentOption::EthereumSubscription(_) => Self::Ethereum,
PaymentOption::MoneroSubscription(payment_info) => Self::Monero {
price: payment_info.price,
payout_address: payment_info.payout_address,
},
};
Some(settings)
}
}

View file

@ -1,4 +1,4 @@
use actix_web::{post, web, HttpResponse, Scope}; use actix_web::{get, post, web, HttpResponse, Scope};
use actix_web_httpauth::extractors::bearer::BearerAuth; use actix_web_httpauth::extractors::bearer::BearerAuth;
use crate::activitypub::builders::update_person::prepare_update_person; use crate::activitypub::builders::update_person::prepare_update_person;
@ -30,7 +30,7 @@ use super::types::{
Invoice, Invoice,
InvoiceData, InvoiceData,
SubscriptionQueryParams, SubscriptionQueryParams,
SubscriptionSettings, SubscriptionOption,
}; };
pub async fn authorize_subscription( pub async fn authorize_subscription(
@ -59,19 +59,33 @@ pub async fn authorize_subscription(
Ok(HttpResponse::Ok().json(signature)) Ok(HttpResponse::Ok().json(signature))
} }
#[get("/options")]
async fn get_subscription_options(
auth: BearerAuth,
db_pool: web::Data<Pool>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
let options: Vec<SubscriptionOption> = current_user.profile
.payment_options.into_inner().into_iter()
.filter_map(SubscriptionOption::from_payment_option)
.collect();
Ok(HttpResponse::Ok().json(options))
}
pub async fn subscriptions_enabled( pub async fn subscriptions_enabled(
auth: BearerAuth, auth: BearerAuth,
config: web::Data<Config>, config: web::Data<Config>,
db_pool: web::Data<Pool>, db_pool: web::Data<Pool>,
maybe_blockchain: web::Data<Option<ContractSet>>, maybe_blockchain: web::Data<Option<ContractSet>>,
subscription_settings: web::Json<SubscriptionSettings>, subscription_option: web::Json<SubscriptionOption>,
) -> Result<HttpResponse, HttpError> { ) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?; let db_client = &**get_database_client(&db_pool).await?;
let mut current_user = get_current_user(db_client, auth.token()).await?; let mut current_user = get_current_user(db_client, auth.token()).await?;
let mut maybe_payment_option = None; let mut maybe_payment_option = None;
match subscription_settings.into_inner() { match subscription_option.into_inner() {
SubscriptionSettings::Ethereum => { SubscriptionOption::Ethereum => {
let ethereum_config = config.blockchain() let ethereum_config = config.blockchain()
.and_then(|conf| conf.ethereum_config()) .and_then(|conf| conf.ethereum_config())
.ok_or(HttpError::NotSupported)?; .ok_or(HttpError::NotSupported)?;
@ -95,7 +109,7 @@ pub async fn subscriptions_enabled(
)); ));
}; };
}, },
SubscriptionSettings::Monero { price, payout_address } => { SubscriptionOption::Monero { price, payout_address } => {
let monero_config = config.blockchain() let monero_config = config.blockchain()
.and_then(|conf| conf.monero_config()) .and_then(|conf| conf.monero_config())
.ok_or(HttpError::NotSupported)?; .ok_or(HttpError::NotSupported)?;
@ -161,6 +175,7 @@ async fn create_invoice_view(
pub fn subscription_api_scope() -> Scope { pub fn subscription_api_scope() -> Scope {
web::scope("/api/v1/subscriptions") web::scope("/api/v1/subscriptions")
.route("/authorize", web::get().to(authorize_subscription)) .route("/authorize", web::get().to(authorize_subscription))
.service(get_subscription_options)
.route("/enable", web::post().to(subscriptions_enabled)) .route("/enable", web::post().to(subscriptions_enabled))
.service(create_invoice_view) .service(create_invoice_view)
} }