summaryrefslogtreecommitdiff
path: root/merchant-spec/public-orders-get.ts
blob: d46475f12323909b53c78cc98c6feb8f7b4225bb (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
// 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;
  fulfillmentUrl?: string;
  publicReorderUrl?: string;
  lastPaidSessionId?: string;
}

interface Req {
  orderId: string;
  contractHash?: string;
  claimToken?: string;
  sessionId?: string;
  accept: "json" | "html";
}

interface Resp {
  httpStatus: string;
  responseType: string;
  // Additional details about response
  response?: any;
}

type MerchantOrderStore = { [orderId: string]: MerchantOrderInfo };

function handlePublicOrdersGet(mos: MerchantOrderStore, req: Req): Resp {
  const ord = mos[req.orderId];
  if (!ord) {
    return {
      httpStatus: "404 Not Found",
      responseType: "TalerErrorResponse",
    };
  }
  if (!ord.claimed) {
    if (ord.requireClaimToken && ord.claimToken !== req.claimToken) {
      return {
        httpStatus: "403 Forbidden",
        responseType: "TalerErrorResponse",
      };
    }
    return {
      httpStatus: "402 Payment Required",
      responseType: "StatusUnpaidResponse",
      response: {
        fulfillmentUrl: ord.fulfillmentUrl,
        // FIXME: do we include claim token here?
        talerPayUri: "taler://pay/",
      },
    };
  }

  if (!ord.paid) {
    if (ord.requireClaimToken && ord.claimToken !== req.claimToken) {
      // This can happen when the fulfillment URL page detects
      // the user has not paid under the current session.
      return {
        httpStatus: "202 Accepted",
        responseType: "StatusGotoResponse",
        response: {
          public_reorder_url: ord.publicReorderUrl,
        },
      };
    }
    return {
      httpStatus: "402 Payment Required",
      responseType: "StatusUnpaidResponse",
      response: {
        fulfillmentUrl: ord.fulfillmentUrl,
        // FIXME: do we include claim token here?
        talerPayUri: "taler://pay/",
      },
    };
  }

  // Here, we know that the order is paid for.
  // But 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).

  const authOk =
    ord.contractHash === req.contractHash ||
    (ord.requireClaimToken && ord.claimToken === req.claimToken);

  if (authOk) {
    if (!!req.sessionId && req.sessionId !== ord.lastPaidSessionId) {
      const alreadyPaidOrd = findAlreadyPaid(mos, req.sessionId);
      if (alreadyPaidOrd) {
        return {
          httpStatus: "202 Accepted",
          responseType: "StatusGotoResponse",
          response: {
            already_paid_order_id: alreadyPaidOrd.orderId,
          }
        }
      }
      return {
        httpStatus: "402 Payment Required",
        responseType: "StatusUnpaidResponse",
        response: {
          fulfillmentUrl: ord.fulfillmentUrl,
          // TO DISCUSS: do we include claim token here?
          talerPayUri: "taler://pay/",
        },
      };
    }
    return {
      httpStatus: "200 OK",
      responseType: "StatusPaidResponse",
      response: {
        fulfillmentUrl: ord.fulfillmentUrl,
      },
    };
  }

  return {
    httpStatus: "403 Forbidden",
    responseType: "TalerErrorResponse",
  };
}

function findAlreadyPaid(
  mos: MerchantOrderStore,
  sessionId: string
): MerchantOrderInfo | undefined {
  for (const orderId of Object.keys(mos)) {
    if (mos[orderId].lastPaidSessionId === sessionId) {
      return mos[orderId];
    }
  }
  return undefined;
}