github.com/stripe/stripe-go/v76@v76.25.0/file.go (about) 1 // 2 // 3 // File generated from our OpenAPI spec 4 // 5 // 6 7 package stripe 8 9 import ( 10 "bytes" 11 "encoding/json" 12 "github.com/stripe/stripe-go/v76/form" 13 "io" 14 "mime/multipart" 15 "net/url" 16 "path/filepath" 17 ) 18 19 // The [purpose](https://stripe.com/docs/file-upload#uploading-a-file) of the uploaded file. 20 type FilePurpose string 21 22 // List of values that FilePurpose can take 23 const ( 24 FilePurposeAccountRequirement FilePurpose = "account_requirement" 25 FilePurposeAdditionalVerification FilePurpose = "additional_verification" 26 FilePurposeBusinessIcon FilePurpose = "business_icon" 27 FilePurposeBusinessLogo FilePurpose = "business_logo" 28 FilePurposeCustomerSignature FilePurpose = "customer_signature" 29 FilePurposeDisputeEvidence FilePurpose = "dispute_evidence" 30 FilePurposeDocumentProviderIdentityDocument FilePurpose = "document_provider_identity_document" 31 FilePurposeFinanceReportRun FilePurpose = "finance_report_run" 32 FilePurposeIdentityDocument FilePurpose = "identity_document" 33 FilePurposeIdentityDocumentDownloadable FilePurpose = "identity_document_downloadable" 34 FilePurposePCIDocument FilePurpose = "pci_document" 35 FilePurposeSelfie FilePurpose = "selfie" 36 FilePurposeSigmaScheduledQuery FilePurpose = "sigma_scheduled_query" 37 FilePurposeTaxDocumentUserUpload FilePurpose = "tax_document_user_upload" 38 FilePurposeTerminalReaderSplashscreen FilePurpose = "terminal_reader_splashscreen" 39 ) 40 41 // Returns a list of the files that your account has access to. Stripe sorts and returns the files by their creation dates, placing the most recently created files at the top. 42 type FileListParams struct { 43 ListParams `form:"*"` 44 // Only return files that were created during the given date interval. 45 Created *int64 `form:"created"` 46 // Only return files that were created during the given date interval. 47 CreatedRange *RangeQueryParams `form:"created"` 48 // Specifies which fields in the response should be expanded. 49 Expand []*string `form:"expand"` 50 // Filter queries by the file purpose. If you don't provide a purpose, the queries return unfiltered files. 51 Purpose *string `form:"purpose"` 52 } 53 54 // AddExpand appends a new field to expand. 55 func (p *FileListParams) AddExpand(f string) { 56 p.Expand = append(p.Expand, &f) 57 } 58 59 // Optional parameters that automatically create a [file link](https://stripe.com/docs/api#file_links) for the newly created file. 60 type FileFileLinkDataParams struct { 61 Params `form:"*"` 62 // Set this to `true` to create a file link for the newly created file. Creating a link is only possible when the file's `purpose` is one of the following: `business_icon`, `business_logo`, `customer_signature`, `dispute_evidence`, `pci_document`, `tax_document_user_upload`, or `terminal_reader_splashscreen`. 63 Create *bool `form:"create"` 64 // The link isn't available after this future timestamp. 65 ExpiresAt *int64 `form:"expires_at"` 66 // Set of [key-value pairs](https://stripe.com/docs/api/metadata) that you can attach to an object. This can be useful for storing additional information about the object in a structured format. Individual keys can be unset by posting an empty value to them. All keys can be unset by posting an empty value to `metadata`. 67 Metadata map[string]string `form:"metadata"` 68 } 69 70 // AddMetadata adds a new key-value pair to the Metadata. 71 func (p *FileFileLinkDataParams) AddMetadata(key string, value string) { 72 if p.Metadata == nil { 73 p.Metadata = make(map[string]string) 74 } 75 76 p.Metadata[key] = value 77 } 78 79 // To upload a file to Stripe, you need to send a request of type multipart/form-data. Include the file you want to upload in the request, and the parameters for creating a file. 80 // 81 // All of Stripe's officially supported Client libraries support sending multipart/form-data. 82 type FileParams struct { 83 Params `form:"*"` 84 // Specifies which fields in the response should be expanded. 85 Expand []*string `form:"expand"` 86 // FileReader is a reader with the contents of the file that should be uploaded. 87 FileReader io.Reader 88 89 // Filename is just the name of the file without path information. 90 Filename *string 91 // Optional parameters that automatically create a [file link](https://stripe.com/docs/api#file_links) for the newly created file. 92 FileLinkData *FileFileLinkDataParams `form:"file_link_data"` 93 // The [purpose](https://stripe.com/docs/file-upload#uploading-a-file) of the uploaded file. 94 Purpose *string `form:"purpose"` 95 } 96 97 // AddExpand appends a new field to expand. 98 func (p *FileParams) AddExpand(f string) { 99 p.Expand = append(p.Expand, &f) 100 } 101 102 // This object represents files hosted on Stripe's servers. You can upload 103 // files with the [create file](https://stripe.com/docs/api#create_file) request 104 // (for example, when uploading dispute evidence). Stripe also 105 // creates files independently (for example, the results of a [Sigma scheduled 106 // query](https://stripe.com/docs/api#scheduled_queries)). 107 // 108 // Related guide: [File upload guide](https://stripe.com/docs/file-upload) 109 type File struct { 110 APIResource 111 // Time at which the object was created. Measured in seconds since the Unix epoch. 112 Created int64 `json:"created"` 113 // The file expires and isn't available at this time in epoch seconds. 114 ExpiresAt int64 `json:"expires_at"` 115 // The suitable name for saving the file to a filesystem. 116 Filename string `json:"filename"` 117 // Unique identifier for the object. 118 ID string `json:"id"` 119 // A list of [file links](https://stripe.com/docs/api#file_links) that point at this file. 120 Links *FileLinkList `json:"links"` 121 // String representing the object's type. Objects of the same type share the same value. 122 Object string `json:"object"` 123 // The [purpose](https://stripe.com/docs/file-upload#uploading-a-file) of the uploaded file. 124 Purpose FilePurpose `json:"purpose"` 125 // The size of the file object in bytes. 126 Size int64 `json:"size"` 127 // A suitable title for the document. 128 Title string `json:"title"` 129 // The returned file type (for example, `csv`, `pdf`, `jpg`, or `png`). 130 Type string `json:"type"` 131 // Use your live secret API key to download the file from this URL. 132 URL string `json:"url"` 133 } 134 135 // FileList is a list of Files as retrieved from a list endpoint. 136 type FileList struct { 137 APIResource 138 ListMeta 139 Data []*File `json:"data"` 140 } 141 142 // GetBody gets an appropriate multipart form payload to use in a request body 143 // to create a new file. 144 func (p *FileParams) GetBody() (*bytes.Buffer, string, error) { 145 body := &bytes.Buffer{} 146 writer := multipart.NewWriter(body) 147 148 if p.Purpose != nil { 149 err := writer.WriteField("purpose", StringValue(p.Purpose)) 150 if err != nil { 151 return nil, "", err 152 } 153 } 154 155 if p.FileReader != nil && p.Filename != nil { 156 part, err := writer.CreateFormFile( 157 "file", 158 filepath.Base(StringValue(p.Filename)), 159 ) 160 161 if err != nil { 162 return nil, "", err 163 } 164 165 _, err = io.Copy(part, p.FileReader) 166 if err != nil { 167 return nil, "", err 168 } 169 } 170 171 if p.FileLinkData != nil { 172 values := &form.Values{} 173 form.AppendToPrefixed(values, p.FileLinkData, []string{"file_link_data"}) 174 175 params, err := url.ParseQuery(values.Encode()) 176 if err != nil { 177 return nil, "", err 178 } 179 for key, values := range params { 180 err := writer.WriteField(key, values[0]) 181 if err != nil { 182 return nil, "", err 183 } 184 } 185 } 186 187 err := writer.Close() 188 if err != nil { 189 return nil, "", err 190 } 191 192 return body, writer.Boundary(), nil 193 } 194 195 // UnmarshalJSON handles deserialization of a File. 196 // This custom unmarshaling is needed because the resulting 197 // property may be an id or the full struct if it was expanded. 198 func (f *File) UnmarshalJSON(data []byte) error { 199 if id, ok := ParseID(data); ok { 200 f.ID = id 201 return nil 202 } 203 204 type file File 205 var v file 206 if err := json.Unmarshal(data, &v); err != nil { 207 return err 208 } 209 210 *f = File(v) 211 return nil 212 }