@@ -11,8 +11,12 @@ let submitButtonOne = document.querySelector('#submit-button-one');
1111let applePayButton = document . querySelector ( '#apple-pay-button' ) ;
1212let apmAmount = document . getElementById ( 'apm-amount' ) ;
1313let settings = JSON . parse ( document . getElementById ( 'customer-settings' ) . dataset . settings ) ;
14- let deviceData ;
15-
14+ let deviceData , localPaymentInstance ;
15+ let serverOptionsObject = {
16+ options : {
17+ submitForSettlement : true
18+ }
19+ } ;
1620let paypalOrderJsonEditor = document . getElementById ( 'paypal-order-json-editor' ) ;
1721let paypalOrderJson = {
1822 flow : 'checkout' ,
@@ -46,7 +50,57 @@ let customerJsonEditor = new JSONEditor(
4650 } ,
4751 paypalOrderJson
4852) ;
49- braintreePayments . animatePaymentForm ( ) ;
53+ braintreePayments . animatePaymentForm ( serverOptionsObject ) ;
54+
55+ function merchantAccountId ( )
56+ {
57+ return ( "devoralive" ) ;
58+ }
59+ function createLocalPaymentClickListener ( type )
60+ {
61+ return function ( event ) {
62+ event . preventDefault ( ) ;
63+ localPaymentInstance . startPayment ( {
64+ paymentType : type ,
65+ amount : apmAmount ,
66+ fallback : {
67+ url : 'https://google.es' ,
68+ buttonText : 'Complete Payment'
69+ } ,
70+ currencyCode : 'EUR' ,
71+ shippingAddressRequired : true ,
72+ email : 'joe@getbraintree.com' ,
73+ phone : '5101231234' ,
74+ givenName : 'Joe' ,
75+ surname : 'Doe' ,
76+ address : {
77+ streetAddress : 'Oosterdoksstraat 110' ,
78+ extendedAddress : 'Apt. B' ,
79+ locality : 'DK Amsterdam' ,
80+ postalCode : '1011' ,
81+ region : 'NH' ,
82+ countryCode : 'NL'
83+ } ,
84+ onPaymentStart : function ( data , start ) {
85+ start ( ) ;
86+ }
87+ } , function ( startPaymentError , payload ) {
88+ if ( startPaymentError ) {
89+ if ( startPaymentError . code === 'LOCAL_PAYMENT_POPUP_CLOSED' ) {
90+ console . error ( 'Customer closed Local Payment popup.' ) ;
91+ } else {
92+ console . error ( 'Error!' , startPaymentError ) ;
93+ }
94+ } else {
95+ braintreePayments . sendServerPayLoad ( payload , deviceData ) ;
96+ }
97+ } ) ;
98+ } ;
99+ }
100+ $ ( '#ideal-button' ) . on ( 'click' , createLocalPaymentClickListener ( 'ideal' ) ) ;
101+ $ ( '#sofort' ) . on ( 'click' , createLocalPaymentClickListener ( 'sofort' ) ) ;
102+ $ ( '#bancontact-button' ) . on ( 'click' , createLocalPaymentClickListener ( 'bancontact' ) ) ;
103+ $ ( '#trustly-button' ) . on ( 'click' , createLocalPaymentClickListener ( 'trustly' ) ) ;
50104
51105submitButtonOne . addEventListener ( 'click' , function ( ) {
52106 apmAmount = apmAmount . value ;
@@ -66,6 +120,16 @@ submitButtonOne.addEventListener('click', function () {
66120 } , function ( err , dataCollectorInstance ) {
67121 deviceData = JSON . parse ( dataCollectorInstance . deviceData ) ;
68122 } ) ;
123+ braintree . localPayment . create ( {
124+ client : clientInstance ,
125+ merchantAccountId : merchantAccountId ( )
126+ } , function ( localPaymentErr , paymentInstance ) {
127+ if ( localPaymentErr ) {
128+ console . error ( 'Error creating local payment:' , localPaymentErr ) ;
129+ return ;
130+ }
131+ localPaymentInstance = paymentInstance ;
132+ } ) ;
69133 if ( window . ApplePaySession && ApplePaySession . supportsVersion ( 3 ) && ApplePaySession . canMakePayments ( ) ) {
70134 braintree . applePay . create ( {
71135 client : clientInstance
@@ -156,5 +220,9 @@ submitButtonOne.addEventListener('click', function () {
156220 } ) . render ( '#apm-container' ) ;
157221 } ) ;
158222 } ) ;
223+ $ ( '#ideal-button' ) . removeClass ( 'd-none' ) ;
224+ $ ( '#sofort-button' ) . removeClass ( 'd-none' ) ;
225+ $ ( '#trustly-button' ) . removeClass ( 'd-none' ) ;
226+ $ ( '#bancontact-button' ) . removeClass ( 'd-none' ) ;
159227 } ) ;
160228} ) ;
0 commit comments