22
33use bencher_json:: {
44 organization:: plan:: DEFAULT_PRICE_NAME ,
5- system:: payment:: { JsonCheckout , JsonNewCheckout , JsonNewPayment , JsonPayment } ,
5+ system:: payment:: { JsonCheckout , JsonNewCheckout } ,
66} ;
77use bencher_rbac:: organization:: Permission ;
88use bencher_schema:: {
99 conn_lock,
1010 context:: ApiContext ,
11- error:: { forbidden_error, issue_error, resource_not_found_err } ,
11+ error:: { forbidden_error, issue_error} ,
1212 model:: {
1313 organization:: QueryOrganization ,
14- user:: {
15- auth:: { AuthUser , BearerToken } ,
16- same_user,
17- } ,
14+ user:: auth:: { AuthUser , BearerToken } ,
1815 } ,
1916} ;
2017use dropshot:: { endpoint, HttpError , RequestContext , TypedBody } ;
@@ -24,78 +21,6 @@ use crate::endpoints::{
2421 Endpoint ,
2522} ;
2623
27- #[ allow( clippy:: no_effect_underscore_binding, clippy:: unused_async) ]
28- #[ endpoint {
29- method = OPTIONS ,
30- path = "/v0/payments" ,
31- tags = [ "payments" ]
32- } ]
33- pub async fn payments_options (
34- _rqctx : RequestContext < ApiContext > ,
35- ) -> Result < CorsResponse , HttpError > {
36- Ok ( Endpoint :: cors ( & [ Post . into ( ) ] ) )
37- }
38-
39- #[ endpoint {
40- method = POST ,
41- path = "/v0/payments" ,
42- tags = [ "payments" ]
43- } ]
44- pub async fn payments_post (
45- rqctx : RequestContext < ApiContext > ,
46- bearer_token : BearerToken ,
47- body : TypedBody < JsonNewPayment > ,
48- ) -> Result < ResponseCreated < JsonPayment > , HttpError > {
49- sentry:: capture_message ( "Payments endpoint activated" , sentry:: Level :: Info ) ;
50- let auth_user = AuthUser :: from_token ( rqctx. context ( ) , bearer_token) . await ?;
51- let json = post_inner ( rqctx. context ( ) , body. into_inner ( ) , & auth_user)
52- . await
53- . inspect_err ( |e| {
54- #[ cfg( feature = "sentry" ) ]
55- sentry:: capture_error ( & e) ;
56- } ) ?;
57- Ok ( Post :: pub_response_created ( json) )
58- }
59-
60- async fn post_inner (
61- context : & ApiContext ,
62- json_payment : JsonNewPayment ,
63- auth_user : & AuthUser ,
64- ) -> Result < JsonPayment , HttpError > {
65- let biller = context. biller ( ) ?;
66-
67- same_user ! ( auth_user, context. rbac, json_payment. customer. uuid) ;
68-
69- // Create a customer for the user
70- let customer_id = biller
71- . get_or_create_customer ( & json_payment. customer )
72- . await
73- . map_err ( resource_not_found_err ! ( Plan , json_payment. customer) ) ?;
74-
75- // Create a payment method for the user
76- let payment_method_id = biller
77- . create_payment_method ( customer_id. clone ( ) , json_payment. card )
78- . await
79- . map_err ( resource_not_found_err ! ( Plan , & customer_id) ) ?;
80-
81- Ok ( JsonPayment {
82- customer : customer_id. as_ref ( ) . parse ( ) . map_err ( |e| {
83- issue_error (
84- "Failed to parse customer ID" ,
85- & format ! ( "Failed to parse customer ID ({customer_id})." ) ,
86- e,
87- )
88- } ) ?,
89- payment_method : payment_method_id. as_ref ( ) . parse ( ) . map_err ( |e| {
90- issue_error (
91- "Failed to parse payment method ID" ,
92- & format ! ( "Failed to parse payment method ID ({payment_method_id})." ) ,
93- e,
94- )
95- } ) ?,
96- } )
97- }
98-
9924#[ allow( clippy:: no_effect_underscore_binding, clippy:: unused_async) ]
10025#[ endpoint {
10126 method = OPTIONS ,
0 commit comments