1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// ref https://developer.apple.com/documentation/appstorereceipts/verifyreceipt

use http::{
    header::{ACCEPT, CONTENT_TYPE, USER_AGENT},
    Method, StatusCode, Version,
};
use std::io;

use crate::endpoints::endpoint_prelude::*;
use crate::{
    objects::{request_body::RequestBody, response_body::ResponseBody},
    types::status::Status,
};

const URL_PRODUCTION: &str = "https://buy.itunes.apple.com/verifyReceipt";
const URL_SANDBOX: &str = "https://sandbox.itunes.apple.com/verifyReceipt";

#[derive(Debug, PartialEq, Eq)]
pub enum RetryReason {
    ServiceUnavailable,
    GotoSandbox,
    TryAgainWithStatus(Status),
}

pub struct VerifyReceipt {
    password: String,
    receipt_data: ReceiptData,
    exclude_old_transactions: Option<bool>,
    //
    goto_sandbox: bool,
    retry_count: u8,
    max_retry_count: u8,
}
impl VerifyReceipt {
    pub fn new(
        password: String,
        receipt_data: ReceiptData,
        exclude_old_transactions: Option<bool>,
    ) -> Self {
        Self {
            password,
            receipt_data,
            exclude_old_transactions,
            goto_sandbox: false,
            retry_count: 0,
            max_retry_count: 3,
        }
    }
}

pub enum ReceiptData {
    Bytes(Vec<u8>),
    Base64String(String),
}
impl ReceiptData {
    pub fn data(&self) -> String {
        match self {
            Self::Bytes(vec) => base64::encode(vec),
            Self::Base64String(string) => string.to_owned(),
        }
    }
}

impl Endpoint for VerifyReceipt {
    type ParseResponseOutput = ResponseBody;
    type RetryReason = RetryReason;

    fn render_request(&self) -> io::Result<Request<Body>> {
        let url = if self.goto_sandbox {
            URL_SANDBOX
        } else {
            URL_PRODUCTION
        };
        let receipt_data = self.receipt_data.data();

        let body = RequestBody::new(
            receipt_data.as_str(),
            &self.password,
            self.exclude_old_transactions,
        );

        let body_bytes = serde_json::to_vec(&body)?;

        let request = Request::builder()
            .method(Method::POST)
            .uri(url)
            .version(Version::HTTP_11)
            .header(USER_AGENT, "curl/7.72.0")
            .header(CONTENT_TYPE, "application/json")
            .header(ACCEPT, "application/json")
            .body(body_bytes)
            .map_err(|err| io::Error::new(io::ErrorKind::Other, err))?;

        Ok(request)
    }

    fn parse_response(
        &mut self,
        response: Response<Body>,
    ) -> io::Result<EndpointParseResponseOutput<Self::ParseResponseOutput, Self::RetryReason>> {
        match response.status() {
            StatusCode::OK => {}
            /*
            503 <html><body><b>Http/1.1 Service Unavailable</b></body> </html>
            */
            StatusCode::SERVICE_UNAVAILABLE => {
                return Ok(EndpointParseResponseOutput::Retryable(
                    RetryReason::ServiceUnavailable,
                ));
            }
            _ => {
                return Err(io::Error::new(
                    io::ErrorKind::Other,
                    format!("status [{}] mismatch", response.status()),
                ));
            }
        }

        let body: ResponseBody = serde_json::from_slice(response.body())?;

        match &body {
            ResponseBody::Success(_) => {}
            ResponseBody::Error(body) => match &body.status {
                Status::Error21007 => {
                    if self.goto_sandbox {
                        debug_assert!(false, "double goto sandbox");
                    } else {
                        self.goto_sandbox = true;
                        return Ok(EndpointParseResponseOutput::Retryable(
                            RetryReason::GotoSandbox,
                        ));
                    }
                }
                Status::Error21002 | Status::Error21005 | Status::Error21009 => {
                    self.retry_count += 1;
                    if self.retry_count <= self.max_retry_count {
                        return Ok(EndpointParseResponseOutput::Retryable(
                            RetryReason::TryAgainWithStatus(body.status.to_owned()),
                        ));
                    }
                }
                Status::InternalDataAccessError(_) => {
                    if body.is_retryable == Some(true) {
                        self.retry_count += 1;
                        if self.retry_count <= self.max_retry_count {
                            return Ok(EndpointParseResponseOutput::Retryable(
                                RetryReason::TryAgainWithStatus(body.status.to_owned()),
                            ));
                        }
                    }
                }
                _ => {}
            },
        }

        Ok(EndpointParseResponseOutput::Done(body))
    }
}