Esempio n. 1
0
    def perform_credits_oprations(self):
        try:
            apple_product = AppleProducts.objects.get(ProductId=self.product)
        except AppleProducts.DoesNotExist:
            raise WrongParameter(parameter='Product')

        cost = Decimal(apple_product.ViewsCount * self.quantity)

        try:
            transaction = Transactions(Purchaser=self.user.Purchaser,
                                       ProductId=apple_product,
                                       Cost=cost,
                                       ViewsCount=apple_product.ViewsCount,
                                       AppleTransactionId=self.transaction_id)
            transaction.save()
        except IntegrityError:
            raise Conflict(message='Duplicated transaction.')

        self.user.Purchaser.Balance += cost
        self.user.Purchaser.save()
Esempio n. 2
0
    def perform_credits_oprations(self):
        try:
            apple_product = AppleProducts.objects.get(ProductId=self.product)
        except AppleProducts.DoesNotExist:
            raise WrongParameter(parameter='Product')

        cost = Decimal(apple_product.ViewsCount * self.quantity)

        try:
            transaction = Transactions(Purchaser=self.user.Purchaser,
                                       ProductId=apple_product,
                                       Cost=cost,
                                       ViewsCount=apple_product.ViewsCount,
                                       AppleTransactionId=self.transaction_id)
            transaction.save()
        except IntegrityError:
            raise Conflict(message='Duplicated transaction.')

        self.user.Purchaser.Balance += cost
        self.user.Purchaser.save()
Esempio n. 3
0
def buy (request):
    if request.method != 'POST':
        return HttpResponse ("There is POST method only.")
    keys = request.POST.keys()
    response = {}
    if "API" not in keys:
        response["Error"] = {"ErrorCode": "666", "ErrorDescription": "There is no API parameter."}
        return HttpResponse (json.dumps(response))
    else:
        api_ver = request.POST.get("API")
    if api_ver != "1":
        response["Error"] = {"ErrorCode": "777", "ErrorDescription": "Wrong API version."}
        return HttpResponse (json.dumps(response))
    else:
        response["Error"] = {"ErrorCode": "", "ErrorDescription": ""}
    
    key = request.POST.get('Key', None)
    apple_purchase = request.POST.get('AppleReceiptData', None)
    
    if not key or not apple_purchase:
        response["Error"] = {"ErrorCode": "100", "ErrorDescription": "Authentication error."}
        return HttpResponse (json.dumps(response))
    
    from restserver.pipture.models import PipUsers
    
    try:
        purchaser = PipUsers.objects.get(Token=key)
    except PipUsers.DoesNotExist:
        response["Error"] = {"ErrorCode": "100", "ErrorDescription": "Authentication error."}
        return HttpResponse (json.dumps(response))

    #-----------------------To Apple Server----------------------------
    data_json = json.dumps({"receipt-data" : "%s" % (apple_purchase)})
    url = 'https://buy.itunes.apple.com/verifyReceipt'
    req = urllib2.Request(url=url, data=data_json)
    response_apple = urllib2.urlopen(req)
    result = response_apple.read()
    result_json = json.loads(result)
    
    if result_json['status'] != 0:
        response["Error"] = {"ErrorCode": "1", "ErrorDescription": "Purchase Validation error."}
        return HttpResponse (json.dumps(response))
    else:
        apple_product_response = result_json['receipt']['product_id']
        apple_product_quantity = int(result_json['receipt']['quantity'])
        apple_transaction_id = result_json['receipt']['transaction_id']
        
    #-----------------------To Apple Server----------------------------
    
    from restserver.pipture.models import AppleProducts
    from restserver.pipture.models import Transactions
    from django.db import IntegrityError
    
    try:
        apple_product = AppleProducts.objects.get (ProductId=apple_product_response)
    except AppleProducts.DoesNotExist:
        response["Error"] = {"ErrorCode": "2", "ErrorDescription": "Wrong product."}
        return HttpResponse (json.dumps(response))
    if apple_product.ProductId == "com.pipture.Pipture.credits":
        try:
            t = Transactions(UserId=purchaser, ProductId=apple_product, Cost=Decimal(apple_product.Price * apple_product_quantity), AppleTransactionId=apple_transaction_id)
            t.save()
        except IntegrityError:
            response["Error"] = {"ErrorCode": "3", "ErrorDescription": "Duplicate transaction Id."}
            return HttpResponse (json.dumps(response))

        purchaser.Balance = Decimal (purchaser.Balance + Decimal(apple_product.Price * apple_product_quantity))
        purchaser.save()
        response["Balance"] = "%s" % (purchaser.Balance)
        return HttpResponse (json.dumps(response))
 
    else:
        response["Error"] = {"ErrorCode": "2", "ErrorDescription": "Wrong product."}
        return HttpResponse (json.dumps(response))