public void onButtonPayWithCreditCardClicked(View view) {
    final PayPalCard lastUsedCard = PayPalCard.getLastUsedCard(this);
    if (lastUsedCard != null && !lastUsedCard.hasVaultStorageExpired()) {
      AlertDialog.Builder builder = new AlertDialog.Builder(this);
      if (this.paypalEnvironment == PayPalCard.Environment.SANDBOX) {
        builder.setTitle("Payment Source (Sandbox)");
      } else {
        builder.setTitle("Payment Source");
      }

      builder.setItems(
          new String[] {"Pay with new card", "Pay with card ending " + lastUsedCard.getLastFour()},
          new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int itemIndex) {
              if (itemIndex == 0) {
                payWithNewCard();
              } else {
                payWithExistingCard(lastUsedCard);
              }
            }
          });
      builder.show();
    } else {
      payWithNewCard();
    }
  }
  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());
          }
        });
  }
  @Override
  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_CODE_PAYPAL) {
      if (resultCode == Activity.RESULT_OK) {
        PaymentConfirmation confirm =
            data.getParcelableExtra(
                com.paypal.android.sdk.payments.PaymentActivity.EXTRA_RESULT_CONFIRMATION);
        if (confirm != null) {
          try {
            String proofOfPayment =
                confirm
                    .toJSONObject()
                    .getJSONObject("proof_of_payment")
                    .getJSONObject("adaptive_payment")
                    .getString("pay_key");
            submitOrderForPrinting(proofOfPayment);
          } catch (JSONException e) {
            showErrorDialog(e.getMessage());
          }
        }
      }
    } else if (requestCode == REQUEST_CODE_CREDITCARD) {
      if (data != null && data.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT)) {
        CreditCard scanResult = data.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);

        if (!scanResult.isExpiryValid()) {
          showErrorDialog("Sorry it looks like that card has expired. Please try again.");
          return;
        }

        PayPalCard card = new PayPalCard();
        card.setNumber(scanResult.cardNumber);
        card.setExpireMonth(scanResult.expiryMonth);
        card.setExpireYear(scanResult.expiryYear);
        card.setCvv2(scanResult.cvv);
        card.setCardType(PayPalCard.CardType.getCardType(scanResult.getCardType()));

        if (card.getCardType() == PayPalCard.CardType.UNSUPPORTED) {
          showErrorDialog(
              "Sorry we couldn't recognize your card. Please try again manually entering your card details if necessary.");
          return;
        }

        final ProgressDialog dialog = new ProgressDialog(this);
        dialog.setCancelable(false);
        dialog.setTitle("Processing");
        dialog.setMessage("One moment");
        dialog.show();
        card.storeCard(
            paypalEnvironment,
            new PayPalCardVaultStorageListener() {
              @Override
              public void onStoreSuccess(PayPalCard card) {
                dialog.dismiss();
                payWithExistingCard(card);
              }

              @Override
              public void onError(PayPalCard card, Exception ex) {
                dialog.dismiss();
                showErrorDialog(ex.getMessage());
              }
            });

      } else {
        // card scan cancelled
      }
    } else if (requestCode == REQUEST_CODE_RECEIPT) {
      setResult(Activity.RESULT_OK);
      finish();
    }
  }