Search in sources :

Example 6 with PaymentDatabase

use of org.thoughtcrime.securesms.database.PaymentDatabase in project Signal-Android by signalapp.

the class MessageContentProcessor method handlePayment.

private void handlePayment(@NonNull SignalServiceContent content, @NonNull SignalServiceDataMessage message, @NonNull Recipient senderRecipient) {
    log(content.getTimestamp(), "Payment message.");
    if (!message.getPayment().isPresent()) {
        throw new AssertionError();
    }
    if (!message.getPayment().get().getPaymentNotification().isPresent()) {
        warn(content.getTimestamp(), "Ignoring payment message without notification");
        return;
    }
    SignalServiceDataMessage.PaymentNotification paymentNotification = message.getPayment().get().getPaymentNotification().get();
    PaymentDatabase paymentDatabase = SignalDatabase.payments();
    UUID uuid = UUID.randomUUID();
    String queue = "Payment_" + PushProcessMessageJob.getQueueName(senderRecipient.getId());
    try {
        paymentDatabase.createIncomingPayment(uuid, senderRecipient.getId(), message.getTimestamp(), paymentNotification.getNote(), Money.MobileCoin.ZERO, Money.MobileCoin.ZERO, paymentNotification.getReceipt());
    } catch (PaymentDatabase.PublicKeyConflictException e) {
        warn(content.getTimestamp(), "Ignoring payment with public key already in database");
        return;
    } catch (SerializationException e) {
        warn(content.getTimestamp(), "Ignoring payment with bad data.", e);
    }
    ApplicationDependencies.getJobManager().startChain(new PaymentTransactionCheckJob(uuid, queue)).then(PaymentLedgerUpdateJob.updateLedger()).enqueue();
}
Also used : SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SerializationException(com.mobilecoin.lib.exceptions.SerializationException) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase) UUID(java.util.UUID) PaymentTransactionCheckJob(org.thoughtcrime.securesms.jobs.PaymentTransactionCheckJob)

Example 7 with PaymentDatabase

use of org.thoughtcrime.securesms.database.PaymentDatabase in project Signal-Android by WhisperSystems.

the class PaymentSendJob method onRun.

@Override
protected void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    if (!SignalStore.paymentsValues().mobileCoinPaymentsEnabled()) {
        Log.w(TAG, "Payments are not enabled");
        return;
    }
    Stopwatch stopwatch = new Stopwatch("Payment submission");
    Wallet wallet = ApplicationDependencies.getPayments().getWallet();
    PaymentDatabase paymentDatabase = SignalDatabase.payments();
    paymentDatabase.createOutgoingPayment(uuid, recipientId, publicAddress, timestamp, note, amount);
    Log.i(TAG, "Payment record created " + uuid);
    stopwatch.split("Record created");
    try {
        PaymentSubmissionResult paymentSubmissionResult = wallet.sendPayment(publicAddress, amount.requireMobileCoin(), totalFee.requireMobileCoin());
        stopwatch.split("Payment submitted");
        if (paymentSubmissionResult.containsDefrags()) {
            Log.i(TAG, "Payment contains " + paymentSubmissionResult.defrags().size() + " defrags, main payment" + uuid);
            RecipientId self = Recipient.self().getId();
            MobileCoinPublicAddress selfAddress = wallet.getMobileCoinPublicAddress();
            for (TransactionSubmissionResult defrag : paymentSubmissionResult.defrags()) {
                UUID defragUuid = UUID.randomUUID();
                PaymentTransactionId.MobileCoin mobileCoinTransaction = (PaymentTransactionId.MobileCoin) defrag.getTransactionId();
                paymentDatabase.createDefrag(defragUuid, self, selfAddress, timestamp - 1, mobileCoinTransaction.getFee(), mobileCoinTransaction.getTransaction(), mobileCoinTransaction.getReceipt());
                Log.i(TAG, "Defrag entered with id " + defragUuid);
                ApplicationDependencies.getJobManager().startChain(new PaymentTransactionCheckJob(defragUuid, QUEUE)).then(new MultiDeviceOutgoingPaymentSyncJob(defragUuid)).enqueue();
            }
            stopwatch.split("Defrag");
        }
        TransactionSubmissionResult.ErrorCode errorCode = paymentSubmissionResult.getErrorCode();
        switch(errorCode) {
            case INSUFFICIENT_FUNDS:
                paymentDatabase.markPaymentFailed(uuid, FailureReason.INSUFFICIENT_FUNDS);
                throw new PaymentException("Payment failed due to " + errorCode);
            case GENERIC_FAILURE:
                paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
                throw new PaymentException("Payment failed due to " + errorCode);
            case NETWORK_FAILURE:
                paymentDatabase.markPaymentFailed(uuid, FailureReason.NETWORK);
                throw new PaymentException("Payment failed due to " + errorCode);
            case NONE:
                Log.i(TAG, "Payment submission complete");
                TransactionSubmissionResult transactionSubmissionResult = Objects.requireNonNull(paymentSubmissionResult.getNonDefrag());
                PaymentTransactionId.MobileCoin mobileCoinTransaction = (PaymentTransactionId.MobileCoin) transactionSubmissionResult.getTransactionId();
                paymentDatabase.markPaymentSubmitted(uuid, mobileCoinTransaction.getTransaction(), mobileCoinTransaction.getReceipt(), mobileCoinTransaction.getFee());
                Log.i(TAG, "Payment record updated " + uuid);
                break;
        }
    } catch (Exception e) {
        Log.w(TAG, "Unknown payment failure", e);
        paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
        throw e;
    }
    stopwatch.split("Update database record");
    stopwatch.stop(TAG);
}
Also used : PaymentTransactionId(org.thoughtcrime.securesms.payments.PaymentTransactionId) PaymentSubmissionResult(org.thoughtcrime.securesms.payments.PaymentSubmissionResult) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Wallet(org.thoughtcrime.securesms.payments.Wallet) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) TransactionSubmissionResult(org.thoughtcrime.securesms.payments.TransactionSubmissionResult) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase) UUID(java.util.UUID) MobileCoinPublicAddress(org.thoughtcrime.securesms.payments.MobileCoinPublicAddress)

Example 8 with PaymentDatabase

use of org.thoughtcrime.securesms.database.PaymentDatabase in project Signal-Android by signalapp.

the class PaymentSendJob method onRun.

@Override
protected void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    if (!SignalStore.paymentsValues().mobileCoinPaymentsEnabled()) {
        Log.w(TAG, "Payments are not enabled");
        return;
    }
    Stopwatch stopwatch = new Stopwatch("Payment submission");
    Wallet wallet = ApplicationDependencies.getPayments().getWallet();
    PaymentDatabase paymentDatabase = SignalDatabase.payments();
    paymentDatabase.createOutgoingPayment(uuid, recipientId, publicAddress, timestamp, note, amount);
    Log.i(TAG, "Payment record created " + uuid);
    stopwatch.split("Record created");
    try {
        PaymentSubmissionResult paymentSubmissionResult = wallet.sendPayment(publicAddress, amount.requireMobileCoin(), totalFee.requireMobileCoin());
        stopwatch.split("Payment submitted");
        if (paymentSubmissionResult.containsDefrags()) {
            Log.i(TAG, "Payment contains " + paymentSubmissionResult.defrags().size() + " defrags, main payment" + uuid);
            RecipientId self = Recipient.self().getId();
            MobileCoinPublicAddress selfAddress = wallet.getMobileCoinPublicAddress();
            for (TransactionSubmissionResult defrag : paymentSubmissionResult.defrags()) {
                UUID defragUuid = UUID.randomUUID();
                PaymentTransactionId.MobileCoin mobileCoinTransaction = (PaymentTransactionId.MobileCoin) defrag.getTransactionId();
                paymentDatabase.createDefrag(defragUuid, self, selfAddress, timestamp - 1, mobileCoinTransaction.getFee(), mobileCoinTransaction.getTransaction(), mobileCoinTransaction.getReceipt());
                Log.i(TAG, "Defrag entered with id " + defragUuid);
                ApplicationDependencies.getJobManager().startChain(new PaymentTransactionCheckJob(defragUuid, QUEUE)).then(new MultiDeviceOutgoingPaymentSyncJob(defragUuid)).enqueue();
            }
            stopwatch.split("Defrag");
        }
        TransactionSubmissionResult.ErrorCode errorCode = paymentSubmissionResult.getErrorCode();
        switch(errorCode) {
            case INSUFFICIENT_FUNDS:
                paymentDatabase.markPaymentFailed(uuid, FailureReason.INSUFFICIENT_FUNDS);
                throw new PaymentException("Payment failed due to " + errorCode);
            case GENERIC_FAILURE:
                paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
                throw new PaymentException("Payment failed due to " + errorCode);
            case NETWORK_FAILURE:
                paymentDatabase.markPaymentFailed(uuid, FailureReason.NETWORK);
                throw new PaymentException("Payment failed due to " + errorCode);
            case NONE:
                Log.i(TAG, "Payment submission complete");
                TransactionSubmissionResult transactionSubmissionResult = Objects.requireNonNull(paymentSubmissionResult.getNonDefrag());
                PaymentTransactionId.MobileCoin mobileCoinTransaction = (PaymentTransactionId.MobileCoin) transactionSubmissionResult.getTransactionId();
                paymentDatabase.markPaymentSubmitted(uuid, mobileCoinTransaction.getTransaction(), mobileCoinTransaction.getReceipt(), mobileCoinTransaction.getFee());
                Log.i(TAG, "Payment record updated " + uuid);
                break;
        }
    } catch (Exception e) {
        Log.w(TAG, "Unknown payment failure", e);
        paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
        throw e;
    }
    stopwatch.split("Update database record");
    stopwatch.stop(TAG);
}
Also used : PaymentTransactionId(org.thoughtcrime.securesms.payments.PaymentTransactionId) PaymentSubmissionResult(org.thoughtcrime.securesms.payments.PaymentSubmissionResult) RecipientId(org.thoughtcrime.securesms.recipients.RecipientId) Wallet(org.thoughtcrime.securesms.payments.Wallet) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) Stopwatch(org.thoughtcrime.securesms.util.Stopwatch) NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) TransactionSubmissionResult(org.thoughtcrime.securesms.payments.TransactionSubmissionResult) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase) UUID(java.util.UUID) MobileCoinPublicAddress(org.thoughtcrime.securesms.payments.MobileCoinPublicAddress)

Aggregations

PaymentDatabase (org.thoughtcrime.securesms.database.PaymentDatabase)8 UUID (java.util.UUID)4 NotPushRegisteredException (org.thoughtcrime.securesms.net.NotPushRegisteredException)4 PaymentTransactionId (org.thoughtcrime.securesms.payments.PaymentTransactionId)4 SignalServiceDataMessage (org.whispersystems.signalservice.api.messages.SignalServiceDataMessage)4 SerializationException (com.mobilecoin.lib.exceptions.SerializationException)2 PaymentTransactionCheckJob (org.thoughtcrime.securesms.jobs.PaymentTransactionCheckJob)2 MobileCoinPublicAddress (org.thoughtcrime.securesms.payments.MobileCoinPublicAddress)2 PaymentSubmissionResult (org.thoughtcrime.securesms.payments.PaymentSubmissionResult)2 Payments (org.thoughtcrime.securesms.payments.Payments)2 TransactionSubmissionResult (org.thoughtcrime.securesms.payments.TransactionSubmissionResult)2 Wallet (org.thoughtcrime.securesms.payments.Wallet)2 Recipient (org.thoughtcrime.securesms.recipients.Recipient)2 RecipientId (org.thoughtcrime.securesms.recipients.RecipientId)2 RetryLaterException (org.thoughtcrime.securesms.transport.RetryLaterException)2 Stopwatch (org.thoughtcrime.securesms.util.Stopwatch)2 SignalServiceMessageSender (org.whispersystems.signalservice.api.SignalServiceMessageSender)2 UnidentifiedAccessPair (org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair)2 SendMessageResult (org.whispersystems.signalservice.api.messages.SendMessageResult)2 SignalServiceAddress (org.whispersystems.signalservice.api.push.SignalServiceAddress)2