 Once you master Autofill, how would you like to make payments even easier? Hi, I'm Sarah Clark, and I'm ready to show you how to use the payment tools built into iOS and Android from your web app. The Payment Request API is a W3C standard to eliminate payment forms for users and standardized payment collections for sites. It allows us to request payment and shipping information with a single API call. This knows how to run Apple Pay on iOS Safari and Google Pay on Android. It's not limited to those two. Any payment provider can write a plug-in. It's not even restricted to one browser. It's probably in the browser you're using right now. The browser simply acts as an intermediary, collecting the information we need in a consistent way, then returns a JSON object with the results that we can use to collect the payment. If you have used a Pay with Apple Pay or Pay with Google Pay button in the browser, you already know what the experience will be like. Developers previously used custom libraries to implement this, such as the Apple Pay API or the Google Wallet API. And if you want to do that, too, great. But if you want to work with whatever payment system the user has, payment request is your friend. Let's take a quick look at the code flow and user experience. We want to buy this jacket. What happens when we tap Buy Now? This is an ordinary button with an ordinary event handler. First, the site creates a payment request object and populates it with some details. It typically has the price, a list of items being purchased, and the available forms of payment. This is all invisible to the user. We need to trigger the payment user interface. You get the UI by calling paymentrequest.show. This displays a native UI over the browser and returns a promise. The next few steps are all happening inside this promise. The first screen is a summary of the information supplied by the app. Tapping on an item opens it. The API stores addresses and payment options securely. In this example, Larry can select his shipping address with one tap, edit the address, or add another. Need to change the shipping option? No problem. It's just a tap away. Note that changing this option may change the total price. The payment request API can handle this with a little help from your app. Notice the multiple payment options. API also handles country-specific payment methods. And finally, the user taps on pay. At this point, the browser runs the selected payment service. This might be a nap on your device or going across the network to a payment provider. The completed payment returns a special token that you can send to your server. The server has ways of verifying this token. Is this safe? The API is more secure than form-based payments. The new payment instruments are based on modern cryptography. You don't need to enter payment data that can be logged or observed at the point of entry. So to review, payment request eliminates manual and tedious entry by storing the user's information securely in the browser. It works across browsers, across OS platforms, and can work with any payment service that wants to write just a little code. Now that you've seen how payment request works for the user, let me show you how to code it. Remember that the payment UI contains multiple sections. We need to populate these sections before making the request. The request needs at least three data structures, the payment options, the order details, and shipping options. So we'll create those, then generate a payment request. We will call show and get back a promise that resolves when it's time to contact the payment processor. Finally, notice that we may want two or three event handlers. These are called while the UI is visible and can trigger updates when the user changes addresses or shipping options. Let's walk through the steps in the UI and the corresponding code. First, the user taps Buy Now, so we want to set up a payment request. Remember that we need some data structures before we can create the request. The payment methods lists all the payment services that your site understands. It's an array of payment method objects, and each method may have its own specific properties. Here, we're listing the basic credit card method and Google Pay. Note that I'm leaving out the details of the Google Pay object as it's pretty involved. We want the user to know what they're buying. This includes the core details of the transaction, how much money, what currency, and what list items you want displayed. Keep the line items fairly high level. It isn't meant to be used as an itemized receipt. If you want more than one shipping option, you can define this structure. Notice it's a property on the transaction details. If you have multiple options, include a selected bit. The browser will highlight the selected option and fire an event when it changes. Now you can define what information you want from the API. By default, payment requests will only return the payment info. If the user is logged in, you may have the rest of their information. If you need more, these options let you collect what you need. Take the data structures you just created and pass them to the payment request constructor. This should happen in response to a button tap or other user initiated action. Now so far, the screen hasn't changed. We need to trigger the UI. Take the payment request and call the show method. This returns a promise and opens the user interface. Notice there's a big block of code after this. We still have a couple of steps until we're done. The UI shows the details of the purchase and collects payment information. When the user approves, the promise resolves with a payment response object. The payment response object contains everything needed to submit a payment. For credit card payments, the response is standardized. For non-credit card payments, such as Google Pay, the response will be documented by the provider. Next, you should check that the payment is valid. Send the information to your payment processor for processing. The UI will show a spinner while the request takes place. OK, so how do we remove the payment UI? After we click Payment, even if it fails, call paymentResponse.complete. You can include a message to the user. This tells the browser that the payment is complete and allows the app to notify the user of the result. Now there's one loose end to tie up. What happens if the user changes their shipping address or options? This may change a total cost, and in some cases, may change the shipping options. Payment request can emit shipping address change, shipping option change, and payment method change events. You should have event listeners for these and update the payment details accordingly. Remember we're making the update in the middle of a promise just as we do installing the service worker, so we use a similar bit of code. We get the event and call Update With on it. This takes a promise that resolves to a new transaction details record. It's not all that different from calling cash.addall from the service worker's install event. That also returns its result via the event. The get new info method is something that you would write. It takes some data from the request and builds a new transaction details object. Here's a reminder of what goes in the transaction details. Your code probably needs to copy from the old object to a new one. We resolve the promise with the new details. Update With then updates the payment request and the screen. And that's it. You now have a seamless, easy to use checkout experience. Now that was a bit of a long example, but most of it is boilerplate. Check out the payment request samples at googlechrome.github.io. Remember the process for easier payments starts with autofill. If you want more, payment request is there to help. You can also check out specific libraries for Apple Pay, Google Pay, Samsung Pay, and others. They put up a Pay With button on the screen. They usually fall back to the payment request interface, but they may be an easier option for your users. The only way to know is to try them for yourself. Thanks for hanging in there through web payments. This is one of the most complex parts of building a PWA, yet it's really satisfying and could be great for business. Take care, and I'll see you again soon.