Search in sources :

Example 1 with PaymentDatabase

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

the class PaymentNotificationSendJob method onRun.

@Override
protected void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    PaymentDatabase paymentDatabase = SignalDatabase.payments();
    Recipient recipient = Recipient.resolved(recipientId);
    if (recipient.isUnregistered()) {
        Log.w(TAG, recipientId + " not registered!");
        return;
    }
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, recipient);
    Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient);
    PaymentDatabase.PaymentTransaction payment = paymentDatabase.getPayment(uuid);
    if (payment == null) {
        Log.w(TAG, "Could not find payment, cannot send notification " + uuid);
        return;
    }
    if (payment.getReceipt() == null) {
        Log.w(TAG, "Could not find payment receipt, cannot send notification " + uuid);
        return;
    }
    SignalServiceDataMessage dataMessage = SignalServiceDataMessage.newBuilder().withPayment(new SignalServiceDataMessage.Payment(new SignalServiceDataMessage.PaymentNotification(payment.getReceipt(), payment.getNote()))).build();
    SendMessageResult sendMessageResult = messageSender.sendDataMessage(address, unidentifiedAccess, ContentHint.DEFAULT, dataMessage, IndividualSendEvents.EMPTY);
    if (sendMessageResult.getIdentityFailure() != null) {
        Log.w(TAG, "Identity failure for " + recipient.getId());
    } else if (sendMessageResult.isUnregisteredFailure()) {
        Log.w(TAG, "Unregistered failure for " + recipient.getId());
    } else if (sendMessageResult.getSuccess() == null) {
        throw new RetryLaterException();
    } else {
        Log.i(TAG, String.format("Payment notification sent to %s for %s", recipientId, uuid));
    }
}
Also used : NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase)

Example 2 with PaymentDatabase

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

the class PaymentTransactionCheckJob method onRun.

@Override
protected void onRun() throws Exception {
    PaymentDatabase paymentDatabase = SignalDatabase.payments();
    PaymentDatabase.PaymentTransaction payment = paymentDatabase.getPayment(uuid);
    if (payment == null) {
        Log.w(TAG, "No payment found for UUID " + uuid);
        return;
    }
    Payments payments = ApplicationDependencies.getPayments();
    switch(payment.getDirection()) {
        case SENT:
            {
                Log.i(TAG, "Checking sent status of " + uuid);
                PaymentTransactionId paymentTransactionId = new PaymentTransactionId.MobileCoin(Objects.requireNonNull(payment.getTransaction()), Objects.requireNonNull(payment.getReceipt()), payment.getFee().requireMobileCoin());
                Wallet.TransactionStatusResult status = payments.getWallet().getSentTransactionStatus(paymentTransactionId);
                switch(status.getTransactionStatus()) {
                    case COMPLETE:
                        paymentDatabase.markPaymentSuccessful(uuid, status.getBlockIndex());
                        Log.i(TAG, "Marked sent payment successful " + uuid);
                        break;
                    case FAILED:
                        paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
                        Log.i(TAG, "Marked sent payment failed " + uuid);
                        break;
                    case IN_PROGRESS:
                        Log.i(TAG, "Sent payment still in progress " + uuid);
                        throw new IncompleteTransactionException();
                    default:
                        throw new AssertionError();
                }
                break;
            }
        case RECEIVED:
            {
                Log.i(TAG, "Checking received status of " + uuid);
                Wallet.ReceivedTransactionStatus transactionStatus = payments.getWallet().getReceivedTransactionStatus(Objects.requireNonNull(payment.getReceipt()));
                switch(transactionStatus.getStatus()) {
                    case COMPLETE:
                        paymentDatabase.markReceivedPaymentSuccessful(uuid, transactionStatus.getAmount(), transactionStatus.getBlockIndex());
                        Log.i(TAG, "Marked received payment successful " + uuid);
                        break;
                    case FAILED:
                        paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
                        Log.i(TAG, "Marked received payment failed " + uuid);
                        break;
                    case IN_PROGRESS:
                        Log.i(TAG, "Received payment still in progress " + uuid);
                        throw new IncompleteTransactionException();
                    default:
                        throw new AssertionError();
                }
                break;
            }
        default:
            {
                throw new AssertionError();
            }
    }
}
Also used : PaymentTransactionId(org.thoughtcrime.securesms.payments.PaymentTransactionId) Payments(org.thoughtcrime.securesms.payments.Payments) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase)

Example 3 with PaymentDatabase

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

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 4 with PaymentDatabase

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

the class PaymentNotificationSendJob method onRun.

@Override
protected void onRun() throws Exception {
    if (!Recipient.self().isRegistered()) {
        throw new NotPushRegisteredException();
    }
    PaymentDatabase paymentDatabase = SignalDatabase.payments();
    Recipient recipient = Recipient.resolved(recipientId);
    if (recipient.isUnregistered()) {
        Log.w(TAG, recipientId + " not registered!");
        return;
    }
    SignalServiceMessageSender messageSender = ApplicationDependencies.getSignalServiceMessageSender();
    SignalServiceAddress address = RecipientUtil.toSignalServiceAddress(context, recipient);
    Optional<UnidentifiedAccessPair> unidentifiedAccess = UnidentifiedAccessUtil.getAccessFor(context, recipient);
    PaymentDatabase.PaymentTransaction payment = paymentDatabase.getPayment(uuid);
    if (payment == null) {
        Log.w(TAG, "Could not find payment, cannot send notification " + uuid);
        return;
    }
    if (payment.getReceipt() == null) {
        Log.w(TAG, "Could not find payment receipt, cannot send notification " + uuid);
        return;
    }
    SignalServiceDataMessage dataMessage = SignalServiceDataMessage.newBuilder().withPayment(new SignalServiceDataMessage.Payment(new SignalServiceDataMessage.PaymentNotification(payment.getReceipt(), payment.getNote()))).build();
    SendMessageResult sendMessageResult = messageSender.sendDataMessage(address, unidentifiedAccess, ContentHint.DEFAULT, dataMessage, IndividualSendEvents.EMPTY);
    if (sendMessageResult.getIdentityFailure() != null) {
        Log.w(TAG, "Identity failure for " + recipient.getId());
    } else if (sendMessageResult.isUnregisteredFailure()) {
        Log.w(TAG, "Unregistered failure for " + recipient.getId());
    } else if (sendMessageResult.getSuccess() == null) {
        throw new RetryLaterException();
    } else {
        Log.i(TAG, String.format("Payment notification sent to %s for %s", recipientId, uuid));
    }
}
Also used : NotPushRegisteredException(org.thoughtcrime.securesms.net.NotPushRegisteredException) SignalServiceMessageSender(org.whispersystems.signalservice.api.SignalServiceMessageSender) Recipient(org.thoughtcrime.securesms.recipients.Recipient) UnidentifiedAccessPair(org.whispersystems.signalservice.api.crypto.UnidentifiedAccessPair) SendMessageResult(org.whispersystems.signalservice.api.messages.SendMessageResult) SignalServiceDataMessage(org.whispersystems.signalservice.api.messages.SignalServiceDataMessage) SignalServiceAddress(org.whispersystems.signalservice.api.push.SignalServiceAddress) RetryLaterException(org.thoughtcrime.securesms.transport.RetryLaterException) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase)

Example 5 with PaymentDatabase

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

the class PaymentTransactionCheckJob method onRun.

@Override
protected void onRun() throws Exception {
    PaymentDatabase paymentDatabase = SignalDatabase.payments();
    PaymentDatabase.PaymentTransaction payment = paymentDatabase.getPayment(uuid);
    if (payment == null) {
        Log.w(TAG, "No payment found for UUID " + uuid);
        return;
    }
    Payments payments = ApplicationDependencies.getPayments();
    switch(payment.getDirection()) {
        case SENT:
            {
                Log.i(TAG, "Checking sent status of " + uuid);
                PaymentTransactionId paymentTransactionId = new PaymentTransactionId.MobileCoin(Objects.requireNonNull(payment.getTransaction()), Objects.requireNonNull(payment.getReceipt()), payment.getFee().requireMobileCoin());
                Wallet.TransactionStatusResult status = payments.getWallet().getSentTransactionStatus(paymentTransactionId);
                switch(status.getTransactionStatus()) {
                    case COMPLETE:
                        paymentDatabase.markPaymentSuccessful(uuid, status.getBlockIndex());
                        Log.i(TAG, "Marked sent payment successful " + uuid);
                        break;
                    case FAILED:
                        paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
                        Log.i(TAG, "Marked sent payment failed " + uuid);
                        break;
                    case IN_PROGRESS:
                        Log.i(TAG, "Sent payment still in progress " + uuid);
                        throw new IncompleteTransactionException();
                    default:
                        throw new AssertionError();
                }
                break;
            }
        case RECEIVED:
            {
                Log.i(TAG, "Checking received status of " + uuid);
                Wallet.ReceivedTransactionStatus transactionStatus = payments.getWallet().getReceivedTransactionStatus(Objects.requireNonNull(payment.getReceipt()));
                switch(transactionStatus.getStatus()) {
                    case COMPLETE:
                        paymentDatabase.markReceivedPaymentSuccessful(uuid, transactionStatus.getAmount(), transactionStatus.getBlockIndex());
                        Log.i(TAG, "Marked received payment successful " + uuid);
                        break;
                    case FAILED:
                        paymentDatabase.markPaymentFailed(uuid, FailureReason.UNKNOWN);
                        Log.i(TAG, "Marked received payment failed " + uuid);
                        break;
                    case IN_PROGRESS:
                        Log.i(TAG, "Received payment still in progress " + uuid);
                        throw new IncompleteTransactionException();
                    default:
                        throw new AssertionError();
                }
                break;
            }
        default:
            {
                throw new AssertionError();
            }
    }
}
Also used : PaymentTransactionId(org.thoughtcrime.securesms.payments.PaymentTransactionId) Payments(org.thoughtcrime.securesms.payments.Payments) PaymentDatabase(org.thoughtcrime.securesms.database.PaymentDatabase)

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