Java Code Examples for org.bitcoin.protocols.payments.Protos#Payment

The following examples show how to use org.bitcoin.protocols.payments.Protos#Payment . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Generates a Payment message based on the information in the PaymentRequest.
 * Provide transactions built by the wallet.
 * If the PaymentRequest did not specify a payment_url, returns null.
 * @param txns list of transactions to be included with the Payment message.
 * @param refundAddr will be used by the merchant to send money back if there was a problem.
 * @param memo is a message to include in the payment message sent to the merchant.
 */
@Nullable
public Protos.Payment getPayment(List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
        throws IOException, PaymentProtocolException.InvalidNetwork {
    if (paymentDetails.hasPaymentUrl()) {
        for (Transaction tx : txns) {
            // BIP70 doesn't allow for regtest in its network type. If we mismatch,
            // treat regtest transactions for a testnet payment request as a match.
            if (!tx.getParams().equals(params) &&
                (!tx.getParams().equals(RegTestParams.get()) || !params.equals(TestNet3Params.get())))
                throw new PaymentProtocolException.InvalidNetwork(params.getPaymentProtocolId());
        }
        return PaymentProtocol.createPaymentMessage(txns, totalValue, refundAddr, memo, getMerchantData());
    } else {
        return null;
    }
}
 
Example 2
Source File: PaymentSession.java    From bcm-android with GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    return executor.submit(new Callable<PaymentProtocol.Ack>() {
        @Override
        public PaymentProtocol.Ack call() throws Exception {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", PaymentProtocol.MIMETYPE_PAYMENT);
            connection.setRequestProperty("Accept", PaymentProtocol.MIMETYPE_PAYMENTACK);
            connection.setRequestProperty("Content-Length", Integer.toString(payment.getSerializedSize()));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request.
            DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
            payment.writeTo(outStream);
            outStream.flush();
            outStream.close();

            // Get response.
            Protos.PaymentACK paymentAck = Protos.PaymentACK.parseFrom(connection.getInputStream());
            return PaymentProtocol.parsePaymentAck(paymentAck);
        }
    });
}
 
Example 3
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment message. This wraps up transaction data along with anything else useful for making a payment.
 * 
 * @param transactions transactions to include with the payment message
 * @param refundOutputs list of outputs to refund coins to, or null
 * @param memo arbitrary, user readable memo, or null if none
 * @param merchantData arbitrary merchant data, or null if none
 * @return created payment message
 */
public static Protos.Payment createPaymentMessage(List<Transaction> transactions,
        @Nullable List<Protos.Output> refundOutputs, @Nullable String memo, @Nullable byte[] merchantData) {
    Protos.Payment.Builder builder = Protos.Payment.newBuilder();
    for (Transaction transaction : transactions) {
        transaction.verify();
        builder.addTransactions(ByteString.copyFrom(transaction.unsafeBitcoinSerialize()));
    }
    if (refundOutputs != null) {
        for (Protos.Output output : refundOutputs)
            builder.addRefundTo(output);
    }
    if (memo != null)
        builder.setMemo(memo);
    if (merchantData != null)
        builder.setMerchantData(ByteString.copyFrom(merchantData));
    return builder.build();
}
 
Example 4
Source File: PaymentProtocol.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Create a payment message. This wraps up transaction data along with anything else useful for making a payment.
 * 
 * @param transactions transactions to include with the payment message
 * @param refundOutputs list of outputs to refund coins to, or null
 * @param memo arbitrary, user readable memo, or null if none
 * @param merchantData arbitrary merchant data, or null if none
 * @return created payment message
 */
public static Protos.Payment createPaymentMessage(List<Transaction> transactions,
        @Nullable List<Protos.Output> refundOutputs, @Nullable String memo, @Nullable byte[] merchantData) {
    Protos.Payment.Builder builder = Protos.Payment.newBuilder();
    for (Transaction transaction : transactions) {
        transaction.verify();
        builder.addTransactions(ByteString.copyFrom(transaction.unsafeBitcoinSerialize()));
    }
    if (refundOutputs != null) {
        for (Protos.Output output : refundOutputs)
            builder.addRefundTo(output);
    }
    if (memo != null)
        builder.setMemo(memo);
    if (merchantData != null)
        builder.setMerchantData(ByteString.copyFrom(merchantData));
    return builder.build();
}
 
Example 5
Source File: PaymentSession.java    From GreenBits with GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    return executor.submit(new Callable<PaymentProtocol.Ack>() {
        @Override
        public PaymentProtocol.Ack call() throws Exception {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", PaymentProtocol.MIMETYPE_PAYMENT);
            connection.setRequestProperty("Accept", PaymentProtocol.MIMETYPE_PAYMENTACK);
            connection.setRequestProperty("Content-Length", Integer.toString(payment.getSerializedSize()));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request.
            DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
            payment.writeTo(outStream);
            outStream.flush();
            outStream.close();

            // Get response.
            Protos.PaymentACK paymentAck = Protos.PaymentACK.parseFrom(connection.getInputStream());
            return PaymentProtocol.parsePaymentAck(paymentAck);
        }
    });
}
 
Example 6
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
@VisibleForTesting
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    return executor.submit(new Callable<PaymentProtocol.Ack>() {
        @Override
        public PaymentProtocol.Ack call() throws Exception {
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", PaymentProtocol.MIMETYPE_PAYMENT);
            connection.setRequestProperty("Accept", PaymentProtocol.MIMETYPE_PAYMENTACK);
            connection.setRequestProperty("Content-Length", Integer.toString(payment.getSerializedSize()));
            connection.setUseCaches(false);
            connection.setDoInput(true);
            connection.setDoOutput(true);

            // Send request.
            DataOutputStream outStream = new DataOutputStream(connection.getOutputStream());
            payment.writeTo(outStream);
            outStream.flush();
            outStream.close();

            // Get response.
            Protos.PaymentACK paymentAck = Protos.PaymentACK.parseFrom(connection.getInputStream());
            return PaymentProtocol.parsePaymentAck(paymentAck);
        }
    });
}
 
Example 7
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Generates a Payment message based on the information in the PaymentRequest.
 * Provide transactions built by the wallet.
 * If the PaymentRequest did not specify a payment_url, returns null.
 * @param txns list of transactions to be included with the Payment message.
 * @param refundAddr will be used by the merchant to send money back if there was a problem.
 * @param memo is a message to include in the payment message sent to the merchant.
 */
@Nullable
public Protos.Payment getPayment(List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
        throws IOException, PaymentProtocolException.InvalidNetwork {
    if (paymentDetails.hasPaymentUrl()) {
        for (Transaction tx : txns) {
            // BIP70 doesn't allow for regtest in its network type. If we mismatch,
            // treat regtest transactions for a testnet payment request as a match.
            if (!tx.getParams().equals(params) &&
                (!tx.getParams().equals(RegTestParams.get()) || !params.equals(TestNet3Params.get())))
                throw new PaymentProtocolException.InvalidNetwork(params.getPaymentProtocolId());
        }
        return PaymentProtocol.createPaymentMessage(txns, totalValue, refundAddr, memo, getMerchantData());
    } else {
        return null;
    }
}
 
Example 8
Source File: PaymentSession.java    From green_android with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Generates a Payment message and sends the payment to the merchant who sent the PaymentRequest.
 * Provide transactions built by the wallet.
 * NOTE: This does not broadcast the transactions to the bitcoin network, it merely sends a Payment message to the
 * merchant confirming the payment.
 * Returns an object wrapping PaymentACK once received.
 * If the PaymentRequest did not specify a payment_url, returns null and does nothing.
 * @param txns list of transactions to be included with the Payment message.
 * @param refundAddr will be used by the merchant to send money back if there was a problem.
 * @param memo is a message to include in the payment message sent to the merchant.
 */
@Nullable
public ListenableFuture<PaymentProtocol.Ack> sendPayment(List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
        throws PaymentProtocolException, VerificationException, IOException {
    Protos.Payment payment = getPayment(txns, refundAddr, memo);
    if (payment == null)
        return null;
    if (isExpired())
        throw new PaymentProtocolException.Expired("PaymentRequest is expired");
    URL url;
    try {
        url = new URL(paymentDetails.getPaymentUrl());
    } catch (MalformedURLException e) {
        throw new PaymentProtocolException.InvalidPaymentURL(e);
    }
    return sendPayment(url, payment);
}
 
Example 9
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse transactions from payment message.
 * 
 * @param params network parameters (needed for transaction deserialization)
 * @param paymentMessage payment message to parse
 * @return list of transactions
 */
public static List<Transaction> parseTransactionsFromPaymentMessage(NetworkParameters params,
        Protos.Payment paymentMessage) {
    final List<Transaction> transactions = new ArrayList<>(paymentMessage.getTransactionsCount());
    for (final ByteString transaction : paymentMessage.getTransactionsList())
        transactions.add(params.getDefaultSerializer().makeTransaction(transaction.toByteArray()));
    return transactions;
}
 
Example 10
Source File: PaymentProtocol.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a payment ack.
 * 
 * @param paymentMessage payment message to send with the ack
 * @param memo arbitrary, user readable memo, or null if none
 * @return created payment ack
 */
public static Protos.PaymentACK createPaymentAck(Protos.Payment paymentMessage, @Nullable String memo) {
    final Protos.PaymentACK.Builder builder = Protos.PaymentACK.newBuilder();
    builder.setPayment(paymentMessage);
    if (memo != null)
        builder.setMemo(memo);
    return builder.build();
}
 
Example 11
Source File: PaymentSessionTest.java    From green_android with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testSimplePayment() throws Exception {
    // Create a PaymentRequest and make sure the correct values are parsed by the PaymentSession.
    MockPaymentSession paymentSession = new MockPaymentSession(newSimplePaymentRequest("test"));
    assertEquals(paymentRequestMemo, paymentSession.getMemo());
    assertEquals(coin, paymentSession.getValue());
    assertEquals(simplePaymentUrl, paymentSession.getPaymentUrl());
    assertTrue(new Date(time * 1000L).equals(paymentSession.getDate()));
    assertTrue(paymentSession.getSendRequest().tx.equals(tx));
    assertFalse(paymentSession.isExpired());

    // Send the payment and verify that the correct information is sent.
    // Add a dummy input to tx so it is considered valid.
    tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes()));
    ArrayList<Transaction> txns = new ArrayList<>();
    txns.add(tx);
    Address refundAddr = new Address(PARAMS, serverKey.getPubKeyHash());
    paymentSession.sendPayment(txns, refundAddr, paymentMemo);
    assertEquals(1, paymentSession.getPaymentLog().size());
    assertEquals(simplePaymentUrl, paymentSession.getPaymentLog().get(0).getUrl().toString());
    Protos.Payment payment = paymentSession.getPaymentLog().get(0).getPayment();
    assertEquals(paymentMemo, payment.getMemo());
    assertEquals(merchantData, payment.getMerchantData());
    assertEquals(1, payment.getRefundToCount());
    assertEquals(coin.value, payment.getRefundTo(0).getAmount());
    TransactionOutput refundOutput = new TransactionOutput(PARAMS, null, coin, refundAddr);
    ByteString refundScript = ByteString.copyFrom(refundOutput.getScriptBytes());
    assertTrue(refundScript.equals(payment.getRefundTo(0).getScript()));
}
 
Example 12
Source File: PaymentSessionTest.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
@Test
public void testSimplePayment() throws Exception {
    // Create a PaymentRequest and make sure the correct values are parsed by the PaymentSession.
    MockPaymentSession paymentSession = new MockPaymentSession(newSimplePaymentRequest("test"));
    assertEquals(paymentRequestMemo, paymentSession.getMemo());
    assertEquals(coin, paymentSession.getValue());
    assertEquals(simplePaymentUrl, paymentSession.getPaymentUrl());
    assertTrue(new Date(time * 1000L).equals(paymentSession.getDate()));
    assertTrue(paymentSession.getSendRequest().tx.equals(tx));
    assertFalse(paymentSession.isExpired());

    // Send the payment and verify that the correct information is sent.
    // Add a dummy input to tx so it is considered valid.
    tx.addInput(new TransactionInput(PARAMS, tx, outputToMe.getScriptBytes()));
    ArrayList<Transaction> txns = new ArrayList<>();
    txns.add(tx);
    Address refundAddr = new Address(PARAMS, serverKey.getPubKeyHash());
    paymentSession.sendPayment(txns, refundAddr, paymentMemo);
    assertEquals(1, paymentSession.getPaymentLog().size());
    assertEquals(simplePaymentUrl, paymentSession.getPaymentLog().get(0).getUrl().toString());
    Protos.Payment payment = paymentSession.getPaymentLog().get(0).getPayment();
    assertEquals(paymentMemo, payment.getMemo());
    assertEquals(merchantData, payment.getMerchantData());
    assertEquals(1, payment.getRefundToCount());
    assertEquals(coin.value, payment.getRefundTo(0).getAmount());
    TransactionOutput refundOutput = new TransactionOutput(PARAMS, null, coin, refundAddr);
    ByteString refundScript = ByteString.copyFrom(refundOutput.getScriptBytes());
    assertTrue(refundScript.equals(payment.getRefundTo(0).getScript()));
}
 
Example 13
Source File: PaymentProtocol.java    From GreenBits with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Parse transactions from payment message.
 * 
 * @param params network parameters (needed for transaction deserialization)
 * @param paymentMessage payment message to parse
 * @return list of transactions
 */
public static List<Transaction> parseTransactionsFromPaymentMessage(NetworkParameters params,
        Protos.Payment paymentMessage) {
    final List<Transaction> transactions = new ArrayList<>(paymentMessage.getTransactionsCount());
    for (final ByteString transaction : paymentMessage.getTransactionsList())
        transactions.add(params.getDefaultSerializer().makeTransaction(transaction.toByteArray()));
    return transactions;
}
 
Example 14
Source File: PaymentProtocol.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a payment ack.
 *
 * @param paymentMessage payment message to send with the ack
 * @param memo           arbitrary, user readable memo, or null if none
 * @return created payment ack
 */
public static Protos.PaymentACK createPaymentAck(Protos.Payment paymentMessage, @Nullable String memo) {
    final Protos.PaymentACK.Builder builder = Protos.PaymentACK.newBuilder();
    builder.setPayment(paymentMessage);
    if (memo != null)
        builder.setMemo(memo);
    return builder.build();
}
 
Example 15
Source File: PaymentProtocol.java    From bcm-android with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Create a payment message with one standard pay to address output.
 *
 * @param transactions  one or more transactions that satisfy the requested outputs.
 * @param refundAmount  amount of coins to request as a refund, or null if no refund.
 * @param refundAddress address to refund coins to
 * @param memo          arbitrary, user readable memo, or null if none
 * @param merchantData  arbitrary merchant data, or null if none
 * @return created payment message
 */
public static Protos.Payment createPaymentMessage(List<Transaction> transactions,
                                                  @Nullable Coin refundAmount, @Nullable Address refundAddress, @Nullable String memo,
                                                  @Nullable byte[] merchantData) {
    if (refundAddress != null) {
        if (refundAmount == null)
            throw new IllegalArgumentException("Specify refund amount if refund address is specified.");
        return createPaymentMessage(transactions,
                ImmutableList.of(createPayToAddressOutput(refundAmount, refundAddress)), memo, merchantData);
    } else {
        return createPaymentMessage(transactions, null, memo, merchantData);
    }
}
 
Example 16
Source File: PaymentSessionTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    paymentLog.add(new PaymentLogItem(url, payment));
    return null;
}
 
Example 17
Source File: PaymentSessionTest.java    From green_android with GNU General Public License v3.0 4 votes vote down vote up
@Override
protected ListenableFuture<PaymentProtocol.Ack> sendPayment(final URL url, final Protos.Payment payment) {
    paymentLog.add(new PaymentLogItem(url, payment));
    return null;
}
 
Example 18
Source File: PaymentSessionTest.java    From bcm-android with GNU General Public License v3.0 4 votes vote down vote up
public Protos.Payment getPayment() {
    return payment;
}
 
Example 19
Source File: PaymentSessionTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
public Protos.Payment getPayment() {
    return payment;
}
 
Example 20
Source File: PaymentSessionTest.java    From GreenBits with GNU General Public License v3.0 4 votes vote down vote up
PaymentLogItem(final URL url, final Protos.Payment payment) {
    this.url = url;
    this.payment = payment;
}