/**
  * Create a standard pay to address output for usage in {@link #createPaymentRequest} and {@link
  * #createPaymentMessage}.
  *
  * @param amount amount to pay, or null
  * @param address address to pay to
  * @return output
  */
 public static Protos.Output createPayToAddressOutput(@Nullable Coin amount, Address address) {
   Protos.Output.Builder output = Protos.Output.newBuilder();
   if (amount != null) {
     final NetworkParameters params = address.getParameters();
     if (params.hasMaxMoney() && amount.compareTo(params.getMaxMoney()) > 0)
       throw new IllegalArgumentException("Amount too big: " + amount);
     output.setAmount(amount.value);
   } else {
     output.setAmount(0);
   }
   output.setScript(ByteString.copyFrom(ScriptBuilder.createOutputScript(address).getProgram()));
   return output.build();
 }
 @Test
 public void testDefaults() throws Exception {
   Protos.Output.Builder outputBuilder =
       Protos.Output.newBuilder().setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
   Protos.PaymentDetails paymentDetails =
       Protos.PaymentDetails.newBuilder().setTime(time).addOutputs(outputBuilder).build();
   Protos.PaymentRequest paymentRequest =
       Protos.PaymentRequest.newBuilder()
           .setSerializedPaymentDetails(paymentDetails.toByteString())
           .build();
   MockPaymentSession paymentSession = new MockPaymentSession(paymentRequest);
   assertEquals(BigInteger.ZERO, paymentSession.getValue());
   assertNull(paymentSession.getPaymentUrl());
   assertNull(paymentSession.getMemo());
 }
 private Protos.PaymentRequest newSimplePaymentRequest() {
   Protos.Output.Builder outputBuilder =
       Protos.Output.newBuilder()
           .setAmount(nanoCoins.longValue())
           .setScript(ByteString.copyFrom(outputToMe.getScriptBytes()));
   Protos.PaymentDetails paymentDetails =
       Protos.PaymentDetails.newBuilder()
           .setNetwork("test")
           .setTime(time)
           .setPaymentUrl(simplePaymentUrl)
           .addOutputs(outputBuilder)
           .setMemo(paymentRequestMemo)
           .setMerchantData(merchantData)
           .build();
   Protos.PaymentRequest paymentRequest =
       Protos.PaymentRequest.newBuilder()
           .setPaymentDetailsVersion(1)
           .setPkiType("none")
           .setSerializedPaymentDetails(paymentDetails.toByteString())
           .build();
   return paymentRequest;
 }
 /**
  * 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.
  */
 public @Nullable Protos.Payment getPayment(
     List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
     throws IOException {
   if (!paymentDetails.hasPaymentUrl()) return null;
   Protos.Payment.Builder payment = Protos.Payment.newBuilder();
   if (paymentDetails.hasMerchantData()) payment.setMerchantData(paymentDetails.getMerchantData());
   if (refundAddr != null) {
     Protos.Output.Builder refundOutput = Protos.Output.newBuilder();
     refundOutput.setAmount(totalValue.longValue());
     refundOutput.setScript(
         ByteString.copyFrom(ScriptBuilder.createOutputScript(refundAddr).getProgram()));
     payment.addRefundTo(refundOutput);
   }
   if (memo != null) {
     payment.setMemo(memo);
   }
   for (Transaction txn : txns) {
     txn.verify();
     ByteArrayOutputStream o = new ByteArrayOutputStream();
     txn.bitcoinSerialize(o);
     payment.addTransactions(ByteString.copyFrom(o.toByteArray()));
   }
   return payment.build();
 }