def captura(request): token = request.POST['token'] transacao = transaction.find_by_id(token) valor = transacao['authorized_amount'] valor_minimo = 8000 if valor < valor_minimo: return HttpResponseBadRequest( f'Valor {valor} menor que o minimo de {valor_minimo}') transaction.capture(token, {'amount': valor}) return JsonResponse({'token': 'ok'})
def capture(token: str, django_user_id=None) -> PagarmePayment: pagarme_transaction = transaction.find_by_id(token) try: return find_payment_by_transaction(pagarme_transaction['id']) except PagarmePayment.DoesNotExist: pass # payment must be captured payment, all_payments_items = PagarmePayment.from_pagarme_transaction( pagarme_transaction) captured_transaction = transaction.capture(token, {'amount': payment.amount}) if django_user_id is None: try: user = _user_factory(captured_transaction) except ImpossibleUserCreation: pass else: django_user_id = user.id if django_user_id is not None: profile = UserPaymentProfile.from_pagarme_dict(django_user_id, captured_transaction) profile.save() payment.user_id = django_user_id payment.extract_boleto_data(captured_transaction) with django_transaction.atomic(): payment.save() payment.items.set(all_payments_items) notification = PagarmeNotification( status=captured_transaction['status'], payment=payment) notification.save() for listener in _payment_status_changed_listeners: listener(payment_id=payment.id) return payment
def capture(token: str, django_user_id=None) -> PagarmePayment: pagarme_transaction = transaction.find_by_id(token) transaction_id = pagarme_transaction['id'] if str(transaction_id) != token: raise TokenDifferentFromTransactionIdxception(token, transaction_id) try: payment = find_payment_by_transaction(transaction_id) if payment.status() != AUTHORIZED: # only status capturing makes sense return payment except PagarmePayment.DoesNotExist: payment, all_payments_items = PagarmePayment.from_pagarme_transaction(pagarme_transaction) if django_user_id is None: try: user = _user_factory(pagarme_transaction) except ImpossibleUserCreation: pass else: django_user_id = user.id if django_user_id is not None: profile = UserPaymentProfile.from_pagarme_dict(django_user_id, pagarme_transaction) profile.save() payment.user_id = django_user_id with django_transaction.atomic(): payment.save() payment.items.set(all_payments_items) captured_transaction = transaction.capture(token, {'amount': payment.amount}) payment.extract_boleto_data(captured_transaction) payment.save() _save_notification(payment.id, captured_transaction['status']) return payment
def test_capture_transaction(): trx = transaction.create( transaction_dictionary.VALID_CREDIT_CARD_TRANSACTION_CAPTURE_FALSE) capture_transaction = transaction.capture( trx['id'], transaction_dictionary.REFUNDED_OR_CAPTURE_TRANSACTION) assert 'paid' == capture_transaction['status']