-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstripe-helpers.ts
More file actions
71 lines (67 loc) · 1.77 KB
/
Copy pathstripe-helpers.ts
File metadata and controls
71 lines (67 loc) · 1.77 KB
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
import { Product } from 'use-shopping-cart';
export function formatAmountForDisplay(
amount: number,
currency: string
): string {
let numberFormat = new Intl.NumberFormat(['en-US'], {
style: 'currency',
currency: currency,
currencyDisplay: 'symbol',
minimumFractionDigits: 0,
maximumFractionDigits: 0
})
return numberFormat.format(amount)
}
export function formatAmountForStripe(
amount: number,
currency: string
): number {
let numberFormat = new Intl.NumberFormat(['en-US'], {
style: 'currency',
currency: currency,
currencyDisplay: 'symbol',
minimumFractionDigits: 0,
maximumFractionDigits: 0
})
const parts = numberFormat.formatToParts(amount)
let zeroDecimalCurrency: boolean = true
for (let part of parts) {
if (part.type === 'decimal') {
zeroDecimalCurrency = false
}
}
return zeroDecimalCurrency ? parseInt(amount.toFixed())*100 : Math.abs(amount * 100)
}
export function formatAmountFromStripe(
amount: number,
currency: string
): number {
let numberFormat = new Intl.NumberFormat(['en-US'], {
style: 'currency',
currency: currency,
currencyDisplay: 'symbol',
})
const parts = numberFormat.formatToParts(amount)
let zeroDecimalCurrency: boolean = true
for (let part of parts) {
if (part.type === 'decimal') {
zeroDecimalCurrency = false
}
}
return zeroDecimalCurrency ? amount : Math.round(amount / 100)
}
/**
* Returns the parsed product to send to /api/checkout_session
* @param product
* @returns
*/
export const parseStripeProduct = (product: Product) => {
return {
name: product.name,
currency: product.currency,
description: product.description,
amount: product.price,
// TODO: Add image for the product here if necessary
quantity: 1
}
}