(receipt, id string)
| 121 | } |
| 122 | |
| 123 | func verifyReceipt(receipt, id string) error { |
| 124 | receiptsHost := os.Getenv("RECEIPTS_HOST") |
| 125 | if receiptsHost == "" { |
| 126 | receiptsHost = "https://webmonetization.org/api/receipts/verify?id=" + id |
| 127 | } else { |
| 128 | receiptsHost = fmt.Sprintf("%s/receipts?id=%s", receiptsHost, id) |
| 129 | } |
| 130 | |
| 131 | log.Info("Verifying receipt %s at %s", receipt, receiptsHost) |
| 132 | r, err := http.NewRequest("POST", receiptsHost, bytes.NewBufferString(receipt)) |
| 133 | if err != nil { |
| 134 | log.Error("Unable to create new request to %s: %s", receiptsHost, err) |
| 135 | return err |
| 136 | } |
| 137 | |
| 138 | resp, err := http.DefaultClient.Do(r) |
| 139 | if err != nil { |
| 140 | log.Error("Unable to Do() request to %s: %s", receiptsHost, err) |
| 141 | return err |
| 142 | } |
| 143 | if resp != nil && resp.Body != nil { |
| 144 | defer resp.Body.Close() |
| 145 | } |
| 146 | |
| 147 | body, err := io.ReadAll(resp.Body) |
| 148 | if err != nil { |
| 149 | log.Error("Unable to read %s response body: %s", receiptsHost, err) |
| 150 | return err |
| 151 | } |
| 152 | log.Info("Status : %s", resp.Status) |
| 153 | log.Info("Response: %s", body) |
| 154 | |
| 155 | if resp.StatusCode != http.StatusOK { |
| 156 | log.Error("Bad response from %s:\nStatus: %d\n%s", receiptsHost, resp.StatusCode, string(body)) |
| 157 | return impart.HTTPError{resp.StatusCode, string(body)} |
| 158 | } |
| 159 | return nil |
| 160 | } |
no test coverage detected