Firebase Firestore + Cloud Function服务器端验证应用内购买收据(Swift + NodeJS)

时间:2019-04-21 10:49:33

标签: swift firebase google-cloud-firestore in-app-purchase google-cloud-functions

Firebase Firestore +云功能服务器端验证收据(应用内购买),最终结果:状态21002,表示:收据属性中的数据格式错误或丢失。

步骤1)我使用快速将应用内购买收据存储到Firestore中,Firestore数据库自然将其存储为blob

step2)一旦收据进入消防站,云功能就会触发。 Firebase函数将收据发送到Apple iTunes Store,但最终结果是:状态21002。

我想在将收据从base64EncodedData转换为Firestore blob,然后在云函数上检索收据时,收据的格式错误。如何获得正确的收据格式,以便将其发送到Apple iTunes Store以进行收据验证?

第1步快速代码:

func sendReceiptToFirestore() {
        guard let appStoreReceiptURL = Bundle.main.appStoreReceiptURL,
            FileManager.default.fileExists(atPath: appStoreReceiptURL.path) else {return}
        do {
            let rawReceiptData = try Data(contentsOf: appStoreReceiptURL)
            let receiptData = rawReceiptData.base64EncodedData()
            //Standard way to store receiptData to firestore, i did that but not posting full code here for cleanliness
        }   catch {}
    }

第2步云功能代码:

const request = require("request-promise");
exports.verifyReceiptIAP = functions.firestore
    .document('userReceiptIAP/{userUid}')
    .onWrite((change, context) => {
        const receipt = change.after.data().receiptData  
        const userUid = context.params.userUid
        var options = {
            method: 'POST',
            url: 'https://sandbox.itunes.apple.com/verifyReceipt',
            body: ({
                "receipt-data" : receipt,
                "password" : "8bf2f13bedc6456f97c9323c743a3168"
            }),
            json: true
        };
            return request(options).then(function (response) {
            console.log(response);
            return null
        })

    });

感谢您的帮助

1 个答案:

答案 0 :(得分:1)

解决了,我需要更改编码方法。 更改

let receiptData = rawReceiptData.base64EncodedData()

收件人

let receiptData = rawReceiptData.base64EncodedString()

如果您要使用Firestore和云功能进行服务器端收据验证,那么所涉及的示例代码应该会很有帮助