使用lambda通过Node.js进行iOS收据验证

编程入门 行业动态 更新时间:2024-10-09 12:28:38
本文介绍了使用lambda通过Node.js进行iOS收据验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在Swift中开发一个iOS应用,并尝试对应用内购买实施收据验证.我无法弄清楚如何在Swift中实现此目的,因此,我在这个问题.我的Swift代码如下:

I am developing an iOS app in Swift and attempting to implement receipt validation for an in-app purchase. I couldn't figure out how to achieve this in Swift, so instead I tried having my app send the request through a Lambda function writtin in Node.js, after seeing Giulio Roggero's example in this question. My Swift code looks like this:

let receiptPath = Bundle.main.appStoreReceiptURL?.path if FileManager.default.fileExists(atPath: receiptPath!){ var receiptData:NSData? do{ receiptData = try NSData(contentsOf: Bundle.main.appStoreReceiptURL!, options: NSData.ReadingOptions.alwaysMapped) } catch{ print("ERROR: " + error.localizedDescription) } let receiptString = receiptData?.base64EncodedString(options: .endLineWithLineFeed) let invocationRequest = AWSLambdaInvokerInvocationRequest() invocationRequest?.functionName = "sendReceiptRequest" invocationRequest?.invocationType = AWSLambdaInvocationType.requestResponse invocationRequest?.payload = ["receipt-data" : receiptString!, "password" : SUBSCRIPTION_SECRET] let lambdaInvoker = AWSLambdaInvoker.default() lock() lambdaInvoker.invoke(invocationRequest!).continue(with: AWSExecutor.mainThread(), with: { (task:AWSTask!) -> AnyObject! in if task.error != nil { self.sendErrorPopup("Error: \(task.error?.localizedDescription)") } else { print("TOKEN: ", task.result) } self.unlock() return nil })}

我的Lambda node.js函数如下例所示:

My Lambda node.js function looks like this, following the example:

function (receiptData_base64, password, production, cb) { var url = production ? 'buy.itunes.apple' : 'sandbox.itunes.apple' var receiptEnvelope = { "receipt-data": receiptData_base64, "password":password }; var receiptEnvelopeStr = JSON.stringify(receiptEnvelope); var options = { host: url, port: 443, path: '/verifyReceipt', method: 'POST', headers: { 'Content-Type': 'application/x-www-form-urlencoded', 'Content-Length': Buffer.byteLength(receiptEnvelopeStr) } }; var req = https.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log("body: " + chunk); cb(true, chunk); }); res.on('error', function (error) { console.log("error: " + error); cb(false, error); }); }); req.write(receiptEnvelopeStr); req.end(); }

但是,在通过lambda测试或通过我的应用程序运行此代码时,我收到一条错误消息,提示为Response body: {"errorMessage":"true"}.我注意到,如果我对代码进行调整,则会产生更多预期的错误-例如,如果我对收据数据有其他值,则会得到21002错误代码作为响应,并且如果我将"production"更改为true ,我收到21007错误.问题的部分原因是我不确切知道回调应该如何工作-https.request中的块是否与我在Swift中要执行的操作正确?我觉得收据数据格式正确,因为更改收据数据会产生不同的结果,为什么最终结果仍然是错误的?

However, when running this code, either through a lambda test or through my app, I get an error message that simply says Response body: {"errorMessage":"true"}. I've noticed that if I tweak the code I can create more expected errors- For instance, if I have some other value for the receipt-data, I get a 21002 error code in response, and if I change "production" to true, I get a 21007 error. Part of the problem is that I don't know exactly how the callback is supposed to work-- is the block inside https.request correct for what I'm trying to do in Swift? I get the impression that the receipt-data is correctly formatted since changing it yields a different result, so why is the end result still an error?

我以前没有注意到的是,当我运行Lambda函数时,出现"body:(receipt data)"行,其中(receipt data)是我发送给该函数的base 64编码数据.这使我怀疑我根本没有到达错误回调块,并且该错误与我将回调结果发送回我的应用程序的方式有关.这是什么块:

Something I previously didn't notice is that when I run the Lambda function, the line "body: (receipt data)" appears, where (receipt data) is the base 64 encoded data I sent to the function. This makes me suspect I'm not reaching the error callback block at all, and that the error has something to do with the way I send the result of the callback back to my app. What is this block:

var req = https.request(options, function(res) { res.setEncoding('utf8'); res.on('data', function (chunk) { console.log("body: " + chunk); cb(true, chunk); }); res.on('error', function (error) { console.log("error: " + error); cb(false, error); }); });

该怎么办?我是否需要启用一些权限才能接收回调?

supposed to do? Is it possible I need to enable some permission to receive the callback?

推荐答案

我最终通过遵循 示例,并使用Python代替Node.js.我之前仍然不知道我的代码到底出了什么问题,但是在Swift中使用base-64编码,然后将收据数据和密码发送给Python Lambda函数,这给了我正确的结果.

Ultimately I solved the problem by following this example and using Python instead of Node.js. I still don't know exactly what was wrong with my code before, but base-64 encoding in Swift and then sending the receipt data and password to a Python Lambda function gave me the correct result.

更多推荐

使用lambda通过Node.js进行iOS收据验证

本文发布于:2023-11-26 09:10:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1633505.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:收据   Node   lambda   iOS   js

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!