PayPal Checkout 2.0 - Monetize React, Angular, & Vue Quickly

Fireship · Beginner ·🌐 Frontend Engineering ·7y ago

Key Takeaways

The video demonstrates how to use PayPal Checkout 2.0 APIs to accept and capture payments entirely from frontend code in React, Angular, and Vue applications, with revamped documentation and API resembling Stripe payments. It provides a step-by-step guide on how to integrate PayPal Checkout 2.0 with these frameworks, including generating API keys, mounting payment buttons, and handling payment lifecycles.

Full Transcript

version two of the paypal api was released earlier this year and it makes the extremely easy to start accepting payments from your progressive web app it's so easy in fact that I can show you how to integrate it into angular react and view in a single video dollar dollar bill y'all in today's video you'll learn the basics of the paypal version two API and how to start accepting and capturing real payments from your front-end JavaScript app and it's a lot easier than you might think if you're new here like and subscribe and you can find the full source code on fire ship io now just a few days ago I implemented PayPal payments on fire ship and also added the ability to purchase individual courses so ironically you can now use PayPal to pay for my strength payments course if you want now during this integration I learned that PayPal 2.0 delivers a really nice developer experience they've completely revamped the documentation and the API in a way that resembles stripe payments now in my case I already have stripe implemented so I'm using PayPal alongside it as an additional option but you can actually use PayPal as a standalone payment solution to accept credit cards venmo crypto and more well we'll be looking at today as PayPal checkout which allows you to mount this payment button directly in your application and it will take care of collecting payment details from the user and can also capture and charge the payment entirely from the front end the first thing you'll need to get started is a PayPal business account from there you can go to the PayPal developers dashboard and the first thing you'll want to do is create an application when you create this application it will give you two sets of API keys these sandbox keys are used to make mock payments and to test your integration and live keys are used for actual payments in your production app now each of these environments contains two API keys the client ID and the secret the client ID is perfectly safe to expose in your front-end application and we'll use that client ID in the angular react and view apps the secret on the other hand should never be exposed in your front-end code or a public github repo a secret is used to identify your app to a back-end server like node or firebase cloud functions so you can use it to interact with the API from the backend but we actually don't even need to use it in this demo and that's because you can use PayPal checkout to do everything entirely from the front-end and we'll talk a little bit more about the trade-offs later in the video now we're going to implement the same basic payment spec in angular reactant view and even though each app is solving the same exact problem the implementation details vary widely let's go ahead and get started with my personal favorite and generate a new angular app with the CLI the first thing we'll do is go into the XHTML file and include the script tag for paypal Jas when you include the script tag you'll also need to pass in your client ID and you want to stick to the sandbox client ID for now and again you can retrieve that from the PayPal developer dashboard now a quick side note you might be wondering why we're using a script tag and not installing an NPM module the reason payment systems generally use script tags is because they want your site to always have the latest version of the SDK a security flaw on a payment system is a very big deal so they want to make sure that you're not running outdated code the next thing I'll do is generate a component then in the top of that component I'll declare a variable for PayPal that PayPal script creates a global variable called PayPal that lives on the window object and that's how we make it known to typescript the first thing that PayPal needs to do is Mount the actual button to the Dom so we need to grab an HTML element that PayPal can attach itself to in angular we can do that with the view child decorator and then pass in the name of the template reference variable and we can make that reference in the HTML with a hash tag now in these examples we're just going to hard code the product data directly into the components but in real life you'll most likely be pulling this information from an API or a database somewhere in this example here we just need some data for the product and then we'll set a boolean that will tell us whether or not that products been paid for when the component is initialized is when we'll mount the PayPal button to the Dom element so we call PayPal buttons and then pass in the native Dom element to the render method we can pass an object to buttons that contains the callbacks used to handle different events that happen in the payment lifecycle the first thing that happens in the lifecycle is the user clicks on the PayPal button and we need to configure the order details to send a paypal so they can process the payment so we call actions or to create and we can pass in a bunch of custom options here but at the bare minimum you'll want to have at least one purchase unit and that should at least contain a description and an amount with a currency code this will send the user over to PayPal where they can authorize the order they can check out as a guest with their credit card details or they can use their PayPal balance or some other form of payment on their account at this point the order is authorized and approved but the actual funds have not been captured yet or in other words the credit card has not been charged for most simple use cases you can simply capture the payment directly in your front-end code by calling actions order capture now it's important to point out that you don't actually need to capture the funds here if you don't want to an alternative would be to send the data from the call back to your back-end server and then process the charge there and that tends to be a better option for more advanced use cases where you might need to validate the payment and then do additional backend work after the payment has been captured we're just going to stick with the EZ wrap here and capture the payment directly an angular and then we'll set the paid for property to true and that's something that you would obviously want to persist in a back-end as well in a real application and the last thing I'll point out is that there are additional callbacks we can use here as well for example if we want to catch errors we can use on error to do that that takes care of our typescript code now we can go over to the HTML and set up our template logic we'll use ng if to see if the item has not been paid for and if not we'll go ahead and show the price and product details as well as the PayPal button and if it has been paid for then we'll just go ahead and show an order confirmation and just like that we now have a full stack payment solution in angular now one thing I really like about PayPal is that you can set up multiple mock accounts and then you could add funds to those mock accounts as well as credit cards and bank accounts and things like that so it makes it really easy to test your implementation details in the sandbox and that also makes it easy to perform negative testing where you handle different error conditions in our demo here you can see we have a successful mock payment and then at console logs the payment details to the browser and you can send this data to your back-end to record in a database or send a user a confirmation email and stuff like that that takes care of our angular integration now let's move on to react I'll use create react app to generate a new application and we'll want to build this app the modern way so we'll go ahead and import some hooks like you state use ref and use a fact now including a script tag and react isn't quite as easy as it is an angular and we need to manually create the script and then append it to the Dom so we have two different pieces of state the first one paid for will tell us whether or not the product has been paid for and then we'll have a loaded State that will tell us when the actual paypal script has been loaded and if you know of a better way to do this with react hooks let me know in the comments then we'll also use the use ref hooks so we can set up a reference to a Dom element that the actual PayPal button can be mounted to now at this point I'm going to jump into the JSX and build out the template this fall is the same general structure that we saw in angular if the item has been paid for then we'll show the confirmation otherwise we'll go ahead and show the actual product details and the payment button and to grab an empty div from the Dom for the button we'll go ahead and use that reference that we defined with the use ref hook now the next thing we need to do is get the actual paypal J script into our code we can do that imperative Lee in the use effect hook and then we'll define the script source as the same source that we used in the angular app but this time we want to add an event listener to the load event and then when it's finished loading will set the loaded property to true on this react component once we know that the scripts been loaded we can then initialize our paypal button but for whatever reason I had to wrap this in a set timeout because there seems to be some weird issue between one of the dependencies in PayPal Jas and react but feel free to submit a pull request if you have a good workaround for this demo now at this point we can access PayPal from the window object and then we'll have the exact same code that we wrote previously in the angular example the only difference is that we're using react hooks to update the state and to grab the Dom element to mount the actual button itself and that's all it takes to get started with react and if you check out the PayPal J's repo you'll see an example for react in there as well but it doesn't use hooks but enough of react let's go ahead and move on to view we can start by generating a new view app with the view CLI and then we'll start by defining the template because it's conveniently located at the top of the file the template logic is very similar to angular because view also uses directives so again we'll show the item details if the item is not paid for and then if it is paid for we'll show the order confirmation and then we'll also make a reference to an empty div with a ref of PayPal the next thing we'll do is define the data for this component which is exactly the same as react but instead of hooks we use a data function that returns an object with the data that we use in this component and view will react automatically if we change any of the properties on this object the next thing we'll do is append the paypal script to this application in the mounted life-cycle hook this is the exact same thing we did and they use effect hook with react when the script is loaded we'll have access to the global paypal object and then we'll mount it to that empty div that we made a reference to earlier from there the callbacks will be the same as the previous examples where we first create the order and then we capture the order and handle any errors if necessary and that's all it takes to implement paypal in a view app well that was easy yeah I guess it was my goal with these three examples was to show you how easy it is to start monetizing your progressive web app but if you want to learn more advanced techniques consider becoming a pro member to enroll in the stripe payments master course you'll learn how to build and test a reliable payment system that can work with any front-end framework thanks for watching and I will talk to you soon [Music]

Original Description

Learn how to use the new PayPal Checkout 💰 2.0 APIs to accept and capture payments entirely from your frontend code, including demos for React, Angular, and Vue https://fireship.io - PayPal 2.0 https://developer.paypal.com/docs/api/overview/ - Code https://github.com/fireship-io/193-paypal-checkout-v2-demos #paypal #webdev #money Take the Fireship quizzes 🤓 iOS https://itunes.apple.com/us/app/fireship/id1462592372?mt=8 Android https://play.google.com/store/apps/details?id=io.fireship.quizapp Upgrade to Fireship PRO at https://fireship.io/pro Use code lORhwXd2 for 25% off your first payment.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Fireship · Fireship · 0 of 60

← Previous Next →
1 Angular 4 Development and Production Environments with Firebase
Angular 4 Development and Production Environments with Firebase
Fireship
2 OAuth with Angular and Firebase Tutorial
OAuth with Angular and Firebase Tutorial
Fireship
3 Anonymous Authentication with Angular and Firebase - Lazy Registration
Anonymous Authentication with Angular and Firebase - Lazy Registration
Fireship
4 Angular Router Guards for Firebase Users
Angular Router Guards for Firebase Users
Fireship
5 Angular Firebase CRUD App with NoSQL Database Tutorial
Angular Firebase CRUD App with NoSQL Database Tutorial
Fireship
6 Upload Files from Angular to Firebase Storage
Upload Files from Angular to Firebase Storage
Fireship
7 How to Deploy an Angular App to Firebase Hosting
How to Deploy an Angular App to Firebase Hosting
Fireship
8 Sharing Data between Components in Angular
Sharing Data between Components in Angular
Fireship
9 Loading Spinners for Asynchronous Firebase Data
Loading Spinners for Asynchronous Firebase Data
Fireship
10 Angular 4 Transactional Email with Google Firebase Cloud Functions
Angular 4 Transactional Email with Google Firebase Cloud Functions
Fireship
11 Firebase Database Rules Tutorial
Firebase Database Rules Tutorial
Fireship
12 Autocomplete Search with Angular4 and Firebase
Autocomplete Search with Angular4 and Firebase
Fireship
13 Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Reddit Inspired Upvoting System with Angular and Firebase NoSQL
Fireship
14 Angular Drag-and-Drop File Uploads to Firebase Storage
Angular Drag-and-Drop File Uploads to Firebase Storage
Fireship
15 Text Translation with Firebase Cloud Functions onWrite and Angular 4
Text Translation with Firebase Cloud Functions onWrite and Angular 4
Fireship
16 Custom Usernames with Firebase Authentication
Custom Usernames with Firebase Authentication
Fireship
17 Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Twitter-Inspired Follow Unfollow Feature with Firebase and Angular 4
Fireship
18 Simple Pagination with Firebase and Angular 4
Simple Pagination with Firebase and Angular 4
Fireship
19 How to Connect Firebase Users to their Data - 3 Methods
How to Connect Firebase Users to their Data - 3 Methods
Fireship
20 Add Toast Message Notifications to your Angular App
Add Toast Message Notifications to your Angular App
Fireship
21 Facebook-Inspired Reactions System with Angular and Firebase
Facebook-Inspired Reactions System with Angular and Firebase
Fireship
22 Learn NgModule in Angular with Examples
Learn NgModule in Angular with Examples
Fireship
23 Lazy Loading Components in Angular 4
Lazy Loading Components in Angular 4
Fireship
24 Stripe Checkout Payments with Angular and Firebase - Part 1
Stripe Checkout Payments with Angular and Firebase - Part 1
Fireship
25 Process Stripe Payments with Firebase Cloud Functions - Part 2
Process Stripe Payments with Firebase Cloud Functions - Part 2
Fireship
26 Selling Digital Content in Angular with Stripe Payments - Part 3
Selling Digital Content in Angular with Stripe Payments - Part 3
Fireship
27 Angular 4 Full Text Search with Algolia - Part 1
Angular 4 Full Text Search with Algolia - Part 1
Fireship
28 Algolia with Firebase Cloud Functions - Part 2
Algolia with Firebase Cloud Functions - Part 2
Fireship
29 Firebase Phone Authentication in Angular 4
Firebase Phone Authentication in Angular 4
Fireship
30 Top 7 RxJS Concepts for Angular Developers
Top 7 RxJS Concepts for Angular Developers
Fireship
31 Learn Angular Animations with 5 Examples
Learn Angular Animations with 5 Examples
Fireship
32 Advanced Firebase Data Filtering (Multi-Property)
Advanced Firebase Data Filtering (Multi-Property)
Fireship
33 Realtime Maps with Mapbox + Firebase + Angular
Realtime Maps with Mapbox + Firebase + Angular
Fireship
34 Angular Reactive Forms with Firebase Database Backend
Angular Reactive Forms with Firebase Database Backend
Fireship
35 Send Push Notifications in Angular with Firebase Cloud Messaging
Send Push Notifications in Angular with Firebase Cloud Messaging
Fireship
36 Top 7 Ways to Debug Angular 4 Apps
Top 7 Ways to Debug Angular 4 Apps
Fireship
37 Infinite Scroll with Angular and Firebase
Infinite Scroll with Angular and Firebase
Fireship
38 Use TypeScript with Firebase Cloud Functions
Use TypeScript with Firebase Cloud Functions
Fireship
39 Realtime Graphs and Charts with Plotly and Firebase
Realtime Graphs and Charts with Plotly and Firebase
Fireship
40 Role-Based User Permissions in Firebase
Role-Based User Permissions in Firebase
Fireship
41 User Presence System in Realtime - Online, Offline, Away
User Presence System in Realtime - Online, Offline, Away
Fireship
42 Location-based Queries with GeoFire and Angular Google Maps
Location-based Queries with GeoFire and Angular Google Maps
Fireship
43 Angular ngrx Redux Quick Start Tutorial
Angular ngrx Redux Quick Start Tutorial
Fireship
44 Angular Ngrx Effects with Firebase Database
Angular Ngrx Effects with Firebase Database
Fireship
45 Progressive Web Apps with Angular
Progressive Web Apps with Angular
Fireship
46 Angular Ngrx with Firebase Google OAuth User Authentication
Angular Ngrx with Firebase Google OAuth User Authentication
Fireship
47 RxJS Quick Start with Practical Examples
RxJS Quick Start with Practical Examples
Fireship
48 Send SMS Text Messages with Twilio and Firebase
Send SMS Text Messages with Twilio and Firebase
Fireship
49 Firebase Database Performance Profiling
Firebase Database Performance Profiling
Fireship
50 Native Desktop Apps with Angular and Electron
Native Desktop Apps with Angular and Electron
Fireship
51 Subscription Payments with Stripe, Angular, and Firebase
Subscription Payments with Stripe, Angular, and Firebase
Fireship
52 Firestore with AngularFire5 Quick Start Tutorial
Firestore with AngularFire5 Quick Start Tutorial
Fireship
53 Angular HTTP Client Quick Start Tutorial
Angular HTTP Client Quick Start Tutorial
Fireship
54 Google Sign-In with Firestore Custom User Data
Google Sign-In with Firestore Custom User Data
Fireship
55 Star Review System from Scratch with Firestore + Angular
Star Review System from Scratch with Firestore + Angular
Fireship
56 Angular Chatbot with Dialogflow (API.ai)
Angular Chatbot with Dialogflow (API.ai)
Fireship
57 Learn @ngrx/entity and Feature Modules
Learn @ngrx/entity and Feature Modules
Fireship
58 Infinite Scroll Pagination with Firestore
Infinite Scroll Pagination with Firestore
Fireship
59 Faster Firestore via Data Aggregation
Faster Firestore via Data Aggregation
Fireship
60 Contentful - CMS for Angular Progressive Web Apps
Contentful - CMS for Angular Progressive Web Apps
Fireship

Learn how to use PayPal Checkout 2.0 to monetize React, Angular, and Vue applications quickly, with a focus on front-end integration and payment processing. The video provides a step-by-step guide on how to integrate PayPal Checkout 2.0 with these frameworks, including generating API keys and handling payment lifecycles.

Key Takeaways
  1. Generate a new Angular app with the CLI
  2. Include the script tag for PayPal Jas in the XHTML file and pass in the client ID
  3. Generate a component and declare a variable for PayPal
  4. Mount the actual button to the DOM using the view child decorator and template name
  5. Configure the order details to send to PayPal
  6. Capture the payment directly in the front-end code
💡 PayPal Checkout 2.0 allows developers to accept and capture payments entirely from frontend code, without needing to use the secret key, making it a convenient and secure payment solution for React, Angular, and Vue applications.

Related Reads

📰
Inside the Wayfair Frontend SDE-2 Interview: A Complete Breakdown
Learn how to prepare for a Frontend SDE-2 interview at Wayfair, including online assessments, machine coding, and system design.
Medium · Programming
📰
I Spent Two Years Maintaining a React SPA. HTMX Rebuilt It in a Week
Learn how HTMX rebuilt a React SPA in a week, replacing 2 years of maintenance work, and discover the benefits of this alternative approach
Medium · Programming
📰
The 5 Levels of Front End Engineering (And Where Most Developers Get Stuck)
Learn the 5 levels of front end engineering to improve your skills and avoid getting stuck in a career rut
Medium · Programming
📰
Browser-Based PDF Editing with Vue 3 and pdf-lib
Learn to build a browser-based PDF editor using Vue 3 and pdf-lib, enabling users to edit PDFs directly in the browser
Dev.to · sunshey
Up next
How to Use Semrush Keyword Magic Tool with ChatGPT to Make Money
Grow with Will - SEO, Sales & Entrepreneurship
Watch →