【问题标题】:Setting up return URL for react paypal express checkout为 react paypal express checkout 设置返回 URL
【发布时间】:2018-10-28 05:11:25
【问题描述】:

我正在使用react-paypal-express-checkout 并尝试在仪表板中设置返回 URL,但这不起作用。我可以收到钱,但它不会让我回到正确的页面。

您能否告知我如何将其添加到 react-paypal-express-checkout 代码中?

import React, { Component } from 'react';
import PaypalExpressBtn from 'react-paypal-express-checkout'

export default class PaypalButton extends React.Component {

  render() {
  
const onSuccess = (payment) => { }
  
const onCancel = (data) => { }
  
const onError = (err) => { }
  
  let env = 'production';
  let currency = 'GBP';
  let total = 20
  
  const client = {
  	sandbox: 'YOUR-SANDBOX-APP-ID',
  production: 'YOUR-PROD-APP-ID',
}

return (<PaypalExpressBtn env={env} client={client} currency={currency} total={total} onError={onError} onSuccess={onSuccess} onCancel={onCancel} />)

  }

}

【问题讨论】:

  • 没有代码,没有答案。大声笑
  • 附上代码
  • 如果你收到钱,我假设它会触发onsuccess func 并按照上面的代码触发控制台日志。您需要在该功能内路由到您想要的页面。 browserHistory.push("/&lt;Route&gt;")
  • 非常感谢卡伦,非常感谢您帮助我。我明天试试这个,然后再找你。如果其他人有任何其他想法,这也将不胜感激。浏览器历史的来源只有一件事是通过 react-router-dom 导入的?
  • 从“react-router”导入 { browserHistory }

标签: reactjs paypal checkout


【解决方案1】:

如果将来有人需要它,请使用此代码。

import React, { Component } from 'react';
import { withRouter } from 'react-router-dom';
import PaypalExpressBtn from 'react-paypal-express-checkout';

class PaypalButton extends React.Component {
    render() {		
		const onSuccess = (payment) => {
			// Congratulation, it came here means everything's fine!
            		console.log("The payment was succeeded!", payment);
					// You can bind the "payment" object's value to your state or props or whatever here, please see below for sample returned data
					this.props.history.push('/routehere')
					
		}		
		
		const onCancel = (data) => {
			// User pressed "cancel" or close Paypal's popup!
			console.log('The payment was cancelled!', data);
			// You can bind the "data" object's value to your state or props or whatever here, please see below for sample returned data
		}	
		
		const onError = (err) => {
			// The main Paypal's script cannot be loaded or somethings block the loading of that script!
			console.log("Error!", err);
			// Because the Paypal's main script is loaded asynchronously from "https://www.paypalobjects.com/api/checkout.js"
			// => sometimes it may take about 0.5 second for everything to get set, or for the button to appear			
		}			
			
		let env = 'production'; // you can set here to 'production' for production
		let currency = 'GBP'; // or you can set this value from your props or state  
		let total = 20; // same as above, this is the total amount (based on currency) to be paid by using Paypal express checkout
		// Document on Paypal's currency code: https://developer.paypal.com/docs/classic/api/currency_codes/
		
		const client = {
			sandbox:    'YOUR-SANDBOX-APP-ID',
			production: 'AScH9LQjnZZzeEr-Y_YzH0LS6tcmlTuJu4rYnAD2XEipTP9yekPv4bgADHpRVRQCldAObaZsAHjhv1p5',
		}
		// In order to get production's app-ID, you will have to send your app to Paypal for approval first
		// For sandbox app-ID (after logging into your developer account, please locate the "REST API apps" section, click "Create App"): 
		//   => https://developer.paypal.com/docs/classic/lifecycle/sb_credentials/
		// For production app-ID:
		//   => https://developer.paypal.com/docs/classic/lifecycle/goingLive/		
		
		// NB. You can also have many Paypal express checkout buttons on page, just pass in the correct amount and they will work!		  
        return (
            <PaypalExpressBtn env={env} client={client} currency={currency} total={total} onError={onError} onSuccess={onSuccess} onCancel={onCancel} />
        );
    }
}


export default withRouter (PaypalButton);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-04-14
    • 2021-01-20
    • 2012-10-22
    • 2010-12-03
    • 2020-02-21
    • 2017-01-08
    • 2012-09-25
    • 2014-01-11
    相关资源
    最近更新 更多