def create(self, payloads): self.model_order = Order() order_details = payloads['order_details'] self.model_order.user_id = payloads['user_id'] self.model_order.status = 'pending' referal = db.session.query(Referal).filter_by( referal_code=payloads['referal_code']) if (referal.first() is not None): self.model_order.referal_id = referal.first().as_dict()['id'] db.session.add(self.model_order) try: db.session.commit() data = self.model_order.as_dict() # insert the order details order_id = data['id'] order_items = [] for item in order_details: order_item = OrderDetails() order_item.ticket_id = item['ticket_id'] order_item.count = item['count'] order_item.order_id = order_id # get ticket data ticket = self.get_ticket(item['ticket_id']) if payloads['payment_type'] == 'paypal': order_item.price = ticket.usd_price else: order_item.price = ticket.price db.session.add(order_item) db.session.commit() order_items.append(order_item.as_dict()) if payloads['payment_type'] == 'offline': gross_amount = (item['count'] * ticket.price) payment = Payment() payment.order_id = order_id payment.payment_type = 'offline' payment.gross_amount = gross_amount payment.transaction_time = datetime.datetime.now() payment.transaction_status = 'pending' db.session.add(payment) db.session.commit() # if payloads['payment_type'] == 'paypal': # self.paypalorder(payloads) # save all items return {'error': False, 'data': data, 'included': order_items} except SQLAlchemyError as e: data = e.orig.args return {'error': True, 'data': data}
def save_payload(self, data, payloads): new_payment = Payment() new_payment.transaction_id = data['transaction_id'] new_payment.order_id = data['order_id'] new_payment.gross_amount = data['gross_amount'] new_payment.payment_type = data['payment_type'] new_payment.transaction_time = data['transaction_time'] new_payment.transaction_status = data['transaction_status'] new_payment.bank = data['bank'] new_payment.fraud_status = data['fraud_status'] if 'fraud_status' in data else None new_payment.masked_card = payloads['masked_card'] if 'masked_card' in payloads else None new_payment.saved_token_id = payloads['saved_token_id'] if 'saved_token_id' in payloads else None new_payment.va_number = data['va_number'] if 'va_number' in data else None db.session.add(new_payment) db.session.commit()
def run(): """ Create 4 Payments seeds """ transaction_statuses = ['capture', 'authorize', 'deny'] statuses = ['accept', 'challenge', 'deny'] payment_types = ['bank_transfer', 'credit_card'] banks = ['bri', 'bni', 'permata', 'maybank', 'mandiri', 'bca', 'cimb'] range_start = 10**(7 - 1) range_end = (10**7) - 1 orders = BaseModel.as_list(db.session.query(Order).all()) for i in range(0, 4): order_id = choice(orders)['id'] saved_token_id = randint(range_start, range_end) transaction_id = randint(range_start, range_end) gross_amount = randint(range_start, range_end) transaction_time = datetime.datetime.now() transaction_status = transaction_statuses[randint(0, 2)] masked_card = randint(range_start, range_end) payment_type = payment_types[randint(0, 1)] bank = banks[randint(0, 6)] fraud_status = statuses[randint(0, 2)] new_payment = Payment() new_payment.order_id = order_id new_payment.saved_token_id = saved_token_id new_payment.transaction_id = transaction_id new_payment.gross_amount = gross_amount new_payment.transaction_time = transaction_time new_payment.transaction_status = transaction_status new_payment.masked_card = masked_card new_payment.payment_type = payment_type new_payment.bank = bank new_payment.fraud_status = fraud_status db.session.add(new_payment) db.session.commit()
def confirm(self, payload, user_id, request): response = ResponseBuilder() emailservice = EmailService() transaction_exist = db.session.query(Payment).filter_by( transaction_id=payload['transaction_id']).first() if transaction_exist: return response.set_error(True).set_message( 'this transaction have been used before').set_data( None).build() paypal_details = self.get_paypal_detail(payload['transaction_id']) if paypal_details == False: return response.set_error(True).set_message( 'Transaction id not found').set_data(None).build() paypal_details_amount = int( paypal_details['transactions'][0]['amount']['total'].split('.')[0]) order_ = db.session.query(Order).filter_by( id=payload['order_id']).first() if order_ is None: return response.set_error(True).set_data(None).set_message( 'Order not found').build() user = order_.user order_details = db.session.query(OrderDetails).filter_by( order_id=payload['order_id']).all() check_total = 0 discount = 0 for order in order_details: check_total += order.price * order.count if order_.referal_id is not None: discount = check_total * order_.referal.discount_amount if check_total - discount <= paypal_details_amount: payment_exist = db.session.query(Payment).filter_by( transaction_id=payload['transaction_id']).first() if payment_exist: return response.set_data(None).set_message( 'Payment had been completed!').set_error(True).build() payment = Payment() payment.order_id = payload['order_id'] payment.transaction_id = payload['transaction_id'] payment.gross_amount = paypal_details_amount payment.payment_type = 'paypal' payment.transaction_status = "captured" try: db.session.add(payment) db.session.commit() except SQLAlchemyError as e: data = e.orig.args return response.set_data(None).set_message(data).set_error( True).build() items = db.session.query(OrderDetails).filter_by( order_id=payment.order_id).first() url_invoice = request.host + '/invoices/' + order_.id if items.ticket.type == TICKET_TYPES['exhibitor']: payload = {} payload['user_id'] = user.id payload['ticket_id'] = items.ticket_id UserTicketService().create(payload) self.create_booth(user) user_query = db.session.query(User).filter_by(id=user.id) user_query.update({'role_id': ROLE['booth']}) redeem_payload = {} redeem_payload['ticket_id'] = items.ticket_id redeem_payload['codeable_id'] = user.id RedeemCodeService().purchase_user_redeems(redeem_payload) get_codes = db.session.query(RedeemCode).filter_by( codeable_type='user', codeable_id=user.id).all() code = [] for get_code in get_codes: code.append("<li>%s</li>" % (get_code.code)) li = ''.join(code) template = "<h3>You have complete the payment with order_id = %s</h3><h4>Here are the redeem codes for claiming full 3 days ticket at devsummit event as described in the package information : </h4>%s<h3>Use the above code to claim your ticket</h3><h3>Thank you for your purchase</h3>" % ( order_.id, li) template += "<h4>And here is your Invoice:</h4>" template += '<a href="' + url_invoice + '">Klik here to show the invoice</a>' email = emailservice.set_recipient(user.email).set_subject( 'Congratulations !! you received exhibitor code' ).set_sender('*****@*****.**').set_html( template).build() mail.send(email) if items.ticket.type == TICKET_TYPES['hackaton']: payload = {} payload['user_id'] = user.id payload['ticket_id'] = items.ticket_id UserTicketService.create(payload) self.create_hackaton_team(user, items.ticket_id) user_query = db.session.query(User).filter_by(id=user.id) user_query.update({'role_id': ROLE['hackaton']}) hacker_team = db.session.query(HackerTeam).order_by( HackerTeam.created_at.desc()).first() redeem_payload = {} redeem_payload['codeable_type'] = TICKET_TYPES['hackaton'] redeem_payload['codeable_id'] = hacker_team.id, redeem_payload['count'] = items.ticket.quota RedeemCodeService().create(redeem_payload) get_codes = db.session.query(RedeemCode).filter_by( codeable_type='hackaton', codeable_id=hacker_team.id).all() code = [] for get_code in get_codes: code.append("<li>%s</li>" % (get_code.code)) li = ''.join(code) template = "<h3>You have complete the payment with order_id = %s</h3><h4>Here your redeem codes : </h4>%s<h3>Share the above code to your teammate, and put it into redeem code menu to let them join your team and claim their ticket</h3><h3>Thank you for your purchase</h3>" % ( order_.id, li) template += "<h4>And here is your Invoice:</h4>" template += '<a href="' + url_invoice + '">Klik here to show the invoice</a>' email = emailservice.set_recipient(user.email).set_subject( 'Congratulations !! you received hackaton code' ).set_sender('*****@*****.**').set_html( template).build() mail.send(email) else: result = None for order in order_details: for i in range(0, order.count): payload = {} payload['user_id'] = user.id payload['ticket_id'] = order.ticket_id result = UserTicketService().create(payload) if (result and (not result['error'])): template = "<h3>Congratulation! you have the previlege to attend Indonesia Developer Summit</h3>" template += "<h4>Here is your Invoice:</h4>" template += '<a href="' + url_invoice + '">Klik here to show the invoice</a>' template += "<h5>Thank you.</h5>" email = emailservice.set_recipient(user.email).set_subject( 'Devsummit Ticket Invoice').set_sender( '*****@*****.**').set_html(template).build() mail.send(email) confirmed_order = db.session.query(Order).filter_by( id=payment.order_id) confirmed_order.update({'status': 'paid'}) db.session.commit() send_notification = FCMService().send_single_notification( 'Payment Status', 'Your payment has been confirmed', user.id, ROLE['admin']) return response.set_data(None).set_message( 'Purchase Completed').build() else: return response.set_error(True).set_message( 'Paypal amount did not match').build()
def create(self, payloads, user): response = ResponseBuilder() if user['role_id'] == ROLE['hackaton']: return response.set_data(None).set_message( 'Hackaton attendee cannot buy ticket').set_error(True).build() order_details = payloads['order_details'] # check if it's hackaton if order_details[0]['ticket_id'] == 10: # check if proposal already submited # disable hackaton now return response.set_data(None).set_message( 'Hackaton registration is over.').set_error(True).build() # if self.hackatonproposalservice.check_hackaton_proposal_exist(user['id']): # return response.set_error(True).set_message('Hackaton cannot be submitted twice, our admin is in the process of verifying it, you will receive notification once it is done').set_data(None).build() self.model_order = Order() self.model_order.user_id = payloads['user_id'] self.model_order.status = 'pending' # Referal code checking referal = db.session.query(Referal).filter_by( referal_code=payloads['referal_code']) if referal.first() is not None: # verify quota and update quota if referal.first().quota > 0: referal.update({'quota': referal.first().quota - 1}) self.model_order.referal_id = referal.first().as_dict()['id'] else: # handle for referal code exceed limit / quota return response.set_error(True).set_data(None).set_message( 'quota for specified code have exceeded the limit').build( ) # place order db.session.add(self.model_order) try: db.session.commit() data = self.model_order.as_dict() # insert the order details order_id = data['id'] order_items = [] for item in order_details: order_item = OrderDetails() order_item.ticket_id = item['ticket_id'] order_item.count = item['count'] order_item.order_id = order_id # get ticket data ticket = self.get_ticket(item['ticket_id']) if payloads['payment_type'] == 'paypal': order_item.price = ticket.usd_price else: order_item.price = ticket.price db.session.add(order_item) db.session.commit() order_item_dict = order_item.as_dict() order_item_dict['ticket'] = order_item.ticket.as_dict() order_items.append(order_item_dict) if payloads['payment_type'] == 'offline': gross_amount = (item['count'] * ticket.price) if referal.first() is not None: # discount on gross amount gross_amount -= gross_amount * referal.first( ).discount_amount payment = Payment() payment.order_id = order_id payment.payment_type = 'offline' payment.gross_amount = gross_amount payment.transaction_time = datetime.datetime.now() payment.transaction_status = 'pending' db.session.add(payment) db.session.commit() # check if ticket is free if order_details[0]['ticket_id'] == 10: # hackaton proposal hackatonproposal = { 'github_link': payloads['hacker_team_name'], 'order_id': order_id } hack_result = self.hackatonproposalservice.create( hackatonproposal) elif gross_amount == 0 or (referal.first() and referal.first().discount_amount == 1): # call verify service ov_service = OrderVerificationService() ov_service.admin_verify(self.model_order.id, request, payloads['hacker_team_name']) # save all items return {'error': False, 'data': data, 'included': order_items} except SQLAlchemyError as e: data = e.orig.args return {'error': True, 'data': data}
def confirm(self, payload): response = ResponseBuilder() transaction_exist = db.session.query(Payment).filter_by(transaction_id=payload['transaction_id']).first() if transaction_exist: return response.set_error(True).set_message('this transaction have been used before').set_data(None).build() paypal_details = self.get_paypal_detail(payload['transaction_id']) if paypal_details == False: return response.set_error(True).set_message('Transaction id not found').set_data(None).build() paypal_details_amount = int(paypal_details['transactions'][0]['amount']['total'].split('.')[0]) order_ = db.session.query(Order).filter_by(id=payload['order_id']).first() if order_ is None: return response.set_error(True).set_data(None).set_message('Order not found').build() user = order_.user order_details = db.session.query(OrderDetails).filter_by(order_id=payload['order_id']).all() check_total = 0 for order in order_details: check_total += order.price * order.count if check_total == paypal_details_amount: payment_exist = db.session.query(Payment).filter_by(transaction_id=payload['transaction_id']).first() if payment_exist: return response.set_data(None).set_message('Payment had been completed!').set_error(True).build() payment = Payment() payment.order_id = payload['order_id'] payment.transaction_id = payload['transaction_id'] payment.gross_amount = paypal_details_amount payment.payment_type = 'paypal' payment.transaction_status = "captured" try: db.session.add(payment) db.session.commit() except SQLAlchemyError as e: data = e.orig.args return response.set_data(None).set_message(data).set_error(True).build() items = db.session.query(OrderDetails).filter_by(order_id=payment.order_id).first() if items.ticket.type == TICKET_TYPES['exhibitor']: payload = {} payload['user_id'] = user.id payload['ticket_id'] = items.ticket_id UserTicketService().create(payload) self.create_booth(user) user_query = db.session.query(User).filter_by(id=user.id) user_query.update({ 'role_id': ROLE['booth'] }) redeem_payload = {} redeem_payload['ticket_id'] = items.ticket_id redeem_payload['codeable_id'] = user.id RedeemCodeService().purchase_user_redeems(redeem_payload) # user_role = db.session.query(User).filter_by(id=user.id) # user_role.update({ # 'updated_at': datetime.datetime.now(), # 'role_id': 3 # }) # ticket_quota = order_details.ticket.quota # for i else: for order in order_details: for i in range(0, order.count): payload = {} payload['user_id'] = user.id payload['ticket_id'] = order.ticket_id UserTicketService().create(payload) confirmed_order = db.session.query(Order).filter_by(id=payment.order_id) confirmed_order.update({ 'status': 'paid' }) db.session.commit() return response.set_data(None).set_message('Purchase Completed').build() else: return response.set_error(True).set_message('Paypal amount did not match').build()