问题描述
我想在我的网站上添加google pay Buy Now按钮,我使用了以下文档作为参考。 https://codelabs.developers.google.com/codelabs/pay-web-checkout/index.html?index=..%2F..index#0 一切正常,这是我创建的代码。
<div id="buy-Now"></div>
<script async
src="https://pay.google.com/gp/p/js/pay.js"
onload="onGooglePayLoaded()">
</script>
<script>
let googlePayClient;
const baseCardPaymentMethod = {
type: 'CARD',parameters: {
allowedCardNetworks: ['VISA','MASTERCARD'],allowedAuthMethods: ['PAN_ONLY','CRYPTOGRAM_3DS']
}
};
const googlePayBaseConfiguration = {
apiVersion: 2,apiVersionMinor: 0,allowedPaymentMethods: [baseCardPaymentMethod]
};
function onGooglePayLoaded() {
googlePayClient = new google.payments.api.PaymentsClient({
environment: 'TEST'
});
// check compatability
googlePayClient.isReadyToPay(googlePayBaseConfiguration)
.then(function(response) {
if(response.result) {
createAndAddButton();
} else {
alert("Unable to pay using Google Pay");
}
}).catch(function(err) {
console.error("Error determining readiness to use Google Pay: ",err);
});
}
function createAndAddButton() {
const googlePayButton = googlePayClient.createButton({
// currently defaults to black if default or omitted
buttonColor: 'default',// defaults to long if omitted
buttonType: 'long',onClick: onGooglePaymentsButtonClicked
});
document.getElementById('buy-Now').appendChild(googlePayButton);
}
function onGooglePaymentsButtonClicked() {
// Todo: Perform transaction
const tokenizationSpecification = {
type: 'PAYMENT_GATEWAY',parameters: {
gateway: 'example',gatewayMerchantId: 'gatewayMerchantId'
}
};
const cardPaymentMethod = {
type: 'CARD',tokenizationSpecification: tokenizationSpecification,parameters: {
allowedCardNetworks: ['VISA','CRYPTOGRAM_3DS'],billingAddressrequired: true,billingAddressparameters: {
format: 'FULL',phoneNumberrequired: true
}
}
};
const transactionInfo = {
totalPriceStatus: 'FINAL',totalPrice: '123.45',currencyCode: 'USD'
};
const merchantInfo = {
merchantId: 'BCR2DN6TRPZNDYLL',Only in PRODUCTION
merchantName: 'Example Merchant Name'
};
const paymentDataRequest = Object.assign({},googlePayBaseConfiguration,{
allowedPaymentMethods: [cardPaymentMethod],transactionInfo: transactionInfo,merchantInfo: merchantInfo
});
googlePayClient
.loadPaymentData(paymentDataRequest)
.then(function(paymentData) {
processpayment(paymentData);
}).catch(function(err) {
// Log error: { statusCode: CANCELED || DEVELOPER_ERROR }
});
}
function processpayment(paymentData) {
// Todo: Send a POST request to your processor with the payload
// https://us-central1-devrel-payments.cloudfunctions.net/google-pay-server
// Sorry,this is out-of-scope for this codelab.
return new Promise(function(resolve,reject) {
// @todo pass payment token to your gateway to process payment
const paymentToken = paymentData.paymentMethodData.tokenizationData.token;
console.log('mock send token ' + paymentToken + ' to payment processor');
setTimeout(function() {
console.log('mock response from processor');
alert('done');
console.log(paymentData);
resolve({});
},800);
});
}
</script>
我熟悉PHP和Ruby,并且我不想使用第三方支付网关进行令牌化规范,而希望通过DIRECT方法来实现。我不知道下一步该怎么做以及付款成功后如何比较令牌,并且对DIRECT方法的令牌化规范一无所知。 谢谢大家。
解决方法
如果您要进行网络集成,请考虑使用Google Pay components。如果使用React,则有一个React version,而其他大多数框架都有一个Web Component版本。
对于DIRECT集成,由于额外的合规性义务,强烈建议不要这样做。来自https://developers.google.com/pay/api/web/reference/request-objects#direct:
关键点::直接集成允许商家在其服务器上解密Google Pay响应。要符合资格,您必须符合支付卡行业(PCI)数据安全标准(DSS)1级标准。您的服务器还需要具有必需的基础结构,以安全地处理用户的付款凭证。
代表实际商家提供网关或处理服务的第三方没有资格使用直接集成。有关您如何集成为付款服务提供商的问题,请contact us。
如果您不满足必要的先决条件,我们建议您使用supported gateway来接收付款令牌。
出于兴趣,您为什么不想使用支付网关?
如果您仍然感觉需要直接集成,可以在https://developers.google.com/pay/api/web/guides/resources/payment-data-cryptography上找到有关如何解密令牌的资源。
我不知道下一步该怎么做以及付款成功后如何比较令牌,并且不了解使用DIRECT方法的令牌化规范对我有帮助。
您需要generate a public/private key pair并在Google Pay控制台中注册公共密钥。您还需要更新tokenizationSpecification
以包括公共密钥(下面的示例)
"tokenizationSpecification": {
"type": "DIRECT","parameters": {
"protocolVersion": "ECv2","publicKey": "BOdoXP1aiNp.....kh3JUhiSZKHYF2Y="
}
}
付款成功后如何比较令牌
您可以使用Google Pay进行付款。您需要解密付款令牌并获取付款详细信息并自行处理付款。
DIRECT集成是一个比较困难的集成过程,因此,我强烈建议您首先探索其他替代方法,并且仅在无法使用支付网关的情况下才考虑使用此方法。