我正在尝试使用Typescript在React项目中在结帐时实施Apple Pay。我已经安装了braintree-web软件包,并按照Braintree网站上的说明进行了初始化ApplePaySession
。尽管打字稿允许我编译项目,但在浏览器中对new ApplePaySession(3, paymentRequest);
的调用未定义。我伤了脑筋,但我不明白出了什么问题。这是我如何初始化ApplePay的代码段。
import { applePay, ApplePayStatusCodes, ApplePaySession } from 'braintree-web';
...
beginApplePay() {
if (!this.btApplePayInstance) {
//throw an erro
}
const paymentRequest = this.createPaymentRequest();
const session = this.createSession(paymentRequest);
session && session.begin();
}
createSession(
paymentRequest: braintree.ApplePayPaymentRequest
): braintree.ApplePaySession | null {
let session: braintree.ApplePaySession;
console.log('creating session');
console.log('Sample payment request: ', paymentRequest);
console.log(
'Supports API v3: ' + window.ApplePaySession.supportsVersion(3)
);
try {
debugger;
session = new ApplePaySession(3, paymentRequest); //this is where it fails
console.log(session);
} catch (err) {
console.log(err);
//throw an error
return null;
}
这是我的浏览器说的:TypeError: undefined is not a constructor (evaluating 'new braintree_web__WEBPACK_IMPORTED_MODULE_1__["ApplePaySession"](3, paymentRequest)')
任何对出问题的见解将不胜感激。
[我发现在顶级Js文件中添加<script src="https://js.braintreegateway.com/web/3.55.0/js/apple-pay.min.js"></script>
对我有用,然后在使用会话时仅使用@ ts-ignore。
我知道很难解决,如果发现更好的解决方案,请分享。