如何整合Google钱包付款获取我的应用?

时间:2017-12-08 11:42:07

标签: android

public class MainActivity extends AppCompatActivity
    implements GoogleApiClient.ConnectionCallbacks, GoogleApiClient.OnConnectionFailedListener {
  public static final int MASKED_WALLET_REQUEST_CODE = 888;
  public static final String WALLET_FRAGMENT_ID = "wallet_fragment";
  public static final int FULL_WALLET_REQUEST_CODE = 889;
  private SupportWalletFragment mWalletFragment;
  private MaskedWallet mMaskedWallet;
  private GoogleApiClient mGoogleApiClient;
  private FullWallet mFullWallet;

  @Override protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WalletFragmentStyle walletFragmentStyle =
        new WalletFragmentStyle().setBuyButtonText(BuyButtonText.BUY_NOW)
            .setBuyButtonWidth(Dimension.MATCH_PARENT);

    WalletFragmentOptions walletFragmentOptions = WalletFragmentOptions.newBuilder()
        .setEnvironment(WalletConstants.ENVIRONMENT_SANDBOX)
        .setFragmentStyle(walletFragmentStyle)
        .setTheme(WalletConstants.THEME_LIGHT)
        .setMode(WalletFragmentMode.BUY_BUTTON)
        .build();

    mWalletFragment = SupportWalletFragment.newInstance(walletFragmentOptions);

    WalletFragmentInitParams.Builder startParamsBuilder = WalletFragmentInitParams.newBuilder()
        .setMaskedWalletRequest(generateMaskedWalletRequest())
        .setMaskedWalletRequestCode(MASKED_WALLET_REQUEST_CODE)
        .setAccountName("Google I/O Codelab");
    mWalletFragment.initialize(startParamsBuilder.build());

    getSupportFragmentManager().beginTransaction()
        .replace(R.id.wallet_button_holder, mWalletFragment, WALLET_FRAGMENT_ID)
        .commit();

    mGoogleApiClient = new GoogleApiClient.Builder(this).addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(Wallet.API,
            new Wallet.WalletOptions.Builder().setEnvironment(WalletConstants.ENVIRONMENT_SANDBOX)
                .setTheme(WalletConstants.THEME_LIGHT)
                .build())
        .build();

  }

  public void requestFullWallet(View view) {
    if (mMaskedWallet == null) {
      Toast.makeText(this, "No masked wallet, can't confirm", Toast.LENGTH_SHORT).show();
      return;
    }
    Wallet.Payments.loadFullWallet(mGoogleApiClient,
        generateFullWalletRequest(mMaskedWallet.getGoogleTransactionId()),
        FULL_WALLET_REQUEST_CODE);
  }

  @Override
  public void onStart() {
    mGoogleApiClient.connect();
    super.onStart();
  }

  @Override
  public void onStop() {
    mGoogleApiClient.disconnect();
    super.onStop();
  }



  @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    switch (requestCode) {
      case MASKED_WALLET_REQUEST_CODE:
        switch (resultCode) {
          case Activity.RESULT_OK:
            mMaskedWallet = data.getParcelableExtra(WalletConstants.EXTRA_MASKED_WALLET);
            Toast.makeText(this, "Got Masked Wallet", Toast.LENGTH_SHORT).show();
            break;
          case Activity.RESULT_CANCELED:
            // The user canceled the operation
            break;
          case WalletConstants.RESULT_ERROR:
            Toast.makeText(this, "An Error Occurred", Toast.LENGTH_SHORT).show();
            break;

          case FULL_WALLET_REQUEST_CODE:
            switch (resultCode) {
              case Activity.RESULT_OK:
                mFullWallet = data
                    .getParcelableExtra(WalletConstants.EXTRA_FULL_WALLET);
                // Show the credit card number
                Toast.makeText(this,
                    mFullWallet.getProxyCard().getPan().toString(),
                    Toast.LENGTH_SHORT).show();
                Wallet.Payments.notifyTransactionStatus(mGoogleApiClient,
                    generateNotifyTransactionStatusRequest(
                        mFullWallet.getGoogleTransactionId(),
                        NotifyTransactionStatusRequest.Status.SUCCESS));

                break;
              case WalletConstants.RESULT_ERROR:
                Toast.makeText(this, "An Error Occurred", Toast.LENGTH_SHORT).show();
                break;


            }
            break;


        }
        break;
    }
  }
  public static NotifyTransactionStatusRequest generateNotifyTransactionStatusRequest(
      String googleTransactionId, int status) {
    return NotifyTransactionStatusRequest.newBuilder()
        .setGoogleTransactionId(googleTransactionId)
        .setStatus(status)
        .build();
  }
  private FullWalletRequest generateFullWalletRequest(String googleTransactionId) {
    FullWalletRequest fullWalletRequest = FullWalletRequest.newBuilder()
        .setGoogleTransactionId(googleTransactionId)
        .setCart(Cart.newBuilder()
            .setCurrencyCode("USD")
            .setTotalPrice("10.10")
            .addLineItem(LineItem.newBuilder()
                .setCurrencyCode("USD")
                .setDescription("Google I/O Sticker")
                .setQuantity("1")
                .setUnitPrice("10.00")
                .setTotalPrice("10.00")
                .build())
            .addLineItem(LineItem.newBuilder()
                .setCurrencyCode("USD")
                .setDescription("Tax")
                .setRole(LineItem.Role.TAX)
                .setTotalPrice(".10")
                .build())
            .build())
        .build();
    return fullWalletRequest;
  }

  private MaskedWalletRequest generateMaskedWalletRequest() {
    MaskedWalletRequest maskedWalletRequest = MaskedWalletRequest.newBuilder()
        .setMerchantName("Google I/O Codelab")
        .setPhoneNumberRequired(true)
        .setShippingAddressRequired(true)
        .setCurrencyCode("USD")
        .setCart(Cart.newBuilder()
            .setCurrencyCode("USD")
            .setTotalPrice("10.00")
            .addLineItem(LineItem.newBuilder()
                .setCurrencyCode("USD")
                .setDescription("Google I/O Sticker")
                .setQuantity("1")
                .setUnitPrice("10.00")
                .setTotalPrice("10.00")
                .build())
            .build())
        .setEstimatedTotalPrice("15.00")
        .build();
    return maskedWalletRequest;
  }

  @Override public void onConnected(Bundle bundle) {

  }

  @Override public void onConnectionSuspended(int i) {

  }

  @Override public void onConnectionFailed(ConnectionResult connectionResult) {

  }

  @Override public void onPointerCaptureChanged(boolean hasCapture) {

  }
}

此代码我已经尝试但我可以运行,但我无法使用谷歌钱包付款 我从来没有尝试过谷歌钱包支付方式请告诉我如何实现这是否有任何关键或任何其他设置我们需要使用请帮助我。当我尝试运行此代码并点击BuyNow然后我得到这笔付款选项不再在这个应用程序中可用我不知道我将如何设置我需要设置,以便我应该支付,

0 个答案:

没有答案