vue-integration's People
vue-integration's Issues
paypal-button-container
Cannot hide paypal-button-container when paid changed to true.
Request for Help with Integrating PayPal in Vue.js 3 Project
Hi everyone,
I've been struggling to find useful Vue.js 3 examples for integrating PayPal into real projects. It seems like everything available is only partially implemented. It would be fantastic if developers using Vue.js had the opportunity to seamlessly integrate PayPal with the help of good and comprehensive examples. This would undoubtedly benefit not only us but also PayPal.
I, along with many other developers, would greatly appreciate at least one example that we could use as a foundation for real projects.
Specifically, what I'm missing at this point is the ability to validate user input in the form as soon as the user clicks on a PayPal button. The payment window should only open if the entered data complies with all rules (e.g., Laravel Request Validation). Otherwise, an error message should be displayed (e.g., Laravel Request Validation Errors). If my approach is incorrect, I would appreciate a proper example that I can also use in practice.
Thank you for any code snippets you can provide!
Here is the structure I have so far. I've created a separate component for the PayPal buttons (with the idea of having a reusable component, possibly a wrong approach?).
The content of the file looks like this:
<template>
<div>
<div v-if="!paid" id="paypal-button-container"></div>
</div>
</template>
<script>
import { loadScript } from '@paypal/paypal-js';
import axios from 'axios';
export default {
name: 'PaypalButtons',
emits: [
'orderApproved',
],
props: {
item: {
type: Object,
required: true,
},
form: {
type: Object,
required: true,
}
},
data() {
return {
paid: false,
ppOptions: {
'client-id': import.meta.env.VITE_PAYPAL_SANDBOX_CLIENT_ID,
locale: import.meta.env.VITE_PAYPAL_LOCALE,
currency: import.meta.env.VITE_PAYPAL_CURRENCY,
},
ppActions: null,
};
},
methods: {
createOrder(data, actions) {
console.log('Creating order...');
return axios.post(route('donation.create-order'), data)
.then(res => {
// TODO: Call backend
})
.catch((error) => {
console.log("An error has occurred.")
});
},
onApprove(data, actions) {
console.log('Order approved...');
return axios.post(route('donation.capture-order'), data)
.then(res => {
this.paid = true;
this.$emit('orderApproved', res.data);
console.log('Order complete!');
})
.catch((error) => {
console.log("An error has occurred.")
});
},
// Called when the button first renders.
onInit(data, actions) {
this.ppActions = actions;
// Disable the payment buttons.
this.ppActions.disable();
},
// Called when the button is clicked.
onClick(data, actions) {
console.log('Button is clicked.');
// According to the PayPal documentation, the user input could be validated here. But what is the correct procedure here?
let valid = false;
// Enable or disable the button when it is checked or unchecked.
if (valid) {
this.ppActions.enable();
return actions.resolve();
} else {
this.ppActions.disable();
return actions.reject();
}
},
// Handles any errors that occur while the payer submits the form.
onError(error) {
// Handle the error object
console.error(error);
// TODO: Informs the user about an error that has occurred. Output of a message in a pop-up window or similar.
},
// Buyer cancels a payment.
onCancel(data) {
console.info('Payment cancelled.');
// TODO: Inform the user about the cancelled process. Output of a message in a pop-up window or similar.
},
},
beforeMount() {
loadScript(this.ppOptions)
.then((paypal) => {
paypal
.Buttons({
createOrder: this.createOrder,
onApprove: this.onApprove,
onInit: this.onInit,
onClick: this.onClick,
onError: this.onError,
onCancel: this.onCancel,
style: {
shape: 'pill',
},
})
.render('#paypal-button-container');
})
.catch((err) => {
console.error("failed to load the PayPal JS SDK script", err);
});
},
};
</script>
This is how I plan to use the component:
<PaypalButtons :item="form.flower" :form="form" @order-approved="donationSuccessful"></PaypalButtons>
I would be very grateful for any contribution, no matter how small. And thank you already now on behalf of all Vuejs developers who are interested in the PayPal integration.
P.S.: I've already looked through the official PayPal documentation multiple times, but so far, it hasn't been of much help.
Upgrade to Vue 3
Move the codebase to use Vite and Vue 3 instead of Vue CLI(Webpack) and Vue 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. ๐๐๐
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google โค๏ธ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.