|
77 | 77 | this.form = this.$add_payment_form; |
78 | 78 | } |
79 | 79 |
|
80 | | - // Pay for order ( change_payment_method for subscriptions) |
81 | | - if ( this.$order_pay_form.length ) { |
82 | | - if ( wc_paypal_form.is_paypal_selected() ) { |
83 | | - wc_paypal_form.init_checkout_paypal(); |
| 80 | + // Pay for order ( change_payment_method for subscriptions) |
| 81 | + if ( this.$order_pay_form.length ) { |
| 82 | + this.is_order_pay = true; |
| 83 | + this.form = this.$order_pay_form; |
| 84 | + // Form reference set |
| 85 | + if ( wc_paypal_form.is_paypal_selected() ) { |
| 86 | + wc_paypal_form.init_checkout_paypal(); |
| 87 | + } |
84 | 88 | } |
85 | | - this.is_order_pay = true; |
86 | | - this.form = this.$order_pay_form; |
87 | | - // Form reference set |
88 | | - } |
89 | 89 |
|
90 | | - if ( this.form ) { |
91 | | - this.form.on( 'change', this.on_change ); |
92 | | - } |
93 | | - }, |
94 | | - on_change() { |
95 | | - // Triggers on payment method selection. |
96 | | - $( "[name='payment_method']" ).on( 'change', function () { |
97 | | - wc_paypal_form.on_payment_selected(); |
98 | | - } ); |
99 | | - }, |
100 | | - on_payment_selected() { |
| 90 | + if ( this.form ) { |
| 91 | + this.form.on( 'change', this.on_change ); |
| 92 | + } |
| 93 | + }, |
| 94 | + on_change() { |
| 95 | + // Triggers on payment method selection. |
| 96 | + $( "[name='payment_method']" ).on( 'change', function () { |
| 97 | + wc_paypal_form.on_payment_selected(); |
| 98 | + } ); |
| 99 | + }, |
| 100 | + on_payment_selected() { |
101 | 101 | if ( wc_paypal_form.is_paypal_selected() ) { |
102 | 102 | wc_paypal_form.init_checkout_paypal(); |
103 | 103 | if ( wc_paypal_form.is_checkout ) { |
|
133 | 133 | wc_paypal_form.instantiate_payment_request(); |
134 | 134 | }, |
135 | 135 | instantiate_payment_request() { |
136 | | - const paymentRequest = monei.PaymentRequest( { |
| 136 | + const paymentRequest = monei.PayPal( { |
137 | 137 | accountId: wc_paypal_params.account_id, |
138 | 138 | sessionId: wc_paypal_params.session_id, |
139 | 139 | amount: parseInt( wc_paypal_form.total ), |
140 | 140 | currency: wc_paypal_params.currency, |
141 | 141 | language: wc_paypal_params.language, |
142 | | - paymentMethod: 'paypal', |
143 | | - style: wc_paypal_params.paypal_style || {}, |
| 142 | + style: wc_paypal_params.paypal_style || {}, |
144 | 143 | onSubmit( result ) { |
145 | 144 | $( '#place_order' ).prop( 'disabled', false ); |
146 | 145 | wc_paypal_form.request_token_handler( result.token ); |
|
149 | 148 | // Error handling is managed by MONEI SDK |
150 | 149 | }, |
151 | 150 | } ); |
152 | | - // Render an instance of the Payment Request Component into the `payment_request_container` <div>. |
| 151 | + // Render an instance of the PayPal Component into the `paypal-container` <div>. |
153 | 152 | paymentRequest.render( '#paypal-container' ); |
154 | 153 | // Assign a global variable to paymentRequest so it's accessible. |
155 | 154 | window.paypalRequest = paymentRequest; |
156 | 155 | }, |
157 | 156 | init_checkout_paypal() { |
| 157 | + // Check if container exists, create if needed (for order-pay page) |
| 158 | + let container = document.getElementById( 'paypal-container' ); |
| 159 | + if ( ! container ) { |
| 160 | + // Create container structure if it doesn't exist |
| 161 | + const paymentMethodLi = document.querySelector( '#payment_method_monei_paypal' )?.closest( 'li' ); |
| 162 | + if ( ! paymentMethodLi ) { |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + // Create the container structure |
| 167 | + const fieldset = document.createElement( 'fieldset' ); |
| 168 | + fieldset.id = 'wc-monei_paypal-form'; |
| 169 | + fieldset.className = 'wc-paypal-form'; |
| 170 | + fieldset.style.background = 'transparent'; |
| 171 | + fieldset.style.border = 'none'; |
| 172 | + |
| 173 | + container = document.createElement( 'div' ); |
| 174 | + container.id = 'paypal-container'; |
| 175 | + |
| 176 | + fieldset.appendChild( container ); |
| 177 | + paymentMethodLi.appendChild( fieldset ); |
| 178 | + } |
| 179 | + |
158 | 180 | // If checkout is updated (and monei was initiated already), ex, selecting new shipping methods, checkout is re-render by the ajax call. |
159 | 181 | // and we need to reset the counter in order to initiate again the monei component. |
160 | 182 | if ( |
|
0 commit comments