iap支付服务器验证python版本

flowerCSDN 2012-05-15

苹果最近修改了规则,正式上线之前申请时请使用沙箱地址

客户端支付成功,发送信息到服务器,进行二次验证。

#-*- coding: utf-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
import simplejson as json
import httplib
def pay_check(request):
    
    uid = request.uid
    params = request.POST.get('params','')
    
    if not params:
        return HttpResponse('false')
    
    
    pay_receipt_dict = json.loads(params) 
    pay_receipt_data = pay_receipt_dict.get('receipt-data')
    pay_receipt = json.dumps({'receipt-data':pay_receipt_data})
    
    headers = {"Content-type": "application/json"}
    #测试地址
    #connect = httplib.HTTPSConnection("sandbox.itunes.apple.com")
    #正式地址
    connect = httplib.HTTPSConnection("buy.itunes.apple.com")
    
    try:
        connect.request("POST", "/verifyReceipt", pay_receipt, headers)
        result = connect.getresponse()
    except:
        return HttpResponse('false')
    
    if result.status != 200:
        return HttpResponse('false')
    
    data = result.read()
    connect.close()
    if data:
        decodedJson = json.loads(data)
        
        status = decodedJson.get('status')
        receipt = decodedJson.get('receipt', {})
        
        transaction_id = receipt.get('transaction_id', '')
        purchase_date = receipt.get('original_purchase_date', '')
        product_id = receipt.get('product_id', '')
        
        if status == 0:
            #返回的status为0时代表支付是成功的,支付成功,最好记录一下
            return HttpResponse('true')
        
    return HttpResponse('false')

验证返回结果格式,注意其中的时间是格林尼治时间。和中国时间相差8小时。

{'receipt': {'bid': 'com.****.shop',
             'bvrs': '1.0.1',
             'item_id': '514487074',
             'original_purchase_date': '2012-05-03 03:46:52 Etc/GMT',
             'original_purchase_date_ms': '1336016812072',
             'original_purchase_date_pst': '2012-05-02 20:46:52 America/Los_Angeles',
             'original_transaction_id': '1000000046751500',
             'product_id': 'store_1',
             'purchase_date': '2012-05-03 03:46:52 Etc/GMT',
             'purchase_date_ms': '1336016812072',
             'purchase_date_pst': '2012-05-02 20:46:52 America/Los_Angeles',
             'quantity': '1',
             'transaction_id': '1000000046751500'},
 'status': 0}

相关推荐

shengbin / 0评论 2012-08-29