0

I am trying to implement a paypal checkout with vue 3 but i keep getting the same error.

Uncaught TypeError: undefined has no properties

For reference i'm trying to do the same code as this link.

Following what they suggested in: this link.

Here is my code:

<template>
    <div id="smart-button-container">
        <div style="text-align: center;">
            <div id="paypal-button-container"></div>
        </div>
    </div>
    <div ref="paypal"></div>
</template>

And my script:

<script>

    export default {
        name: "Pay-ment",
        data: ()=>{
            return {
                loaded: false,
                paidFor: false,
                product: {
                    price: 5000.00,
                    description: "product description"
                }
            }
        },
        mounted: ()=>{
            const script = document.createElement("script");
            script.srcset = ""
            script.src = "https://www.paypal.com/sdk/js?client-id=MY-CLIENT-ID";
            script.addEventListener("load", ()=> this.setLoaded);
            document.body.appendChild(script);
        },
        methods: {
            setLoaded: ()=>{
                this.loaded = true;
                window.paypal
                .Button({
                    createOrder: (data, actions)=>{
                    return actions.order.create({
                        purchase_units: [
                            {
                                description: this.product.description,
                                amount: {
                                    currency_code: "MXN",
                                    value: this.product.price
                                }
                            }
                        ]
                    })
                    }
                }).render(this.$refs.paypal);
            }
        }
    }
</script>
tony19
  • 99,316
  • 15
  • 147
  • 208
Ezequiel
  • 69
  • 1
  • 8
  • What line is the error from? Did the SDK script not load successfully? Is `window.paypal` undefined? Better to load the script at page load time than from a component. There is vue sample code at https://developer.paypal.com/docs/checkout/standard/customize/single-page-app/#vue – Preston PHX Apr 06 '22 at 18:10
  • @PrestonPHX It says mounted and then the component.vue and the line pointing at the addEventListener line – Ezequiel Apr 06 '22 at 18:16
  • Does this answer your question? [Use arrow function in vue computed does not work](https://stackoverflow.com/questions/42971081/use-arrow-function-in-vue-computed-does-not-work) – tony19 Apr 06 '22 at 20:14
  • Don't try to declare your `methods`, `data`, or `mounted` hook with arrow functions. Use regular functions instead. – tony19 Apr 06 '22 at 20:15

0 Answers0