如何从Node.js中的Post请求获取收据数据

时间:2019-02-08 11:04:21

标签: javascript node.js server in-app-purchase

我正在尝试将receipt发送到我的Node.js server。目前,它会打印出Firebase中的receipt-data,表明它是从我从POST发送来的iOS App请求中获得的。但是,我无法在我的代码中正确传递要在const receipt = request.body;中进行验证的代码。我该如何以正确的方式传递它?谢谢。

exports.receiptValidation = functions.https.onRequest((request, response) => {

    const receipt = request.body;
    console.log(receipt);

    iap.config({
        applePassword: 'MySharedAppleKey',
        test: true
    });

    iap.setup((error) => {
        if (error) {
            console.log('Failed to validate receipt', error);
        }
       });

    iap.validate(receipt).then((pResponse) => {
            //Check if receipt is valid
            if (iap.isValidated(pResponse)) {
                console.log('The receipt is valid', pResponse);
                response.send(pResponse);
            }
            return pResponse;
        })
        .catch((error) => {
            console.log(`Sorry couldn't validate receipt`, error);
            response.send(error);
            return error
        });
});

这是我在Firebase中的输出。我正在使用firebase function

enter image description here

1 个答案:

答案 0 :(得分:1)

我通过更改SwiftServer code中的收据数据字符串来解决了该问题

迅速

let receiptData = receipt!.base64EncodedString()
let requestReceiptDict = ["receipt": receiptData]

Node.js

const receipt = request.body.receipt;
console.log(receipt);