public void onButtonApplyClicked(View view) {
    if (printOrder.getPromoCode() != null) {
      // Clear promo code
      printOrder.clearPromoCode();
      updateViewsBasedOnPromoCodeChange();
    } else {
      // Apply promo code
      final ProgressDialog dialog = new ProgressDialog(this);
      dialog.setCancelable(false);
      dialog.setTitle("Processing");
      dialog.setMessage("Checking Code...");
      dialog.show();

      String promoCode = ((EditText) findViewById(R.id.edit_text_promo_code)).getText().toString();
      printOrder.applyPromoCode(
          promoCode,
          new ApplyPromoCodeListener() {
            @Override
            public void onPromoCodeApplied(PrintOrder order, BigDecimal discount) {
              dialog.dismiss();
              Toast.makeText(PaymentActivity.this, "Discount applied!", Toast.LENGTH_LONG).show();
              updateViewsBasedOnPromoCodeChange();
            }

            @Override
            public void onError(PrintOrder order, Exception ex) {
              dialog.dismiss();
              showErrorDialog(ex.getMessage());
            }
          });
    }
  }
  private void updateViewsBasedOnPromoCodeChange() {
    Button applyButton = (Button) findViewById(R.id.button_apply_promo);
    EditText promoText = (EditText) findViewById(R.id.edit_text_promo_code);
    if (printOrder.getPromoCode() != null) {
      promoText.setText(printOrder.getPromoCode());
      promoText.setEnabled(false);
      applyButton.setText("Clear");
    } else {
      promoText.setText("");
      promoText.setEnabled(true);
      applyButton.setText("Apply");
    }

    Button payWithCreditCardButton = (Button) findViewById(R.id.button_pay_with_credit_card);
    if (printOrder.getCost(printOrder.getCurrencyCode()).compareTo(BigDecimal.ZERO) <= 0) {
      findViewById(R.id.button_pay_with_paypal).setVisibility(View.GONE);
      payWithCreditCardButton.setText("Checkout for Free!");
      payWithCreditCardButton.setOnClickListener(
          new View.OnClickListener() {
            @Override
            public void onClick(View view) {
              submitOrderForPrinting(null);
            }
          });

    } else {
      findViewById(R.id.button_pay_with_paypal).setVisibility(View.VISIBLE);
      payWithCreditCardButton.setText("Pay with Credit Card");
    }
  }
  private void payWithExistingCard(PayPalCard card) {
    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setCancelable(false);
    dialog.setTitle("Processing");
    dialog.setMessage("One moment");
    dialog.show();

    card.chargeCard(
        paypalEnvironment,
        printOrder.getCost(printOrder.getCurrencyCode()),
        getPayPalCurrency(printOrder.getCurrencyCode()),
        "",
        new PayPalCardChargeListener() {
          @Override
          public void onChargeSuccess(PayPalCard card, String proofOfPayment) {
            dialog.dismiss();
            submitOrderForPrinting(proofOfPayment);
            card.saveAsLastUsedCard(PaymentActivity.this);
          }

          @Override
          public void onError(PayPalCard card, Exception ex) {
            dialog.dismiss();
            showErrorDialog(ex.getMessage());
          }
        });
  }
 public void onButtonPayWithPayPalClicked(View view) {
   PayPalPayment payment =
       new PayPalPayment(
           printOrder.getCost(printOrder.getCurrencyCode()),
           printOrder.getCurrencyCode(),
           "Product");
   Intent intent = new Intent(this, com.paypal.android.sdk.payments.PaymentActivity.class);
   intent.putExtra(
       com.paypal.android.sdk.payments.PaymentActivity.EXTRA_PAYPAL_ENVIRONMENT,
       printEnvironment.getPayPalEnvironment());
   intent.putExtra(
       com.paypal.android.sdk.payments.PaymentActivity.EXTRA_CLIENT_ID,
       printEnvironment.getPayPalClientId());
   intent.putExtra(
       com.paypal.android.sdk.payments.PaymentActivity.EXTRA_PAYER_ID,
       "<*****@*****.**>");
   intent.putExtra(
       com.paypal.android.sdk.payments.PaymentActivity.EXTRA_RECEIVER_EMAIL,
       printEnvironment.getPayPalReceiverEmail());
   intent.putExtra(com.paypal.android.sdk.payments.PaymentActivity.EXTRA_PAYMENT, payment);
   intent.putExtra(com.paypal.android.sdk.payments.PaymentActivity.EXTRA_SKIP_CREDIT_CARD, true);
   startActivityForResult(intent, REQUEST_CODE_PAYPAL);
 }
  private void submitOrderForPrinting(String proofOfPayment) {
    if (proofOfPayment != null) {
      printOrder.setProofOfPayment(proofOfPayment);
    }
    printOrder.saveToHistory(this);

    final ProgressDialog dialog = new ProgressDialog(this);
    dialog.setCancelable(false);
    dialog.setIndeterminate(false);
    dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    dialog.setTitle("Processing");
    dialog.setMessage("One moment...");
    dialog.setMax(100);
    dialog.show();

    printOrder.submitForPrinting(
        this,
        new PrintOrderSubmissionListener() {
          @Override
          public void onProgress(
              PrintOrder printOrder,
              int totalAssetsUploaded,
              int totalAssetsToUpload,
              long totalAssetBytesWritten,
              long totalAssetBytesExpectedToWrite,
              long totalBytesWritten,
              long totalBytesExpectedToWrite) {
            if (Looper.myLooper() != Looper.getMainLooper())
              throw new AssertionError("Should be calling back on the main thread");
            final float step = (1.0f / totalAssetsToUpload);
            float progress =
                totalAssetsUploaded * step
                    + (totalAssetBytesWritten / (float) totalAssetBytesExpectedToWrite) * step;
            dialog.setProgress((int) (totalAssetsUploaded * step * 100));
            dialog.setSecondaryProgress((int) (progress * 100));
            dialog.setMessage("Uploading images");
          }

          @Override
          public void onSubmissionComplete(PrintOrder printOrder, String orderIdReceipt) {
            if (Looper.myLooper() != Looper.getMainLooper())
              throw new AssertionError("Should be calling back on the main thread");
            printOrder.saveToHistory(PaymentActivity.this);
            dialog.dismiss();
            Intent i = new Intent(PaymentActivity.this, OrderReceiptActivity.class);
            i.putExtra(OrderReceiptActivity.EXTRA_PRINT_ORDER, (Parcelable) printOrder);
            startActivityForResult(i, REQUEST_CODE_RECEIPT);
          }

          @Override
          public void onError(PrintOrder printOrder, Exception error) {
            if (Looper.myLooper() != Looper.getMainLooper())
              throw new AssertionError("Should be calling back on the main thread");
            printOrder.saveToHistory(PaymentActivity.this);
            dialog.dismiss();
            // showErrorDialog(error.getMessage());

            Intent i = new Intent(PaymentActivity.this, OrderReceiptActivity.class);
            i.putExtra(OrderReceiptActivity.EXTRA_PRINT_ORDER, (Parcelable) printOrder);
            startActivityForResult(i, REQUEST_CODE_RECEIPT);
          }
        });
  }