1+ use lazy_static:: lazy_static;
12use reqwest:: { header:: AUTHORIZATION , Client , Error as RequestError } ;
23use serde:: Deserialize ;
34use thiserror:: Error ;
45
5- use crate :: models:: { key:: { ApiKey , ApiKeyError } , user:: User } ;
6+ use crate :: models:: {
7+ key:: { ApiKey , ApiKeyError } ,
8+ user:: User ,
9+ } ;
10+
11+ lazy_static ! {
12+ static ref STRIPE_SECRET : String = lc!( env!( "STRIPE_SECRET" ) ) ;
13+ }
614
715#[ derive( Error , Debug ) ]
816pub enum StripeError {
917 #[ error( "{0:#}" ) ]
1018 Request ( #[ from] RequestError ) ,
1119
1220 #[ error( "{0:#}" ) ]
13- ApiKey ( #[ from] ApiKeyError )
21+ ApiKey ( #[ from] ApiKeyError ) ,
1422}
1523
1624#[ derive( Deserialize ) ]
1725struct StripeSession {
18- url : String
26+ url : String ,
1927}
2028
21- pub async fn create_stripe_payment ( host : & String , user : & User , amount : f32 ) -> Result < String , StripeError > {
29+ pub async fn create_stripe_payment (
30+ host : & String ,
31+ user : & User ,
32+ amount : f32 ,
33+ ) -> Result < String , StripeError > {
2234 let StripeSession { url } = Client :: new ( )
2335 . post ( "https://api.stripe.com/v1/checkout/sessions" )
24- . header ( AUTHORIZATION , format ! ( "Bearer {}" , lc! ( " STRIPE_SECRET" ) ) )
36+ . header ( AUTHORIZATION , format ! ( "Bearer {}" , * STRIPE_SECRET ) )
2537 . form ( & [
2638 ( "payment_method_types[]" , "card" ) ,
2739 ( "line_items[0][price_data][currency]" , "usd" ) ,
28- ( "line_items[0][price_data][product_data][name]" , "RlARndG API key" ) ,
29- ( "line_items[0][price_data][unit_amount]" , & ( amount * 100f32 ) . round ( ) . to_string ( ) ) ,
40+ (
41+ "line_items[0][price_data][product_data][name]" ,
42+ "RlARndG API key" ,
43+ ) ,
44+ (
45+ "line_items[0][price_data][unit_amount]" ,
46+ & ( amount * 100f32 ) . round ( ) . to_string ( ) ,
47+ ) ,
3048 ( "line_items[0][quantity]" , "1" ) ,
3149 ( "mode" , "payment" ) ,
32- ( "success_url" , & format ! (
33- "{host}/api/keys/checkout/success?i={}&p={}&c={{CHECKOUT_SESSION_ID}}" ,
34- user. id,
35- amount
36- ) ) ,
37- ( "cancel_url" , & format ! ( "{host}/pricing" ) )
50+ (
51+ "success_url" ,
52+ & format ! (
53+ "{host}/api/keys/checkout/success?i={}&p={}&c={{CHECKOUT_SESSION_ID}}" ,
54+ user. id, amount
55+ ) ,
56+ ) ,
57+ ( "cancel_url" , & format ! ( "{host}/pricing" ) ) ,
3858 ] )
3959 . send ( )
4060 . await ?
@@ -46,12 +66,12 @@ pub async fn create_stripe_payment(host: &String, user: &User, amount: f32) -> R
4666
4767#[ derive( Deserialize ) ]
4868struct StripeSessionResponse {
49- payment_intent : String
69+ payment_intent : String ,
5070}
5171
5272#[ derive( Deserialize ) ]
5373struct PaymentIntentResponse {
54- status : String
74+ status : String ,
5575}
5676
5777pub async fn verify_payment ( session_id : & String ) -> Result < bool , StripeError > {
@@ -60,16 +80,20 @@ pub async fn verify_payment(session_id: &String) -> Result<bool, StripeError> {
6080 }
6181
6282 let StripeSessionResponse { payment_intent } = Client :: new ( )
63- . get ( format ! ( "https://api.stripe.com/v1/checkout/sessions/{session_id}" ) )
64- . header ( AUTHORIZATION , format ! ( "Bearer {}" , lc!( "STRIPE_SECRET" ) ) )
83+ . get ( format ! (
84+ "https://api.stripe.com/v1/checkout/sessions/{session_id}"
85+ ) )
86+ . header ( AUTHORIZATION , format ! ( "Bearer {}" , * STRIPE_SECRET ) )
6587 . send ( )
6688 . await ?
6789 . json ( )
6890 . await ?;
6991
7092 let PaymentIntentResponse { status } = Client :: new ( )
71- . get ( format ! ( "https://api.stripe.com/v1/payment_intents/{payment_intent}" ) )
72- . header ( AUTHORIZATION , format ! ( "Bearer {}" , lc!( "STRIPE_SECRET" ) ) )
93+ . get ( format ! (
94+ "https://api.stripe.com/v1/payment_intents/{payment_intent}"
95+ ) )
96+ . header ( AUTHORIZATION , format ! ( "Bearer {}" , * STRIPE_SECRET ) )
7397 . send ( )
7498 . await ?
7599 . json ( )
0 commit comments