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
418:
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:
post:
summary: Enable subscriptions
@ -655,22 +669,7 @@ paths:
content:
application/json:
schema:
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
$ref: '#/components/schemas/SubscriptionOption'
responses:
200:
description: Successful operation
@ -1133,6 +1132,23 @@ components:
description: The date when subscription expires.
type: string
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:
type: object
properties:

View file

@ -2,6 +2,7 @@ use serde::{Deserialize, Serialize};
use uuid::Uuid;
use crate::models::invoices::types::DbInvoice;
use crate::models::profiles::types::PaymentOption;
#[derive(Deserialize)]
pub struct InvoiceData {
@ -33,11 +34,23 @@ pub struct SubscriptionQueryParams {
pub price: u64,
}
#[derive(Deserialize)]
#[serde(tag = "type")]
pub enum SubscriptionSettings {
#[serde(rename = "ethereum")]
#[derive(Deserialize, Serialize)]
#[serde(tag = "type", rename_all = "kebab-case")]
pub enum SubscriptionOption {
Ethereum,
#[serde(rename = "monero")]
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 crate::activitypub::builders::update_person::prepare_update_person;
@ -30,7 +30,7 @@ use super::types::{
Invoice,
InvoiceData,
SubscriptionQueryParams,
SubscriptionSettings,
SubscriptionOption,
};
pub async fn authorize_subscription(
@ -59,19 +59,33 @@ pub async fn authorize_subscription(
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(
auth: BearerAuth,
config: web::Data<Config>,
db_pool: web::Data<Pool>,
maybe_blockchain: web::Data<Option<ContractSet>>,
subscription_settings: web::Json<SubscriptionSettings>,
subscription_option: web::Json<SubscriptionOption>,
) -> Result<HttpResponse, HttpError> {
let db_client = &**get_database_client(&db_pool).await?;
let mut current_user = get_current_user(db_client, auth.token()).await?;
let mut maybe_payment_option = None;
match subscription_settings.into_inner() {
SubscriptionSettings::Ethereum => {
match subscription_option.into_inner() {
SubscriptionOption::Ethereum => {
let ethereum_config = config.blockchain()
.and_then(|conf| conf.ethereum_config())
.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()
.and_then(|conf| conf.monero_config())
.ok_or(HttpError::NotSupported)?;
@ -161,6 +175,7 @@ async fn create_invoice_view(
pub fn subscription_api_scope() -> Scope {
web::scope("/api/v1/subscriptions")
.route("/authorize", web::get().to(authorize_subscription))
.service(get_subscription_options)
.route("/enable", web::post().to(subscriptions_enabled))
.service(create_invoice_view)
}