Beispiel #1
0
	def post(self):
		args = parse.parse_args()
		try:
			c_id = args['contact_id']
			dcontact = Contact.query.get(c_id).first()
		except:
			abort(404, message="Contact id: %s doesn't exist" % (c_id))
		inv = Invoice()
		inv.email = args['email']
		inv.invoice_date = args['invoice_date']
		inv.recipient_note = args['recipient_note']
		inv.subtotal = args['subtotal']
		inv.total = args['total']
		inv.paid = False
		inv.contact_id = c_id
		import paypalrestsdk
		paypalrestsdk.configure({
				"mode": "sandbox", # PAYPAL_MODE
				"client_id": app.config['PAYPAL_CLIENT_ID'], # PAYPAL_CLIENT_ID
				"client_secret": app.config['PAYPAL_CLIENT_SECRET'] })		# PAYPAL_CLIENT_SECRET
		paypal_invoice = paypalrestsdk.Invoice({
				"merchant_info": {
					"email":"*****@*****.**",
					"first_name":"Francisco",
					"last_name": "Barcena",
					"business_name":"fdev.tk",
					"phone":{"country_code": "001","national_number":"5555555555"},
					"address":{
						"line1":"123 Fake St. Apt.A",
						"city":"Fake City",
						"country_code":"US",
						"state":"California"
					},
				},
				"billing_info":[{"email":request.form["email"]}],
				"note":"MAKE MONEY F*CK B*TCHES"
		})
		# inv_lines (from list in args)
		the_items = []
		for the_item in inv_lines:
			# append to the_items (for paypal)
			the_items.append(dict({"name":the_item.d_description.data,"quantity":str(the_item.qty.data),"unit_price":{"currency":"USD","value":str(the_item.unit_price.data)}}))
			# Create and append to Invoice model
			new_invoice_line = InvoiceLine()
			new_invoice_line.description
			new_invoice_line.quantity
			new_invoice_line.unit_price
			new_invoice_line.amount
			inv.invoice_lines.append(new_invoice_line)	
		paypal_invoice.items = the_items
		error = None
		if paypal_invoice.create():
			print('paypal invoice created')
			# Add invoice lines here (from list as argument) 
			db.session.add(inv)
			db.session.commit()
		else:
			error = paypal_invoice.error
			abort(404, message="Invoice creation error: %s" % (error)) 
		return inv, 201