- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在尝试将客户端 PayPal JavaScript SDK 与 PayPal 服务器端 REST API 集成。
我知道如何在客户端 JavaScript SDK 和服务器端 REST API 中创建新的 PayPal 订单。我都成功地做到了。但问题是,无论我采用哪种方式,都会出现问题。如果我在服务器上创建了一个新订单,而不是在客户端代码中,那么客户登录后显示的价格是 0.01 美分,而不是正确的价格,即使订单创建正确。
我可以同时使用客户端 createOrder
方法和服务器端订单创建,但我担心它会创建两个订单,甚至可能捕获两个付款。
我找不到集成服务器端订单创建并在用户登录后出现的 PayPal 对话框中设置价格的方法。
用户不使用客户端登录后如何显示产品价格createOrder
方法?
如果我在服务器上创建订单并将订单 ID 传递回客户端代码,则客户端 JavaScript API 会出现 3 个关于 PATCH 的错误。我不明白。
我可以省略客户端createOrder
方法,一切正常,除了付款金额默认为 0.01 美分。我不想要那个。
客户端代码:
function thePromiseCode() {
return new Promise (function (resolve,reject) {
google.script.run
.withSuccessHandler (function (result) {
resolve (result);//What resolve does is return the result from the server back to the FIRST anonymous function in the "then" part
})
.withFailureHandler (function (error) {
reject (error);
})
.createA_PayPalOrder();//Call the server to create a PayPal order
})
}
function initPayPalButton() {
thePromiseCode()// Call the server code to create an order
.then(
function(response) {
console.log('response 89: ' + response)
var orderObj = JSON.parse(response);
window.payPalOrderId = orderObj.id;
console.log('payPalOrderId: ' + payPalOrderId)
},
function (error) {
showModalMessageBox("There was an error in the PayPal button!","ERROR!");
console.log(error);
return "Error";
}
);
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder: function(data, actions) {
// This fnc sets up the details of the transaction, including the amount
return actions.order.create({
purchase_units:[
{"description":"My Product Name",
"amount":{
"currency_code":"USD",
"value":"2.95",
}
}
]
});
},
onApprove: function(data, actions) {
return actions.order.capture().then(function(details) {
payPalPaymentComplete();
console.log('Transaction completed by ' + details.payer.name.given_name + '!');
});
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
showModalMessageBox('PayPal Session was cancelled',"CANCELLED!");
backToStartPay();
},
onError: function(err) {
showModalMessageBox("There was an error in the PayPal button!","ERROR!");
console.log(err);
}
}).render('#paypal-button-container');
}
initPayPalButton();
</script>
最佳答案
这是我使用客户端 PayPal Javascript SDK 和服务器端 PayPal API 接受买家付款的解决方案。
此服务器代码特定于 Apps Script,来自客户端的服务器调用也特定于 Apps Script。
注意:要“直播”,您必须:
function payPalSettings_() {
//see:
//https://developer.paypal.com/docs/business/javascript-sdk/javascript-sdk-reference/#createorder
return {
intent:'CAPTURE',//The PayPal JavaScript SDK defaults to capture if not set
//but for purposes of explicitly making it clear what the code is doing -
//it is being set here -
//The Capture setting results in the capturing of funds immediately
//after the buyer approves the purchase -
purchase_units:[
{"custom_id":"abc123",//You can add a custom value to pass to PayPal
"description":"Emails Director Gold",
"amount":{
"currency_code":"USD",
"value":"2.95",
}
}
],
}
}
function getCredentials_() {
var CLIENT_Live,Paypal_Client_Test,PayPalSecret_Test,rtrnObj,SECRET_Live;
rtrnObj = {};
Paypal_Client_Test = 'abc123';//You must get this from your PayPal account
PayPalSecret_Test = '321cba';
//For testing comment out the live credentials
//CLIENT_Live = 'put it here';//You must get this from your PayPal account
//SECRET_Live = 'put it here';
rtrnObj.client = CLIENT_Live ? CLIENT_Live : Paypal_Client_Test;//If the live credential is available then use it
rtrnObj.secret = SECRET_Live ? SECRET_Live : PayPalSecret_Test;
return rtrnObj;
}
/* This code is for making calls to PayPal from the SERVER
This server code must work together with the client side code
*/
//To see the equivalent of this code go to:
//https://developer.paypal.com/docs/business/checkout/server-side-api-calls/create-order#1-add-server-code
//which is the Add Server Code section and show the JavaScript code
function captureThePayment(po) {
try{
var accessTkn,capture,options,PayPal_Capture_API,rCode;
/*
po.orderId - the order ID
*/
/*
See documentation at:
https://developer.paypal.com/docs/checkout/reference/server-integration/capture-transaction/
*/
//Logger.log('po 61',po)
accessTkn = getAuthTkn_();
PayPal_Capture_API = 'https://api.sandbox.paypal.com/v2/checkout/orders/' + po.orderId + '/capture';
//Logger.log('PayPal_Capture_API',PayPal_Capture_API)
options = {};
options.muteHttpExceptions = true;
options.method = 'post';
options.headers = {
//"Accept":"application/json",
"Authorization": "Bearer " + accessTkn
}
options.contentType = 'application/json';//This needs to be used or it doesnt work
capture = UrlFetchApp.fetch(PayPal_Capture_API,options);//Call PayPal to capture the order
//Logger.log('capture.getResponseCode() 77',capture.getResponseCode())
//Logger.log('capture 85',capture)
//Logger.log('capture.error',capture.error)
rCode = capture.getResponseCode();
if (rCode !== 200 && rCode !== 201) {
//Logger.log('capture.getContentText',capture.getContentText)
throw new Error("Error capturing the payment: " + rCode);
}
//Logger.log('capture.getContentText',capture.getContentText)
/*
There is no response - just a response code
To get order detail another request must be made to PayPal
*/
// Do your own custom processing
return true;//The capture option doesnt return anything but a response code - it doesnt return order details -
//to get order details you would need to use the base API url with just the order number on the end-
//See https://developer.paypal.com/docs/checkout/reference/server-integration/get-transaction/
}catch(e){
//Logger.log('message',e.message)
//Logger.log('stack',e.stack)
}
}
function createA_PayPalOrder() {
try{
var authTkn,options,order,paySets,payload,response;
authTkn = getAuthTkn_();
//2 Set up your server to receive a call from the client
PAYPAL_ORDER_API = 'https://api.sandbox.paypal.com/v2/checkout/orders/';//Must be changed for live mode
// 3 Call PayPal to set up a transaction
options = {};
paySets = payPalSettings_();
//console.log('paySets 121' + paySets)
options.muteHttpExceptions = true;//
options.method = 'post';
options.headers = {
"Accept":"application/json",
"Authorization": "Bearer " + authTkn
}
options.contentType = 'application/json';
options.payload = JSON.stringify(paySets);
response = UrlFetchApp.fetch(PAYPAL_ORDER_API,options);//Call PayPal to set up a transaction
//console.log('response.getResponseCode() 131' + response.getResponseCode())
/*
The response returned should look like:
{"id":"abc123",
"status":"CREATED",
"links":[{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/abc123",
"rel":"self",
"method":"GET"},
{"href":"https://www.sandbox.paypal.com/checkoutnow?token=abc123",
"rel":"approve",
"method":"GET"},
{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/abc123",
"rel":"update","method":"PATCH"},
{"href":"https://api.sandbox.paypal.com/v2/checkout/orders/abc123/capture",
"rel":"capture",
"method":"POST"}]}
*/
//4 Handle any errors from the call
if (response.getResponseCode() !== 201) {
//console.log('response.getContentText() 135' + response.getContentText())
//console.log(response);
return "error";
}
order = response.getContentText();
//order = JSON.parse(order);
//console.log('order.id 166' + order.id)
//5 Return a successful response to the client
return order;
}catch(e){
//console.log('message' + e.message)
//console.log('stack' + e.stack)
}
}
function getAuthTkn_() {
try{
var auth,basicAuth,cacheService,creds,credentialClient,credentialSecret,keyConcat,options,payPalToken,PAYPAL_OAUTH_API,paySets,
payload,response,tkn;
/*Log into your developer dashboard: https://www.paypal.com/signin?returnUri=https%3A%2F%2Fdeveloper.paypal.com%2Fdeveloper%2Fapplications
Note that the sandbox (for testing) and the live modes both have their own apps - So I would create different app names for
each one -
If you have not created an app then create an app
If you need to create an app then you will need to generate the credentials
If you already have an app and credentials then you can use those
When getting your credentials the first thing you must do is choose either sandbox or live -
*/
cacheService = CacheService.getDocumentCache();
try{
payPalToken = cacheService.get("authTkn");
}catch(e){
}
if (payPalToken) {//There is already a PayPal auth token generated and in Cache
return payPalToken;
}
creds = getCredentials_();
credentialClient = creds.client;
credentialSecret = creds.secret;
PAYPAL_OAUTH_API = 'https://api.sandbox.paypal.com/v1/oauth2/token/';//1B Use the PayPal APIs which are called with the following URLs -
keyConcat = credentialClient + ':' + credentialSecret;//concatenate the client and secret credentials
basicAuth = Utilities.base64Encode(keyConcat);//Base 64 encode
options = {};
payload = {};
payload.grant_type = "client_credentials";
options.muteHttpExceptions = true;//
options.method = 'post';
options.headers = {
"Accept":"application/json",
"Authorization": "Basic " + basicAuth
}
options.payload = payload;
response = UrlFetchApp.fetch(PAYPAL_OAUTH_API, options);//1C Get an access token from the PayPal API
//Logger.log('response.getResponseCode() 80',response.getResponseCode())
auth = response.getContentText();
//Logger.log('auth 83',auth)
/*
The response returned should look like this:
{"scope":"https://uri.paypal.com/services/invoicing
https://uri.paypal.com/services/disputes/read-buyer
https://uri.paypal.com/services/payments/realtimepayment
https://uri.paypal.com/services/disputes/update-seller
https://uri.paypal.com/services/payments/payment/authcapture openid
https://uri.paypal.com/services/disputes/read-seller
https://uri.paypal.com/services/payments/refund
https://api.paypal.com/v1/vault/credit-card https://api.paypal.com/v1/payments/.*
https://uri.paypal.com/payments/payouts https://api.paypal.com/v1/vault/credit-card/.*
https://uri.paypal.com/services/subscriptions
https://uri.paypal.com/services/applications/webhooks",
"access_token":"abc123",
"token_type":"Bearer",
"app_id":"APP-abc123",
"expires_in":32400,
"nonce":"2020-12-09T01:07:abc123"}
*/
if (!auth) {
throw new Error("Authorization information not retrieved from PayPal");
}
auth = JSON.parse(auth);
//Logger.log('auth',auth)
//Logger.log('auth.access_token 90',auth.access_token)
tkn = auth.access_token;
cacheService.put("authTkn", tkn, 180);//Save in cache for 3 minutes
return tkn;
}catch(e){
Logger.log('message',e.message)
Logger.log('stack',e.stack)
console.log(e)
}
}
客户端 HTML
<!-- This code uses the client side JavaScript PayPal SDK -->
<div style="text-align: center;">
<div id="paypal-button-container"></div>
</div>
客户端 JavaScript
<!--
Required files:
This code works together with the server code -
This PayPal payment system uses both the PayPal javascript SDK AND server side PayPal API calls -
This PayPal payment system uses Smart Payment Buttons - See: https://developer.paypal.com/docs/checkout/
The two most important documentation links are:
https://developer.paypal.com/docs/checkout/integrate/
https://developer.paypal.com/docs/checkout/reference/server-integration/set-up-transaction/
For configuration settings there is information at:
https://developer.paypal.com/docs/business/javascript-sdk/javascript-sdk-reference/
Note that this PayPal implementation does NOT require there to be a button definition in your PayPal settings -
The PayPal JavaScript client side SDK is newer than the checkout.js
See this Link: https://developer.paypal.com/docs/archive/checkout/how-to/server-integration/
For an overview of the PayPal checkout with server integration -
It is very important to understand the "sandbox" and the "production" settings -
There are mutliple settings that must all be for either "sandbox" or "production"
If you mix "sandbox" and "production" credentials and API links then your code will not work
and the error messages may not help you to understand what the real problem is -
Anything to do with "sandbox" is for testing purposes -
"production" is for accepting live payments from customers -
The terminology that PayPal uses for the credentials is:
client id - The client side credential key
secret - The server side credential key
Credentials need to be in three different settings-
1 - Client side script tag - client id
2 - Server side variable - client id
3 - Server side variable - secret
To test your PayPal code you must do multiple things:
1 - Create sandbox (test) client and secret credentials
2 - use a special buyer PayPal account:
https://developer.paypal.com/docs/checkout/integrate/
-->
<script src="https://www.paypal.com/sdk/js?client-id= YOUR CLIENT ID HERE - MAKE SURE IT MATCHES EITHER SANDBOX OR PRODUCTION ¤cy=USD"></script>
<script>
window.payPalPaymentComplete = function(orderId) {
showModalMessageBox("Payment is complete","COMPLETE!");
}
window.backToStartPay = function() {
//Return to the dialog to buy something
}
function capture_the_order(po) {
//Use the google.script.run API to call the server
//This is specific to Google Apps Script
return new Promise (function (resolve,reject) {
google.script.run
.withSuccessHandler (function (result) {
//cl('result 62' + result)
resolve (result);//What resolve does is return the result from the server back to the FIRST anonymous function in the "then" part
})
.withFailureHandler (function (error) {
//cl('error 66' + error)
reject (error);
})
.captureThePayment(po);//Call the server to create a PayPal order
})
}
function iWillWaitForU() {
//Use the google.script.run API to call the server
//This is specific to Google Apps Script
return new Promise (function (resolve,reject) {
google.script.run
.withSuccessHandler (function (result) {
//cl('result 62' + result)
resolve (result);//What resolve does is return the result from the server back to the FIRST anonymous function in the "then" part
})
.withFailureHandler (function (error) {
//cl('error 66' + error)
reject (error);
})
.createA_PayPalOrder();//Call the server to create a PayPal order
})
}
function initPayPalButton() {
/*
This is a server side integration of the client side PayPal JavaScript SDK -
The createOrder method makes a call to the PayPal API -
The PayPal documentation uses the fetch() method but Apps Script uses google.script.run
to make a server call so the PayPal example must be modified -
*/
paypal.Buttons({
style: {
shape: 'rect',
color: 'gold',
layout: 'vertical',
label: 'paypal',
},
createOrder : function() {
//console.log('createOrder 93' + 'order')
return iWillWaitForU()// Call the server code to complete the payment transaction
//This both creates and executes the transaction
.then(function(response) {
//console.log('response 89' + response)
return JSON.parse(response);
//window.PayPalOrderId = orderInfo.id;
//return orderInfo.id;
},//THERE MUST BE A COMMA HERE!!!! This is a list of functions seperated by a comma
function (error) {//Because this is the second function this is what gets called for an error
showModalMessageBox("There was an error in the PayPal button!","ERROR!");
//console.log(error);
return "Error";
}).then(
function(orderObj){
//console.log('orderObj.orderID' + orderObj.id)
return orderObj.id;
});
},
onApprove: function() {
//console.log('onapprove ran')
startSpinner();
backToStartPay();
capture_the_order({"which":"capture","orderId":window.PayPalOrderId})
.then(
function(hadSuccess) {
//cl('hadSuccess 89',hadSuccess)
if (hadSuccess) {
payPalPaymentComplete();
//console.log('Transaction completed !');
} else {
showModalMessageBox("There was an error getting the payment!","ERROR!");
//console.log(error);
stopSpinner();
}
},//THERE MUST BE A COMMA HERE!!!! This is a list of functions seperated by a comma
function (error) {//Because this is the second function this is what gets called for an error
showModalMessageBox("There was an error getting the payment!","ERROR!");
//console.log(error);
stopSpinner();
})
},
onCancel: function (data, actions) {
// Show a cancel page or return to cart
showModalMessageBox('PayPal Session was cancelled',"CANCELLED!");
backToStartPay();
},
onError: function(err) {
showModalMessageBox("There was an error in the PayPal button!","ERROR!");
//console.log(err);
}
}).render('#paypal-button-container');//Render the PayPal button into the html element with id #paypal-button-container -
//See HTML file - H_PayPal_New
}
initPayPalButton();
</script>
关于javascript - 带有服务器集成的 PayPal 客户端 JavaScript SDK - 设置付款金额,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65244678/
我想在一些计算机之间建立点对点连接,这样用户就可以在没有外部服务器的情况下聊天和交换文件。我的第一个想法如下: 我在服务器上创建了一个中央 ServerSocket,所有应用程序都可以连接到该服务器。
我正在 Unity 中构建多人游戏。为此,我必须将一些值从客户端发送到两个或多个通过服务器连接的客户端。我想将其构建为服务器真实游戏。客户端将使用 Android,他们的数据将通过服务器同步(可能是一
练习 C 网络编程:我正在编写一个简单的 TCP 客户端-服务器应用程序,它应该将消息(在每个客户端的单独线程中)作为字符串从服务器发送到客户端并在客户端(稍后将成为控制台商店应用程序)。我首先发送消
我使用证书身份验证设置了 AWS Client VPN。我正在为客户端-客户端访问系统进行设置,基本上如 this AWS scenario/example 中所述.一切正常,如果我知道他们的 IP
我正在开发一个小型客户端1/客户端2、服务器(线程)TCP 游戏。在尝试处理延迟问题时,我意识到我的 transmitState() 中存在缺陷。它强制将不必要的信息传递到通讯流中,从而造成迟缓,将汽
来自文档:Configurable token lifetimes in Azure Active Directory (Public Preview) 它提到“ secret 客户端”,刷新 tok
Apollo 客户端开发工具无法连接到我的应用程序。我已在 ApolloClient 构造函数中将 connectToDevTools 传递为 true,但没有任何 react 。我也试过this p
我想在 Pod 内使用 Fabric8 kubernetes 客户端 (java)。如何获取部署集群的 kubernetes 客户端? 我可以使用该集群的 kubeconfig 文件获取任何集群的配置
我正在阅读 the security issue with Log4j我了解此产品受此漏洞影响。但是 Oracle 客户端 11.2 和 12 是否受此问题影响? 我找不到这些产品是否使用任何 Log
Eureka 服务器设置 pom.xml 1.8 Hoxton.SR1 org.springframework.cloud spring
我有一个点对点(客户端/服务器)设置(通过本地 LAN),它使用 Netty,一个 Java 网络框架。我使用原始 TCP/IP(例如,没有 HTTP)进行通信和传输。现在,根据要求,我们希望转向 T
上一篇已经实现了ModbusTcp服务器和8个主要的功能码,只是还没有实现错误处理功能。 但是在测试客户端时却发现了上一篇的一个错误,那就是写数据成功,服务器不需要响应。 接下来要做的就是实现Modb
有没有办法将二维十六进制代码数组转换为 png 图像? 数组看起来像这样(只是更大) [ [ '#FF0000', '#00FF00' ], [ '#0000FF'
我是套接字编程的新手。每次我运行客户端程序时,它都会说“无法连接到服务器”。谁能告诉我我在哪里犯了错误。任何帮助将不胜感激。 这是client.c #include #include #inclu
我们在UNIX环境下制作了简单的client.c和server.c程序。我们使用它来传输一个简单的文本文件,首先打开它,然后读取它并使用 open、read 和 send 系统调用发送;在客户端,我接
当我的程序来自 my previous question正在响应客户端,它应该发送加密消息。 当客户端连接时,它会发送一条类似“YourMessage”的消息。现在我想做的是,当客户端连接时,应该以某
我正在使用 C 和 putty 编写客户端/服务器程序。两个 c 文件位于同一系统上。 我目前在向客户端写回其正在使用的框架以及打印我的框架时遇到问题。它打印出 3 0 9 8,但随后开始打印 134
我正在使用 C 中的 select() 制作一个模拟快餐或其他任何东西的客户端服务器。 我有客户随机点 1-5 种“食物”。服务器每 30 秒决定一次。所有客户最喜欢的食物是什么?他为那些客户提供服务
对于单机游戏,基本的游戏循环是(来源:维基百科) while( user doesn't exit ) check for user input run AI move enemies
1、CentOS安装TortoiseSVN 复制代码 代码如下: yum install -y subversion 2、SVN客户端命令
我是一名优秀的程序员,十分优秀!