|
| 1 | +import React from "react"; |
| 2 | +import PaypalExpressBtn from "react-paypal-express-checkout"; |
| 3 | + |
| 4 | +export default class MyApp extends React.Component { |
| 5 | + render() { |
| 6 | + const onSuccess = (payment) => { |
| 7 | + // Congratulation, it came here means everything's fine! |
| 8 | + console.log("The payment was succeeded!", payment); |
| 9 | + // You can bind the "payment" object's value to your state or props or whatever here, please see below for sample returned data |
| 10 | + this.props.onSuccess(payment); |
| 11 | + }; |
| 12 | + |
| 13 | + const onCancel = (data) => { |
| 14 | + // User pressed "cancel" or close Paypal's popup! |
| 15 | + console.log("The payment was cancelled!", data); |
| 16 | + // You can bind the "data" object's value to your state or props or whatever here, please see below for sample returned data |
| 17 | + }; |
| 18 | + |
| 19 | + const onError = (err) => { |
| 20 | + // The main Paypal's script cannot be loaded or somethings block the loading of that script! |
| 21 | + console.log("Error!", err); |
| 22 | + // Because the Paypal's main script is loaded asynchronously from "https://www.paypalobjects.com/api/checkout.js" |
| 23 | + // => sometimes it may take about 0.5 second for everything to get set, or for the button to appear |
| 24 | + }; |
| 25 | + |
| 26 | + let env = "sandbox"; // you can set here to 'production' for production |
| 27 | + let currency = "USD"; // or you can set this value from your props or state |
| 28 | + let total = this.props.toPay; // same as above, this is the total amount (based on currency) to be paid by using Paypal express checkout |
| 29 | + // Document on Paypal's currency code: https://developer.paypal.com/docs/classic/api/currency_codes/ |
| 30 | + |
| 31 | + const client = { |
| 32 | + sandbox: |
| 33 | + "ATdlRK1n0Wiy43MJXKOASZPazDEKXE-VI02OxJxtUBmz9MKUqbIsrhYSVu_E_g5QBesorpy24bcnGKyo", |
| 34 | + production: "YOUR-PRODUCTION-APP-ID", |
| 35 | + }; |
| 36 | + // In order to get production's app-ID, you will have to send your app to Paypal for approval first |
| 37 | + // For sandbox app-ID (after logging into your developer account, please locate the "REST API apps" section, click "Create App"): |
| 38 | + // => https://developer.paypal.com/docs/classic/lifecycle/sb_credentials/ |
| 39 | + // For production app-ID: |
| 40 | + // => https://developer.paypal.com/docs/classic/lifecycle/goingLive/ |
| 41 | + |
| 42 | + // NB. You can also have many Paypal express checkout buttons on page, just pass in the correct amount and they will work! |
| 43 | + return ( |
| 44 | + <PaypalExpressBtn |
| 45 | + env={env} |
| 46 | + client={client} |
| 47 | + currency={currency} |
| 48 | + total={total} |
| 49 | + onError={onError} |
| 50 | + onSuccess={onSuccess} |
| 51 | + onCancel={onCancel} |
| 52 | + style={{ |
| 53 | + size: "large", |
| 54 | + color: "blue", |
| 55 | + shape: "rect", |
| 56 | + label: "checkout", |
| 57 | + }} |
| 58 | + /> |
| 59 | + ); |
| 60 | + } |
| 61 | +} |
0 commit comments