| 107 | } |
| 108 | |
| 109 | function pay(succeed: boolean) { |
| 110 | const total = cart.reduce((sum, i) => sum + i.price * i.qty, 0); |
| 111 | op.track('payment_attempted', { total, success: succeed }); |
| 112 | |
| 113 | if (succeed) { |
| 114 | op.revenue(total, { |
| 115 | items: cart.map((i) => i.id), |
| 116 | item_count: cart.reduce((sum, i) => sum + i.qty, 0), |
| 117 | }); |
| 118 | op.track('purchase_completed', { total }); |
| 119 | setCart([]); |
| 120 | navigate('/success'); |
| 121 | } else { |
| 122 | op.track('purchase_failed', { total, reason: 'declined' }); |
| 123 | navigate('/error'); |
| 124 | } |
| 125 | } |
| 126 | |
| 127 | const cartCount = cart.reduce((sum, i) => sum + i.qty, 0); |
| 128 | |