11"use strict" ;
22
3- // requires newsletter.js
4-
53class DesktopLicense {
64
75 constructor ( form , checkoutData ) {
86 this . _form = form ;
97 this . _checkoutData = checkoutData ;
108 this . _paddle = $ . ajax ( {
11- url : 'https://cdn.paddle.com/paddle/paddle.js' ,
9+ url : 'https://cdn.paddle.com/paddle/v2/ paddle.js' ,
1210 cache : true ,
1311 dataType : 'script'
1412 } ) . then ( ( ) => {
1513 if ( PADDLE_ENABLE_SANDBOX ) {
1614 window . Paddle . Environment . set ( 'sandbox' ) ;
1715 }
18- window . Paddle . Setup ( { vendor : PADDLE_VENDOR_ID } ) ;
16+ window . Paddle . Initialize ( {
17+ token : PADDLE_TOKEN ,
18+ eventCallback : data => {
19+ if ( data . name == "checkout.completed" ) {
20+ this . onCheckoutSucceeded ( ) ;
21+ } else if ( data . name == "checkout.closed" ) {
22+ this . _checkoutData . inProgress = false ;
23+ }
24+ }
25+ } ) ;
1926 return window . Paddle ;
2027 } ) ;
2128 }
2229
2330 loadPrice ( ) {
24- $ . ajax ( {
25- url : PADDLE_PRICES_URL ,
26- dataType : 'jsonp' ,
27- data : {
28- product_ids : PADDLE_DESKTOP_PRODUCT_IDS . join ( ',' )
29- } ,
30- } ) . done ( data => {
31- this . _checkoutData . prices = data . response . products . map ( product => {
32- return {
33- productId : product . product_id ,
34- amount : product . price . gross ,
35- listAmount : product . list_price . gross ,
36- currency : product . currency
37- }
31+ this . _paddle . then ( paddle => {
32+ let request = {
33+ items : PADDLE_DESKTOP_PRICE_IDS . map ( priceId => ( { priceId : priceId , quantity : 1 } ) )
34+ } ;
35+ paddle . PricePreview ( request ) . then ( result => {
36+ this . _checkoutData . prices = result . data . details . lineItems . map ( item => {
37+ return {
38+ priceId : item . price . id ,
39+ currencyCode : result . data . currencyCode ,
40+ amount : item . totals . total ,
41+ formattedAmount : item . formattedTotals . total
42+ }
43+ } ) ;
44+ } ) . catch ( ( ) => {
45+ this . _checkoutData . errorMessage = 'Retrieving prices failed. Please try again later.' ;
3846 } ) ;
47+ // let discountedRequest = {
48+ // items: [{ priceId: PADDLE_DESKTOP_PRICE_IDS[0], quantity: 1 }],
49+ // discountId: 'dsc_',
50+ // };
51+ // paddle.PricePreview(discountedRequest).then(discountedResult => {
52+ // if (Number(discountedResult.data.details.lineItems[0].totals.discount) > 0) {
53+ // this._checkoutData.discountedPrice = {
54+ // amount: discountedResult.data.details.lineItems[0].totals.total,
55+ // formattedAmount: discountedResult.data.details.lineItems[0].formattedTotals.total
56+ // };
57+ // }
58+ // }).catch(() => {
59+ // this._checkoutData.errorMessage = 'Retrieving discounted price failed. Please try again later.';
60+ // });
3961 } ) ;
4062 }
4163
42- checkout ( productId , locale ) {
64+ checkout ( priceId , locale ) {
4365 if ( ! $ ( this . _form ) [ 0 ] . checkValidity ( ) ) {
4466 $ ( this . _form ) . find ( ':input' ) . addClass ( 'show-invalid' ) ;
4567 this . _checkoutData . errorMessage = 'Please fill in all required fields.' ;
@@ -51,33 +73,9 @@ class DesktopLicense {
5173 this . _checkoutData . success = false ;
5274 this . _paddle . then ( paddle => {
5375 paddle . Checkout . open ( {
54- product : productId ,
55- email : this . _checkoutData . email ,
56- quantity : this . _checkoutData . quantity ,
57- locale : locale ,
58- successCallback : data => {
59- this . onCheckoutSucceeded ( ) ;
60- this . getPaddleOrderDetails ( data . checkout . id ) ;
61- if ( this . _checkoutData . acceptNewsletter ) {
62- subscribeToNewsletter ( data . user . email , 6 ) ;
63- }
64- } ,
65- closeCallback : ( ) => {
66- this . _checkoutData . inProgress = false ;
67- }
68- } ) ;
69- } ) ;
70- }
71-
72- getPaddleOrderDetails ( checkoutId ) {
73- this . _paddle . then ( paddle => {
74- paddle . Order . details ( checkoutId , data => {
75- let licenseKey = data . lockers ?. [ 0 ] ?. license_code ;
76- if ( licenseKey ) {
77- this . _checkoutData . licenseKey = licenseKey ;
78- } else {
79- this . _checkoutData . errorMessage = 'Retrieving supporter certificate failed. Please check your emails instead.' ;
80- }
76+ settings : { locale : locale } ,
77+ items : [ { priceId : priceId , quantity : this . _checkoutData . quantity } ] ,
78+ customer : { email : this . _checkoutData . email }
8179 } ) ;
8280 } ) ;
8381 }
0 commit comments