def done(self, request, cleaned_data): #product = Product.objects.get(sku=cleaned_data['products']) address = get_object_or_none(Address, pk=cleaned_data['address']) if not address or not request.user.get_profile() == address.content_object: address = Address() address.street = '366 Adelaide' address.city = 'Toronto' address.province = 'ON' address.postal_code = 'M5V1R9' address.country = 'CA' # stuff necessary values into dictionary... to be encoded. param = {'trnCardOwner': cleaned_data['billing_name'], 'trnCardNumber': cleaned_data['cc_number'], 'trnExpMonth': cleaned_data['cc_expiry'].split('/')[0], 'trnExpYear': cleaned_data['cc_expiry'].split('/')[1], 'ordName': cleaned_data['billing_name'], 'ordEmailAddress': cleaned_data['email'], 'ordPhoneNumber': cleaned_data['phone'], 'ordAddress1': address.street, 'ordAddress2': '', 'ordCity':address.city, 'ordProvince': address.province, 'ordPostalCode': address.postal_code, 'ordCountry': address.country, 'requestType': 'BACKEND', 'trnOrderNumber': '%s' % int(time.time()), 'merchant_id': settings.TD_MERCHANT_ID, } product_count = 1 total_cost = 0 product_list = [] if isinstance(cleaned_data['products'],ListType): for sku in cleaned_data['products']: product = Product.objects.get(sku=sku) param['prod_id_%s' % product_count] = product.sku param['prod_quantity_%s' % product_count] = '1' param['prod_name_%s' % product_count] = product.name param['prod_cost_%s' % product_count] = product.amount total_cost += Decimal(product.amount) product_count = product_count + 1 product_list.append(product) else: product = Product.objects.get(sku=cleaned_data['products']) param['prod_id_1'] = product.sku param['prod_quantity_1'] = '1' param['prod_name_1'] = product.name param['prod_cost_1'] = product.amount total_cost += Decimal(product.amount) product_list.append(product) param['trnAmount'] = total_cost if total_cost > 0: #param = [fix_encoding(p) for p in param] fixed_param = {} for x, y in param.items(): fixed_param[x] = fix_encoding(y) encoded = urllib.urlencode(fixed_param) # push the transaction to the bank handle = urllib.urlopen(settings.TD_MERCHANT_URL, encoded) result = handle.read().split('&') # parse the result string back into a dictionary results = {} for r in result: r2 = r.split('=') r2[0] = urllib.unquote_plus(r2[0]) r2[1] = urllib.unquote_plus(r2[1]) results[r2[0]] = r2[1] # TODO: any other processing/recordkeeping we want to do? # do I want to save this into the db? p = Payment() p.cc_type = cleaned_data['cc_type'] p.billing_name=cleaned_data['billing_name'] p.phone=cleaned_data['phone'] p.email=cleaned_data['email'] p.approved=results['trnApproved'] p.response="\n".join(result) p.amount = total_cost p.save() for prod in product_list: p.products.add(prod) p.save() # return based on value if results['trnApproved'] == '1': # send receipt message = loader.get_template("creditcard/receipt.html") c = Context({'name': cleaned_data['billing_name'], 'date': datetime.today(), 'txid': results['trnOrderNumber'], 'product': product_list, 'amount': total_cost}) body = message.render(c) send_mail(subject='Credit Card Receipt', txtMessage=body, htmlMessage=None, fromemail='Engineers Without Borders Canada <*****@*****.**>', recipients=[cleaned_data['email']], use_template=False) # return success return (True, results['trnId'], results['trnOrderNumber']) else: return (False, results['messageText']) else: return (True, '00000', '00000')