@@ -4,6 +4,7 @@ import { Alert, Confirm, Notification } from '../UpdateHTML'
44import { memoize } from '../Utility'
55import { products , subscriptionProducts } from './CartTab'
66import { addToCart , clearCart , getPrice , getProductsInCart , getQuantity , removeFromCart } from './CartUtil'
7+ import { initializePayPal_Subscription } from './SubscriptionsSubtab'
78import { updatePseudoCoins } from './UpgradesSubtab'
89
910const tab = document . querySelector < HTMLElement > ( '#pseudoCoins > #cartContainer' ) !
@@ -98,7 +99,7 @@ export const initializeCheckoutTab = memoize(() => {
9899 checkoutStripe ?. addEventListener ( 'click' , submitCheckout )
99100 checkoutNowPayments ?. addEventListener ( 'click' , submitCheckout )
100101
101- initializePayPal ( '#checkout-paypal' )
102+ initializePayPal_OneTime ( '#checkout-paypal' )
102103} )
103104
104105function addItem ( e : MouseEvent ) {
@@ -177,55 +178,59 @@ const updateTotalPriceInCart = () => {
177178 totalCost ! . textContent = `${ formatter . format ( getPrice ( ) / 100 ) } USD`
178179}
179180
180- async function initializePayPal ( selector : string | HTMLElement ) {
181- try {
182- const paypal = await loadScript ( {
183- clientId : 'AS1HYTVcH3Kqt7IVgx7DkjgG8lPMZ5kyPWamSBNEowJ-AJPpANNTJKkB_mF0C4NmQxFuWQ9azGbqH2Gr' ,
184- disableFunding : [ 'paylater' , 'credit' , 'card' , 'venmo' ]
185- } )
186-
187- paypal ?. Buttons ?. ( {
188- style : {
189- shape : 'rect' ,
190- layout : 'vertical' ,
191- color : 'gold' ,
192- label : 'paypal'
193- } ,
194-
195- async createOrder ( ) {
196- const fd = new FormData ( )
197-
198- for ( const product of getProductsInCart ( ) ) {
199- if ( product . quantity > 0 && product . subscription ) {
200- throw new TypeError ( 'skipping' )
201- }
202-
203- fd . set ( product . id , ` ${ product . quantity } ` )
181+ /**
182+ * https://stackoverflow.com/a/69024269
183+ */
184+ async function initializePayPal_OneTime ( selector : string | HTMLElement ) {
185+ const paypal = await loadScript ( {
186+ clientId : 'AS1HYTVcH3Kqt7IVgx7DkjgG8lPMZ5kyPWamSBNEowJ-AJPpANNTJKkB_mF0C4NmQxFuWQ9azGbqH2Gr' ,
187+ disableFunding : [ 'paylater' , 'credit' , 'card' , 'venmo' ] ,
188+ dataNamespace : 'paypal_one_time'
189+ } )
190+
191+ paypal ?. Buttons ?. ( {
192+ style : {
193+ shape : 'rect' ,
194+ layout : 'vertical' ,
195+ color : 'gold' ,
196+ label : 'paypal'
197+ } ,
198+
199+ async createOrder ( ) {
200+ const fd = new FormData ( )
201+
202+ for ( const product of getProductsInCart ( ) ) {
203+ if ( product . quantity > 0 && product . subscription ) {
204+ throw new TypeError ( 'skipping' )
204205 }
205206
206- fd . set ( 'tosAgree' , radioTOSAgree . checked ? 'on' : 'off' )
207- const url = 'https://synergism.cc/paypal/orders/create'
207+ fd . set ( product . id , ` ${ product . quantity } ` )
208+ }
208209
209- const response = await fetch ( url , {
210- method : 'POST' ,
211- body : fd
212- } )
210+ fd . set ( 'tosAgree' , radioTOSAgree . checked ? 'on' : 'off' )
211+ const url = 'https://synergism.cc/paypal/orders/create'
213212
214- const orderData = await response . json ( )
213+ const response = await fetch ( url , {
214+ method : 'POST' ,
215+ body : fd
216+ } )
215217
216- if ( orderData . id ) {
217- return orderData . id
218- }
218+ const orderData = await response . json ( )
219219
220- const errorDetail = orderData ?. details ?. [ 0 ]
221- const errorMessage = errorDetail
222- ? `${ errorDetail . issue } ${ errorDetail . description } (${ orderData . debug_id } )`
223- : JSON . stringify ( orderData )
220+ if ( orderData . id ) {
221+ return orderData . id
222+ }
223+
224+ const errorDetail = orderData ?. details ?. [ 0 ]
225+ const errorMessage = errorDetail
226+ ? `${ errorDetail . issue } ${ errorDetail . description } (${ orderData . debug_id } )`
227+ : JSON . stringify ( orderData )
224228
225- throw new Error ( errorMessage )
226- } ,
229+ throw new Error ( errorMessage )
230+ } ,
227231
228- async onApprove ( data , actions ) {
232+ async onApprove ( data , actions ) {
233+ try {
229234 const url = `https://synergism.cc/paypal/orders/${ data . orderID } /capture`
230235
231236 const response = await fetch ( url , { method : 'POST' } )
@@ -240,9 +245,7 @@ async function initializePayPal (selector: string | HTMLElement) {
240245 return actions . restart ( )
241246 } else if ( errorDetail ) {
242247 // (2) Other non-recoverable errors -> Show a failure message
243- throw new Error (
244- `${ errorDetail . description } (${ orderData . debug_id } )`
245- )
248+ throw new Error ( `${ errorDetail . description } (${ orderData . debug_id } )` )
246249 } else if ( ! orderData . purchase_units ) {
247250 throw new Error ( JSON . stringify ( orderData ) )
248251 } else {
@@ -263,16 +266,26 @@ async function initializePayPal (selector: string | HTMLElement) {
263266
264267 exponentialPseudoCoinBalanceCheck ( )
265268 }
266- } ,
269+ } finally {
270+ initializePayPal_Subscription ( )
271+ }
272+ } ,
273+
274+ onError ( error ) {
275+ const message = [ ]
267276
268- onError ( error ) {
269- Notification ( 'An error with PayPal happened. More info in console.' )
270- console . log ( error )
277+ for ( const [ key , value ] of Object . entries ( error ) ) {
278+ message . push ( `${ key } : ${ value } ` )
271279 }
272- } ) . render ( selector )
273- } catch ( e ) {
274- console . error ( e )
275- }
280+
281+ Notification ( `An error with PayPal happened. More info in console. ${ message . join ( ', ' ) } ` )
282+ console . log ( error )
283+ } ,
284+
285+ onCancel ( ) {
286+ initializePayPal_Subscription ( )
287+ }
288+ } ) . render ( selector )
276289}
277290
278291const sleep = ( delay : number ) => new Promise ( ( r ) => setTimeout ( r , delay ) )
0 commit comments