async def process_payment(pay_msg: dict, flask_app: Flask): """Render the payment status.""" if not flask_app or not pay_msg: raise QueueException('Flask App or token not available.') with flask_app.app_context(): logger.debug('entering process payment: %s', pay_msg) # capture_message(f'Queue Issue: Unable to find payment.id={payment_id} to place on email queue') # return if pay_msg.get('paymentToken', {}).get('statusCode') == PaymentState.TRANSACTION_FAILED.value: # TODO: The customer has cancelled out of paying, so we could note this better # technically the payment for this service is still pending logger.debug('Failed transaction on queue:%s', pay_msg) return complete_payment_status = [PaymentState.COMPLETED.value, PaymentState.APPROVED.value] if pay_msg.get('paymentToken', {}).get('statusCode') in complete_payment_status: logger.debug('COMPLETED transaction on queue: %s', pay_msg) if payment_token := pay_msg.get('paymentToken', {}).get('id'): if payment := Payment.find_by_payment_token(payment_token): if update_payment := await update_payment_record(payment): payment = update_payment await furnish_receipt_message(qsm, payment) else:
def delete(self, nr_id, payment_id): try: # Find the existing name request nr_model = RequestDAO.query.get(nr_id) if not nr_model: return jsonify(message=f'No NR found with id: {nr_id}.'), 404 # Find the existing payment record payment = PaymentDAO.find_by_payment_token(payment_id) if not payment: return jsonify(message=f'No payment record with id: {payment_id}.'), 404 # check payment record state is CREATED current_payment_state = payment.payment_status_code if current_payment_state != PaymentStatusCode.CREATED.value: return jsonify(message=f'Unable to cancel a payment record in {current_payment_state} state.'), 400 try: # cancelling may change with refactor cancel_payment(payment.payment_token) payment.payment_status_code = PaymentState.CANCELLED.value payment.save_to_db() response_data = nr_model.json() # Add the list of valid Name Request actions for the given state to the response response_data['actions'] = get_nr_state_actions(nr_model.stateCd, nr_model) return jsonify(response_data), 200 except PaymentServiceError as err: # should only get here if there was a conflict (payment went through before cancel happened) return handle_exception(err, err.message, 409) except Exception as err: return handle_exception(err, repr(err), 500)
async def test_furnish_receipt_message(app, session, stan_server, event_loop, client_id, entity_stan, future): """Assert that events are placed on the email queue and the payment is marked furnished.""" from queue_common.messages import create_cloud_event_msg from queue_common.service import ServiceWorker from queue_common.service_utils import subscribe_to_queue from namex_pay.worker import APP_CONFIG, furnish_receipt_message, qsm from namex.models import Request, State, Payment print('test vars') print(app, session, stan_server, event_loop, client_id, entity_stan, future) # setup PAYMENT_TOKEN = 'dog' NR_NUMBER = 'NR B000001' name_request = Request() name_request.nrNum = NR_NUMBER name_request.stateCd = State.DRAFT name_request._source = 'NRO' name_request.save_to_db() payment = Payment() payment.nrId = name_request.id payment._payment_token = PAYMENT_TOKEN payment._payment_status_code = 'COMPLETED' payment.furnished = False payment.save_to_db() # file handler callback msgs = [] s = ServiceWorker() s.sc = entity_stan qsm.service = s async def cb_handler(msg): nonlocal msgs nonlocal future msgs.append(msg) print('call back recvd') if len(msgs) == 1: future.set_result(True) file_handler_subject = APP_CONFIG.EMAIL_PUBLISH_OPTIONS['subject'] print(f'file_handler_subject:{file_handler_subject}') await subscribe_to_queue(entity_stan, file_handler_subject, f'entity_queue.{file_handler_subject}', f'entity_durable_name.{file_handler_subject}', cb_handler) print(payment.as_dict()) # sanity check assert name_request.id assert payment.nrId == name_request.id # test await furnish_receipt_message(qsm, payment) try: await asyncio.wait_for(future, 1, loop=event_loop) except Exception as err: print(err) # results processed_payment = Payment.find_by_payment_token(PAYMENT_TOKEN) # verify assert processed_payment.furnished assert len(msgs) == 1 cloud_event = json.loads(msgs[0].data.decode('utf-8')) assert cloud_event['identifier'] == NR_NUMBER
async def process_payment(pay_msg: dict, flask_app: Flask): """Render the payment status.""" if not flask_app or not pay_msg: raise QueueException('Flask App or token not available.') with flask_app.app_context(): logger.debug('entering process payment: %s', pay_msg) # capture_message(f'Queue Issue: Unable to find payment.id={payment_id} to place on email queue') # return if pay_msg.get( 'paymentToken', {}).get('statusCode') == PaymentState.TRANSACTION_FAILED.value: # TODO: The customer has cancelled out of paying, so we could note this better # technically the payment for this service is still pending logger.debug('Failed transaction on queue:%s', pay_msg) return complete_payment_status = [ PaymentState.COMPLETED.value, PaymentState.APPROVED.value ] if pay_msg.get('paymentToken', {}).get('statusCode') in complete_payment_status: # pylint: disable=R1702 logger.debug('COMPLETED transaction on queue: %s', pay_msg) if payment_token := pay_msg.get('paymentToken', {}).get('id'): if payment := Payment.find_by_payment_token(payment_token): if update_payment := await update_payment_record(payment): payment = update_payment # record event nr = RequestDAO.find_by_id(payment.nrId) # TODO: create a namex_pay user for this user = User.find_by_username( 'name_request_service_account') EventRecorder.record( user, Event.NAMEX_PAY + f' [payment completed] { payment.payment_action }', nr, nr.json()) # try to update NRO otherwise send a sentry msg for OPS if payment.payment_action in \ [payment.PaymentActions.UPGRADE.value, payment.PaymentActions.REAPPLY.value]: change_flags = { 'is_changed__request': True, 'is_changed__previous_request': False, 'is_changed__applicant': False, 'is_changed__address': False, 'is_changed__name1': False, 'is_changed__name2': False, 'is_changed__name3': False, 'is_changed__nwpta_ab': False, 'is_changed__nwpta_sk': False, 'is_changed__request_state': False, 'is_changed_consent': False } warnings = nro.change_nr(nr, change_flags) if warnings: logger.error( 'Queue Error: Unable to update NRO :%s', warnings) capture_message( f'Queue Error: Unable to update NRO for {nr} {payment.payment_action} :{warnings}', level='error') await furnish_receipt_message(qsm, payment) else: