Init & create payment (not working)

This commit is contained in:
Adrian Woźniak 2022-12-09 17:01:16 +01:00
parent 9775274d71
commit 8d17fb9309
2 changed files with 23 additions and 4 deletions

View File

@ -117,6 +117,10 @@ impl PayU {
}
fn create_payment(c: CreatePayment) -> Result<res::CreateOrder, Error> {
eprintln!("Authorizing...");
authorize()?;
eprintln!("Authorized!");
let config = client();
let pay_u = config.lock().unwrap();
@ -158,6 +162,7 @@ fn create_payment(c: CreatePayment) -> Result<res::CreateOrder, Error> {
description,
)
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
Error::MalformedCreatePayment
})?
@ -172,7 +177,7 @@ fn create_payment(c: CreatePayment) -> Result<res::CreateOrder, Error> {
.with_notify_url(notify_uri)
.with_continue_url(continue_uri);
authorize()?;
eprintln!("calling host http 'create order'!");
let res = wapc::host_call(
"binding",
"http:req",
@ -187,11 +192,13 @@ fn create_payment(c: CreatePayment) -> Result<res::CreateOrder, Error> {
.unwrap(),
)
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
Error::PaymentFailed
})
.and_then(|v| {
deserialize(&v).map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
Error::PaymentFailed
})
@ -219,6 +226,7 @@ fn authorize() -> Result<bool, Error> {
expires_in: i64,
}
eprintln!("calling host http 'authorize'!");
let res = wapc::host_call("binding", "http:req", "http_req", &serialize(HttpRequest {
method: HttpMethod::Post,
url: format!(
@ -230,16 +238,20 @@ fn authorize() -> Result<bool, Error> {
bearer_auth: None,
body: None,
}).map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
Error::PaymentFailed
})?).map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
Error::PaymentFailed
})?;
let res: BearerResult = deserialize(&res).map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
Error::PaymentFailed
})?;
eprintln!("Authorized with calling host");
tracing::trace!("Bearer is {}", res.access_token);
pay_u.bearer_expires_at = Utc::now() + Duration::seconds(res.expires_in);
pay_u.bearer = Some(res.access_token);

View File

@ -33,6 +33,7 @@ impl UpdateConfig for Opts {
#[tokio::main]
async fn main() {
tracing_subscriber::fmt::init();
let opts: Opts = gumdrop::parse_args_default_or_exit();
let config = config::default_load(&opts);
@ -180,11 +181,13 @@ fn http_request(req: HttpRequest) -> Result<Vec<u8>, payment_adapter::Error> {
.bearer_auth(bearer)
.send()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?
.text()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?;
@ -196,11 +199,13 @@ fn http_request(req: HttpRequest) -> Result<Vec<u8>, payment_adapter::Error> {
.headers(headers)
.send()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?
.text()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?;
@ -208,9 +213,7 @@ fn http_request(req: HttpRequest) -> Result<Vec<u8>, payment_adapter::Error> {
}
(HttpMethod::Post, Some(bearer)) => {
let body = body.unwrap_or_default();
let len = body.len();
let body = std::io::Cursor::new(body);
let body = reqwest::blocking::Body::sized(body, len as u64);
let body = reqwest::blocking::Body::from(body);
let text = client
.post(url)
.headers(headers)
@ -218,11 +221,13 @@ fn http_request(req: HttpRequest) -> Result<Vec<u8>, payment_adapter::Error> {
.body(body)
.send()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?
.text()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?;
@ -235,11 +240,13 @@ fn http_request(req: HttpRequest) -> Result<Vec<u8>, payment_adapter::Error> {
.body(body.unwrap_or_default())
.send()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?
.text()
.map_err(|e| {
eprintln!("{}", e);
tracing::error!("{}", e);
payment_adapter::Error::HttpFailed
})?;