summaryrefslogtreecommitdiff
path: root/merchant-spec/public-orders-get.ts
blob: 8a5df17e0287b1722c1b141cdbb4e2abd98de7d1 (plain)
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
// Merchant's DB state for one particular order.
// Invariants:
// paid => claimed
// claimed => !!contractHash
// requireClaimToken => !!claimToken
// !!lastPaidSessionId => paid
interface MerchantOrderInfo {
  orderId: string;
  requireClaimToken: boolean;
  claimToken?: string;
  contractHash?: string;
  claimed: boolean;
  paid: boolean;
  // Refund hasn't been picked up yet
  refundPending: boolean;
  fulfillmentUrl?: string;
  publicReorderUrl?: string;
  lastPaidSessionId?: string;
}

// Data from the client's request to /orders/{id}
interface Req {
  orderId: string;
  contractHash?: string;
  claimToken?: string;
  sessionId?: string;
  accept: "json" | "html";
}

// (Abstract) response to /orders/{id}
interface Resp {
  httpStatus: string;
  contentType: "json" | "html";
  // Schema type of the response
  responseType: string;
  // Redirect "Location: " if applicable to status code
  redirectLocation?: string;
  // "Taler: " header in the response
  talerHeader?: string;
  // Additional details about response
  response?: any;
}

// Abstracted merchant database
type MerchantOrderStore = { [orderId: string]: MerchantOrderInfo };

// Logic for /orders/{id}
function handlePublicOrdersGet(mos: MerchantOrderStore, req: Req): Resp {
  const ord = mos[req.orderId];
  if (!ord) {
    return respNotFound(req);
  }

  const authMissing = !!req.contractHash && !!req.claimToken;
  // For this endpoint, when the order does not have a claim token,
  // the order status can be accessed *without* h_contract.
  const authOk =
    ord.contractHash === req.contractHash ||
    (ord.requireClaimToken && ord.claimToken === req.claimToken) ||
    !ord.requireClaimToken;

  if (authMissing && ord.requireClaimToken) {
    // Client is trying to get the order status of an
    // order.  However, the client is not showing authentication.
    //
    // This can happen when the fulfillment URL includes the order ID,
    // and the storefront redirects the user to the backend QR code
    // page, because the order is not paid under the current session.
    // This happens on bookmarking / link sharing.
    if (!ord.publicReorderUrl) {
      return respForbidden(req);
    }
    return respGoto(req, ord.publicReorderUrl);
  }

  // Even if an order is paid for,
  // we still need the ord.claimToken, because
  // the QR code page will poll until it gets a
  // fulfillment URL, but we decided that the
  // fulfillment URL should only be returned
  // when the client is authenticated.
  // (Otherwise, guessing the order ID might leak the
  // fulfillment URL).
  if (!authOk) {
    return respForbidden(req);
  }

  if (!!req.sessionId && req.sessionId !== ord.lastPaidSessionId) {
    if (!!ord.fulfillmentUrl) {
      const alreadyPaidOrd = findAlreadyPaid(
        mos,
        req.sessionId,
        ord.fulfillmentUrl
      );
      if (!!alreadyPaidOrd) {
        return respAlreadyPaid(req, alreadyPaidOrd);
      }
    }
  }

  if (ord.paid) {
    return respPaid(req, ord);
  }

  return respUnpaid(req, ord);
}

function respNotFound(req: Req): Resp {
  return {
    contentType: req.accept,
    httpStatus: "404 Not Found",
    responseType: "TalerError",
  };
}

function respForbidden(req: Req): Resp {
  return {
    contentType: req.accept,
    httpStatus: "403 Forbidden",
    responseType: "TalerError",
  };
}

function respAlreadyPaid(req: Req, alreadyPaidOrd: MerchantOrderInfo): Resp {
  // This could be called with an empty fulfillment URL, but that doesn't
  // really make sense for the client's perspective.
  if (req.accept === "html") {
    return {
      httpStatus: "302 Found",
      contentType: "html",
      redirectLocation: alreadyPaidOrd.fulfillmentUrl,
      responseType: "empty",
    };
  }
  return {
    httpStatus: "402 PaymentRequired",
    contentType: "json",
    responseType: "StatusUnpaidResponse",
    response: {
      fulfillment_url: alreadyPaidOrd.fulfillmentUrl,
      already_paid_order_id: alreadyPaidOrd.orderId,
    },
  };
}

function respGoto(req: Req, publicReorderUrl: string): Resp {
  if (req.accept === "html") {
    return {
      httpStatus: "302 Found",
      contentType: "html",
      redirectLocation: publicReorderUrl,
      responseType: "empty",
    };
  }
  return {
    httpStatus: "202 Accepted",
    contentType: "json",
    responseType: "StatusGotoResponse",
    response: {
      public_reorder_url: publicReorderUrl,
    },
  };
}

function respUnpaid(req: Req, ord: MerchantOrderInfo): Resp {
  if (req.accept === "html") {
    return {
      httpStatus: "402 Payment Required",
      contentType: "html",
      responseType: "StatusUnpaidResponse",
      // This must include the claim token.  The same taler://
      // URI should also be shown as the QR code.
      talerHeader: "taler://pay/...",
    };
  }
  return {
    httpStatus: "402 Payment Required",
    contentType: "json",
    responseType: "StatusUnpaidResponse",
    response: {
      // Required for repurchase detection
      fulfillmentUrl: ord.fulfillmentUrl,
    },
  };
}

function respPaid(req: Req, ord: MerchantOrderInfo): Resp {
  if (req.accept === "html") {
    if (ord.refundPending) {
      return {
        httpStatus: "402 Payment Required",
        contentType: "html",
        responseType: "QRCodeRefundPage",
        talerHeader: "taler://refund/...",
      };
    }
    // We do not redirect here.  Only
    // the JS on the QR code page automatically redirects.
    // Without JS, the user has to manually click through to
    // the fulfillment URL.
    return {
      httpStatus: "200 OK",
      contentType: "html",
      responseType: "OrderStatusHtmlPage",
    };
  }
  return {
    httpStatus: "200 OK",
    contentType: "json",
    responseType: "StatusPaidResponse",
    response: {
      fulfillmentUrl: ord.fulfillmentUrl,
    },
  };
}

// Helper to find an already paid order ID.
function findAlreadyPaid(
  mos: MerchantOrderStore,
  sessionId: string,
  fulfillmentUrl: string
): MerchantOrderInfo | undefined {
  for (const orderId of Object.keys(mos)) {
    if (
      mos[orderId].lastPaidSessionId === sessionId &&
      mos[orderId].fulfillmentUrl === fulfillmentUrl
    ) {
      return mos[orderId];
    }
  }
  return undefined;
}