types.go (5075B)
1 package backend 2 3 // Timestamp is the JSON representation used by the Merchant API for an 4 // absolute point in time. 5 type Timestamp struct { 6 Seconds int64 `json:"t_s"` 7 } 8 9 // RelativeTime is the JSON representation used by the Merchant API for a 10 // duration. 11 type RelativeTime struct { 12 Microseconds int64 `json:"d_us"` 13 } 14 15 // OrderExtra is the application-owned portion of an order's contract terms. 16 // The demos use one set of fields for essays and another for donations. 17 type OrderExtra struct { 18 ArticleName string `json:"article_name,omitempty"` 19 Donor string `json:"donor,omitempty"` 20 Receiver string `json:"receiver,omitempty"` 21 Amount string `json:"amount,omitempty"` 22 } 23 24 // OrderInput describes an input required by an order choice. 25 type OrderInput struct { 26 Count int `json:"count,omitempty"` 27 Type string `json:"type"` 28 TokenFamilySlug string `json:"token_family_slug,omitempty"` 29 } 30 31 // OrderOutput describes an output produced by an order choice. 32 type OrderOutput struct { 33 Type string `json:"type"` 34 TokenFamilySlug string `json:"token_family_slug,omitempty"` 35 Amount string `json:"amount,omitempty"` 36 } 37 38 // OrderChoice is one payment alternative in a version 1 order. 39 type OrderChoice struct { 40 Amount string `json:"amount"` 41 Description string `json:"description,omitempty"` 42 Inputs []OrderInput `json:"inputs,omitempty"` 43 Outputs []OrderOutput `json:"outputs,omitempty"` 44 } 45 46 // Product is an item supplied directly in the order. Prices are totals for 47 // the given quantity, one per currency. 48 type Product struct { 49 Name string `json:"product_name"` 50 Description string `json:"description"` 51 UnitQuantity string `json:"unit_quantity"` 52 Prices []string `json:"prices"` 53 PricesAreNet bool `json:"prices_are_net"` 54 Image string `json:"image,omitempty"` 55 } 56 57 // Order contains the Merchant order fields used by the demo applications. 58 type Order struct { 59 OrderID string `json:"order_id,omitempty"` 60 Version int `json:"version,omitempty"` 61 Amount string `json:"amount,omitempty"` 62 Choices []OrderChoice `json:"choices,omitempty"` 63 Products []Product `json:"products,omitempty"` 64 Extra OrderExtra `json:"extra"` 65 FulfillmentURL string `json:"fulfillment_url"` 66 PublicReorderURL string `json:"public_reorder_url,omitempty"` 67 Summary string `json:"summary"` 68 PayDeadline Timestamp `json:"pay_deadline"` 69 WireTransferDeadline Timestamp `json:"wire_transfer_deadline"` 70 MinimumAge int `json:"minimum_age,omitempty"` 71 } 72 73 // PostOrderRequest is the request body for POST private/orders. 74 type PostOrderRequest struct { 75 Order Order `json:"order"` 76 RefundDelay RelativeTime `json:"refund_delay"` 77 SessionID string `json:"session_id,omitempty"` 78 CreateToken bool `json:"create_token,omitempty"` 79 } 80 81 // PostOrderResponse is the subset of the order creation response used by the 82 // demos. 83 type PostOrderResponse struct { 84 OrderID string `json:"order_id"` 85 Token string `json:"token,omitempty"` 86 } 87 88 // ContractTerms is the subset of contract terms read by the demos. 89 type ContractTerms struct { 90 Version int `json:"version,omitempty"` 91 Amount string `json:"amount,omitempty"` 92 Choices []OrderChoice `json:"choices,omitempty"` 93 Extra OrderExtra `json:"extra"` 94 RefundDeadline Timestamp `json:"refund_deadline"` 95 } 96 97 // OrderStatusResponse combines the fields used by the demos from the paid, 98 // claimed, and unpaid Merchant order status variants. 99 type OrderStatusResponse struct { 100 OrderStatus string `json:"order_status"` 101 Refunded bool `json:"refunded"` 102 ContractTerms ContractTerms `json:"contract_terms"` 103 ChoiceIndex *int `json:"choice_index,omitempty"` 104 AlreadyPaidOrderID string `json:"already_paid_order_id,omitempty"` 105 OrderStatusURL string `json:"order_status_url,omitempty"` 106 } 107 108 // SelectedAmount returns the price of the alternative selected by the wallet. 109 func (s OrderStatusResponse) SelectedAmount() (string, bool) { 110 terms := s.ContractTerms 111 switch terms.Version { 112 case 0: 113 return terms.Amount, terms.Amount != "" 114 case 1: 115 if s.ChoiceIndex != nil && *s.ChoiceIndex >= 0 && *s.ChoiceIndex < len(terms.Choices) { 116 amount := terms.Choices[*s.ChoiceIndex].Amount 117 return amount, amount != "" 118 } 119 } 120 return "", false 121 } 122 123 // OrderHistory is returned by GET private/orders. 124 type OrderHistory struct { 125 Orders []OrderHistoryEntry `json:"orders"` 126 } 127 128 // OrderHistoryEntry contains the fields used to locate an earlier payment in 129 // the same browser session. 130 type OrderHistoryEntry struct { 131 OrderID string `json:"order_id"` 132 Paid bool `json:"paid"` 133 } 134 135 // RefundRequest is the request body for an order refund. 136 type RefundRequest struct { 137 Refund string `json:"refund"` 138 Reason string `json:"reason"` 139 }