Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chron's avatar
Level 6

Where should I put an api call?

I'm using Vue2, axios, and paypal api.

Here is the code.

    export default {
        data() {
            return {
                amount: 0,
                user: "",
            }
        },
        mounted() {
            paypal.Buttons({
                style: {
                    layout: 'horizontal',
                    label: 'pay',
                    tagline: 'false'
                },
                createOrder: (data, actions) => {
                    return actions.order.create({
                        purchase_units: [{
                            amount: {
                                value: this.amount,
                            }
                        }]
                    });
                },
                onApprove: (data, actions) => {
                    return actions.order.capture().then(details => {
                        axios.post('/pay-now', {
                            payer_id: this.user,
                            amount: details.purchase_units[0].amount.value,
                        })
                        .then(res => {
                            window.location.replace("/finish-order");
                        })
                        .catch(err => console.log(err))
                    });
                }
            }).render('#btn');
        },
        created() {
             axios.get('/api')
                .then(res => {
                    this.user = res.data.user;
                    this.amount = res.data.amount;
                })
                .catch(err => console.log(err))
        }
    }

I want to make sure that the data is done loading before the paypal button gets rendered. Where should I put the paypal api?

0 likes
3 replies
Swapnil's avatar

You could do something like this.

export default {
    data() {
        return {
            amount: 0,
            user: "",
        }
    },
    mounted() {
        axios.get('/api')
            .then(res => {
                this.user = res.data.user;
                this.amount = res.data.amount;
                this.preparePaypalButtons();
            })
            .catch(err => console.log(err))  
    },
    methods: {
        preparePaypalButtons() {
            paypal.Buttons({
                style: {
                    layout: 'horizontal',
                    label: 'pay',
                    tagline: 'false'
                },
                createOrder: (data, actions) => {
                    return actions.order.create({
                        purchase_units: [{
                            amount: {
                                value: this.amount,
                            }
                        }]
                    });
                },
                onApprove: (data, actions) => {
                    return actions.order.capture().then(details => {
                        axios.post('/pay-now', {
                            payer_id: this.user,
                            amount: details.purchase_units[0].amount.value,
                        })
                        .then(res => {
                            window.location.replace("/finish-order");
                        })
                        .catch(err => console.log(err))
                    });
                }
            }).render('#btn');
        }
    }
}
Chron's avatar
Level 6

I got an error: paypal is not defined, but I don't get the error when I put the button renderer in mounted.

Peppermintology's avatar

Have you considered using a loading state? This could either be two views:

<div v-if="isLoading">Please wait ...</div>
<div v-if=!isLoading"> ... </div>

Or you could disable/hide the Paypal button using the same principle.

Please or to participate in this conversation.