Skip to content

Commit

Permalink
release 1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
P-Asta committed Apr 9, 2024
1 parent 1602e7b commit 40df9af
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 29 deletions.
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,15 @@
# tosspay-rs
사업자 등록증 없이 결제 시스템을 만들수있는 라이브러리

> [!IMPORTANT]
> 이 라이브러리를 사용하기 위해서는 토스아이디(toss-id)가 필요합니다
> ![](./img/important.png)

## on_payment
특정 유저가 보낸돈만 확인할때만 사용합니다 <br/>
[자세히보기 >](./docs/on_payment.md)

## on_donate
모든 사용자가 보낸 후원 내역을 확인합니다. <br/>
[자세히보기 >](./docs/on_donate.md)
15 changes: 15 additions & 0 deletions docs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# tosspay-rs
사업자 등록증 없이 결제 시스템을 만들수있는 라이브러리

> [!IMPORTANT]
> 이 라이브러리를 사용하기 위해서는 토스아이디(toss-id)가 필요합니다
> ![](../img/important.png)

## on_payment
특정 유저가 보낸돈만 확인할때만 사용합니다 <br/>
[자세히보기 >](./on_payment.md)

## on_donate
모든 사용자가 보낸 후원 내역을 확인합니다. <br/>
[자세히보기 >](./on_donate.md)
18 changes: 18 additions & 0 deletions docs/on_donate.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# on_donate

```rs
use tosspay::TossPay;

#[tokio::main]
async fn main() {
let toss = TossPay::new("wntjd0612".to_string());
toss.on_donate(|data| {
if data.amount == 1 {
println!("{:?}", data);
} else {
println!("nou");
}
});
loop {} // 코드가 계속 반복되고있지 않은 코드라면 이걸 넣어줘야해요
}
```
20 changes: 20 additions & 0 deletions docs/on_payment.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# on_donate

```rs
use tosspay::TossPay;
#[tokio::main]
async fn main() {
let toss = TossPay::new("objective".to_string() /* 여기 자신의 toss-id 써주세여*/);
let code = toss.on_payment(|data| {
if data.amount == 1 {
println!("{:?}", data);
Ok(()) // 성공했다면 Ok(())를 반환해주세요
} else {
println!("nou");
Err(()) // 실패했다면 Ok(())를 반환해주세요
}
});
println!("{}", code);
loop {} // 코드가 계속 반복되고있지 않은 코드라면 이걸 넣어줘야해요
}
```
Binary file added img/important.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
13 changes: 8 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use tosspay::TossPay;
#[tokio::main]
async fn main() {
let toss = TossPay::new("wntjd0612".to_string());
// let toss2 = TossPay::new("wntjd0612".to_string());
// let code = toss.on_pay(|data| {
// println!("{:?}", data);
// });
// println!("{}", code);
toss.on_donate(|data| {
if data.amount == 1 {
println!("{:?}", data);
} else {
println!("nou");
}
});
loop {}
}
52 changes: 28 additions & 24 deletions tosspay/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl TossPay {
}

pub fn on_donate(&self, f: fn(TossPayData)) {
let fake_self = self.clone();
let mut old_datas = Vec::new();
tokio::spawn(async move {
loop {
Expand All @@ -69,33 +68,37 @@ impl TossPay {
}
});
}
pub fn on_pay(&self, f: fn(TossPayData)) -> String {
pub fn on_payment(&self, f: fn(TossPayData) -> Result<(), ()>) -> String {
let code = gen_code();
let fake_code = code.clone();
let mut fake_code = code.clone();
unsafe {
IDS.push(code.clone());
}
let fake_self = self.clone();
let mut old_datas = Vec::new();
tokio::spawn(async move {
loop {
let json = unsafe { RES.clone() };
let datas = json.success.data;
if old_datas.clone() != datas.clone() {
let send_data = datas.clone();
send_data
.iter()
.filter(|e| old_datas.iter().find(|x| x == e).is_none())
.for_each(|x| {
if x.senderDisplayName == fake_code {
f(x.to_owned())
thread::spawn(move || 'scan_loop: loop {
let json = unsafe { RES.clone() };
let datas = json.success.data;
if old_datas.clone() != datas.clone() {
let send_data = datas.clone();
send_data
.iter()
.filter(|e| old_datas.iter().find(|x| x == e).is_none())
.for_each(|x| {
if x.senderDisplayName == fake_code {
match f(x.to_owned()) {
Ok(_) => {
fake_code = "".to_string();
}
Err(_) => {}
}
});
}
});
if fake_code == "".to_string() {
break 'scan_loop;
}
old_datas = datas.clone();
// let rng = rand
thread::sleep(std::time::Duration::from_secs(2));
}
old_datas = datas.clone();
thread::sleep(std::time::Duration::from_secs(2));
});
return code.clone();
}
Expand Down Expand Up @@ -144,15 +147,16 @@ pub struct TossPaySuccess {
#[allow(non_snake_case)]
#[derive(Debug, serde::Serialize, serde::Deserialize, PartialEq, Eq, Clone)]
pub struct TossPayData {
senderDisplayName: String,
amount: usize,
msg: String,
pub senderDisplayName: String,
pub amount: usize,
pub msg: String,
}

fn gen_code() -> String {
let mut rng = rand::thread_rng();
let code = format!(
"{}{}{}{}",
"{}{}{}{}{}",
rng.gen_range(0..10),
rng.gen_range(0..10),
rng.gen_range(0..10),
rng.gen_range(0..10),
Expand Down

0 comments on commit 40df9af

Please sign in to comment.