iOS端
- (void)verifyPurchaseWithPaymentTransactionSuccess:(void(^)(NSString *success))successBlock
failure:(void(^)(NSString *error))failureBlock{
NSString *urlStr = @"/appstorepay/receiptdata";
NSURL *receiptURL = [[NSBundle mainBundle] appStoreReceiptURL];
NSData *receipt = [NSData dataWithContentsOfURL:receiptURL];
NSDictionary *requestContents = @{
@"receipt_data": [receipt base64EncodedStringWithOptions:0],
// @"XDEBUG_SESSION_START":@"12477"
};
[NetworkingUtil postWithUrlString:urlStr parameters:requestContents success:^(NSDictionary *data) {
successBlock(data[@"msg"]);
} failure:^(NSError *error) {
failureBlock([NSString stringWithFormat:@"验证购买过程中发生错误, 错误信息:%@",error.localizedDescription]);
}];
}
Ruby
#encoding:utf-8
module AF
class AppStorePay < Grape::API
@@ALIPID=AF::CFG.ALIPAY[:AppID]
helpers AF::Helpers
namespace 'appstorepay' do
helpers do
def getReceiptData (receipt_data,isSandbox = false)
if (isSandbox)
nvuri = "https://sandbox.itunes.apple.com/verifyReceipt" #地址
else
nvuri = "https://buy.itunes.apple.com/verifyReceipt" #地址
end
#不知道什么原因,iOS传过来的base64个转码了,没找到解决方法,只能替换用+号替换空格
receipt_data = "{\"receipt-data\" : \"#{receipt_data.tr(" ", "+")}\"}"
req=EM::HttpRequest.new(nvuri).post(:body=>receipt_data)
# msg = "reqError#{req.error},reqResponse#{JSON(req.response)["status"]}";
dataStatus = JSON(req.response)["status"] #从苹果服务器返回数据,先编码
if dataStatus == 21007
getReceiptData order_id,total_fee,receipt_data, true
elsif dataStatus == 21008
getReceiptData order_id,total_fee,receipt_data, false
elsif dataStatus == 0
return msg = "#{req.response}";
end
end
end
post 'receiptdata' do
receipt_data=params[:receipt_data]
rv=ReturnValue.new
if receipt_data.nil?
rv.msg = "凭证为空"
else
isSandbox = true
rv.msg = getReceiptData(receipt_data,isSandbox)
end
status(200)
body(rv)
end #end post sign
end #end namespace
end #end class
end #end module
=begin
# 21000 App Store不能读取你提供的JSON对象
# 21002 receipt-data域的数据有问题
# 21003 receipt无法通过验证
# 21004 提供的shared secret不匹配你账号中的shared secret
# 21005 receipt服务器当前不可用
# 21006 receipt合法,但是订阅已过期。服务器接收到这个状态码时,receipt数据仍然会解码并一起发送
# 21007 receipt是Sandbox receipt,但却发送至生产系统的验证服务
# 21008 receipt是生产receipt,但却发送至Sandbox环境的验证服务
=end
因为不懂Ruby,公司又没Ruby技术员,只能自己瞎蒙,代码功能是实现了,但是总感觉哪里写得有问题,如果有朋友会,希望能指出(Ruby的坑太深了T.T)
网友评论