PayPal支付弹窗未显示

huangapple go评论70阅读模式
英文:

PayPal Checkout popup not showing up

问题

在尝试将PayPal集成到我的网站时,我在控制台日志中收到了一个错误。当我按下结账按钮时,应该出现一个小的PayPal弹出窗口,以便我进行结账。但实际上,弹出窗口出现了0.1秒,然后又关闭了。

Payments.html

<script>
	// 将PayPal按钮呈现到 #paypal-button-container 中
	paypal.Buttons({

		// 调用您的服务器以设置交易
		createOrder: function (data, actions) {
			return fetch('/demo/checkout/api/paypal/order/create/', {
				method: 'post'
			}).then(function (res) {
				return res.json();
			}).then(function (orderData) {
				return orderData.id;
			});
		},

		// 调用您的服务器以完成交易
		onApprove: function (data, actions) {
			return fetch('/demo/checkout/api/paypal/order/' + data.orderID + '/capture/', {
				method: 'post'
			}).then(function (res) {
				return res.json();
			}).then(function (orderData) {
				// 处理三种情况:
				//   (1) 可恢复的INSTRUMENT_DECLINED -> 调用 actions.restart()
				//   (2) 其他不可恢复的错误 -> 显示失败消息
				//   (3) 成功的交易 -> 显示确认或感谢

				// 此示例读取从服务器传播的v2/checkout/orders捕获响应
				// 您可以为您的'orderData'使用不同的API或结构
				var errorDetail = Array.isArray(orderData.details) && orderData.details[0];

				if (errorDetail && errorDetail.issue === 'INSTRUMENT_DECLINED') {
					return actions.restart(); // 可恢复状态,详见:
					// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
				}

				if (errorDetail) {
					var msg = '抱歉,无法处理您的交易。';
					if (errorDetail.description) msg += '\n\n' + errorDetail.description;
					if (orderData.debug_id) msg += '(' + orderData.debug_id + ')';
					return alert(
					msg); // 显示失败消息(在生产环境中尽量避免使用警报)
				}

				// 成功的捕获!仅供演示用途:
				console.log('捕获结果', orderData, JSON.stringify(orderData, null, 2));
				var transaction = orderData.purchase_units[0].payments.captures[0];
				alert('交易 ' + transaction.status + ': ' + transaction.id +
					'\n\n请查看控制台以获取所有可用的详细信息');

				// 用成功消息替换上述代码,例如在此页面内显示成功消息:
				// const element = document.getElementById('paypal-button-container');
				// element.innerHTML = '';
				// element.innerHTML = '<h3>感谢您的付款!</h3>';
				// 或转到另一个URL:actions.redirect('thank_you.html');
			});
		}

	}).render('#paypal-button-container');
</script>

base.html

<!-- PayPal脚本 -->
<script src="https://www.paypal.com/sdk/js?client-id=test&currency=USD"></script>

我期望PayPal按钮会打开一个小页面以确认我的结账。

英文:

When trying to integrate paypal to my website i get an error in the console log. PayPal支付弹窗未显示

Ive never seen this error before. So when i press the checkout button it a small paypal popup should show up so that i can checkout. Instead the popup shows up for 0.1s then closes again.

Payments.html


&lt;script&gt;
// Render the PayPal button into #paypal-button-container
paypal.Buttons({
// Call your server to set up the transaction
createOrder: function (data, actions) {
return fetch(&#39;/demo/checkout/api/paypal/order/create/&#39;, {
method: &#39;post&#39;
}).then(function (res) {
return res.json();
}).then(function (orderData) {
return orderData.id;
});
},
// Call your server to finalize the transaction
onApprove: function (data, actions) {
return fetch(&#39;/demo/checkout/api/paypal/order/&#39; + data.orderID + &#39;/capture/&#39;, {
method: &#39;post&#39;
}).then(function (res) {
return res.json();
}).then(function (orderData) {
// Three cases to handle:
//   (1) Recoverable INSTRUMENT_DECLINED -&gt; call actions.restart()
//   (2) Other non-recoverable errors -&gt; Show a failure message
//   (3) Successful transaction -&gt; Show confirmation or thank you
// This example reads a v2/checkout/orders capture response, propagated from the server
// You could use a different API or structure for your &#39;orderData&#39;
var errorDetail = Array.isArray(orderData.details) &amp;&amp; orderData.details[0];
if (errorDetail &amp;&amp; errorDetail.issue === &#39;INSTRUMENT_DECLINED&#39;) {
return actions.restart(); // Recoverable state, per:
// https://developer.paypal.com/docs/checkout/integration-features/funding-failure/
}
if (errorDetail) {
var msg = &#39;Sorry, your transaction could not be processed.&#39;;
if (errorDetail.description) msg += &#39;\n\n&#39; + errorDetail.description;
if (orderData.debug_id) msg += &#39; (&#39; + orderData.debug_id + &#39;)&#39;;
return alert(
msg); // Show a failure message (try to avoid alerts in production environments)
}
// Successful capture! For demo purposes:
console.log(&#39;Capture result&#39;, orderData, JSON.stringify(orderData, null, 2));
var transaction = orderData.purchase_units[0].payments.captures[0];
alert(&#39;Transaction &#39; + transaction.status + &#39;: &#39; + transaction.id +
&#39;\n\nSee console for all available details&#39;);
// Replace the above to show a success message within this page, e.g.
// const element = document.getElementById(&#39;paypal-button-container&#39;);
// element.innerHTML = &#39;&#39;;
// element.innerHTML = &#39;&lt;h3&gt;Thank you for your payment!&lt;/h3&gt;&#39;;
// Or go to another URL:  actions.redirect(&#39;thank_you.html&#39;);
});
}
}).render(&#39;#paypal-button-container&#39;);
&lt;/script&gt;

base.html

   &lt;!-- Paypal Script --&gt;
&lt;script src=&quot;https://www.paypal.com/sdk/js?client-id=test&amp;currency=USD&quot;&gt;&lt;/script&gt;

I was expecting the paypalbutton to open up a little page to confirm my chechout.

答案1

得分: 1

/demo/checkout/api/paypal/order/create/ 或者你用其他路径替换它,必须是你服务器上的有效路由,调用PayPal v2/checkout/orders API 并且仅返回/输出该API的JSON响应。如果返回任何不能解析为有效JSON的内容,比如HTML 404页面,那么结果就是你问题中的错误。

你还必须实现一个捕获路由,可以从onApprove函数中调用。

有关Node.js的完整示例,请参阅文档(当然,你也可以使用其他后端环境来实现所需的路由)。

英文:

/demo/checkout/api/paypal/order/create/ , or whatever you replace this path with, needs to be a valid route on your server that calls the PayPal v2/checkout/orders API and returns/outputs only the JSON response of that. If anything else is returned that does not parse as valid JSON, such as an HTML 404 page, the result is the error in you question.

You must also implement a capture route to be called from the onApprove function.

For a full sample in node.js , see the documentation (you can of course use any other backend environment to implement the required routes)

huangapple
  • 本文由 发表于 2023年2月23日 22:17:08
  • 转载请务必保留本文链接:https://go.coder-hub.com/75546029.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定