File size: 5,214 Bytes
2b2ba56
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import type { BillingRefusal } from './api'

export interface BillingRefusalPresentation {
  action: { type: 'none' } | { type: 'portal'; url?: string } | { type: 'retry' } | { type: 'step_up' }
  message: string
  title: string
}

const portalAction = (url?: string): BillingRefusalPresentation['action'] => ({ type: 'portal', url })

const retryMessage = (refusal: BillingRefusal): string => {
  const mins = refusal.retryAfter ? ` (try again in ~${Math.max(1, Math.round(refusal.retryAfter / 60))} min)` : ''

  return `🟑 Too many charges right now${mins}. This isn't a payment failure.`
}

const stripeRetryMessage = (refusal: BillingRefusal): string => {
  const mins = refusal.retryAfter ? ` (try again in ~${Math.max(1, Math.round(refusal.retryAfter / 60))} min)` : ''

  return `Stripe is having trouble β€” try again shortly${mins}`
}

export const resolveRefusal = (refusal: BillingRefusal): BillingRefusalPresentation => {
  switch (refusal.kind) {
    case 'consent_required':
      return {
        action: portalAction(refusal.portalUrl),
        message: 'Confirm this card for terminal charges in the portal',
        title: 'Card confirmation needed'
      }

    case 'insufficient_scope':
      return {
        action: { type: 'step_up' },
        message: 'This needs Remote Spending allowed. Start a top-up to allow it, then retry.',
        title: 'Remote Spending needs approval'
      }
    case 'remote_spending_revoked': {
      const who =
        refusal.actor === 'admin'
          ? 'An admin stopped remote spending for this terminal.'
          : 'You stopped remote spending for this terminal.'

      return {
        action: portalAction(refusal.portalUrl),
        message: `${who} Reconnect from Settings β†’ Gateway to re-authorize this device.`,
        title: 'Remote spending was stopped'
      }
    }

    case 'session_revoked':
      return {
        action: portalAction(refusal.portalUrl),
        message: 'Your session was logged out. Sign in again from Settings β†’ Gateway.',
        title: 'Session logged out'
      }

    case 'cli_billing_disabled':

    case 'remote_spending_disabled':
      return {
        action: portalAction(refusal.portalUrl),
        message:
          "Remote spending is off for this account β€” a billing admin can turn it on from the portal's Hermes Agent page.",
        title: 'Remote spending is off'
      }

    case 'role_required':
      return {
        action: portalAction(refusal.portalUrl),
        message: 'Adding funds needs an org admin/owner. Ask an admin, or manage on the portal.',
        title: 'Admin role required'
      }

    case 'idempotency_conflict':
      return {
        action: { type: 'none' },
        message: 'πŸ”΄ That charge key was already used for a different amount. Start a fresh top-up.',
        title: 'Start a fresh top-up'
      }

    case 'no_payment_method':
      return {
        action: portalAction(refusal.portalUrl),
        message:
          'πŸ’³ No saved card for terminal charges yet. Set one up on the portal ' +
          "(one-time credit buys don't save a reusable card).",
        title: 'No saved card'
      }

    case 'org_access_denied':
      return {
        action: { type: 'none' },
        message: "This token isn't bound to an org you can manage",
        title: 'Org access denied'
      }
    case 'monthly_cap_exceeded': {
      const remaining = refusal.payload?.remainingUsd

      return {
        action: portalAction(refusal.portalUrl),
        message:
          remaining != null
            ? `πŸ”΄ Monthly spend cap reached β€” $${remaining} headroom left.`
            : 'πŸ”΄ Monthly spend cap reached.',
        title: 'Monthly spend cap reached'
      }
    }

    case 'rate_limited':

    case 'temporarily_unavailable':
      return {
        action: { type: 'retry' },
        message: retryMessage(refusal),
        title: 'Too many charges right now'
      }

    case 'stripe_unavailable':
      return {
        action: { type: 'retry' },
        message: stripeRetryMessage(refusal),
        title: 'Stripe is having trouble'
      }

    case 'upgrade_cap_exceeded':
      return {
        action: { type: 'none' },
        message: 'Daily plan-change limit reached β€” try again tomorrow',
        title: 'Daily plan-change limit reached'
      }

    case 'endpoint_unavailable':
      return {
        action: { type: 'retry' },
        message:
          refusal.message ||
          'Billing endpoint returned a non-JSON response (it may not be available on this deployment).',
        title: 'Billing endpoint unavailable'
      }

    case 'timeout':
      return {
        action: { type: 'retry' },
        message: refusal.message || 'Billing request timed out.',
        title: 'Billing request timed out'
      }

    case 'transport':
      return {
        action: { type: 'retry' },
        message: refusal.message || 'Billing request failed before reaching the gateway.',
        title: 'Billing connection failed'
      }

    default:
      return {
        action: { type: 'none' },
        message: refusal.message || 'Billing request failed.',
        title: 'Billing request failed'
      }
  }
}