将优惠保存到 Google 电子钱包

Posted

技术标签:

【中文标题】将优惠保存到 Google 电子钱包【英文标题】:Saving offer to Google Wallet 【发布时间】:2014-12-15 12:58:57 【问题描述】:

在我的应用中,我需要实现从我的应用到 Google 电子钱包的储蓄优惠。我找到了这个教程:Developer Documentation- wallet,但是当我尝试实现它时,我遇到了一个问题。页面中有代码:

OfferWalletObject wob = OfferWalletObject
        .newBuilder()...

但是我的环境说那个类没有这个方法。本页没有写任何关于这个方法的内容:Developer Reference Document

有人可以帮忙解决这个问题,或者说一下在谷歌钱包中保存优惠的其他方法。

【问题讨论】:

你找到解决这个问题的方法了吗?我有同样的问题 有人成功导入 CreateWalletObjectsRequest 吗? 请您检查一下developers.google.com/pay/api/android/overview 【参考方案1】:

我已通过此视频成功集成了 Google 电子钱包。 Link

  public class MainActivity extends ActionBarActivity implements  GoogleApiClient.ConnectionCallbacks,
GoogleApiClient.OnConnectionFailedListener


private GoogleApiClient mGoogleApiClient;
private SupportWalletFragment mWalletFregment;
private SupportWalletFragment mXmlWalletFregment;

private MaskedWallet mMaskedWallet;
private FullWallet mFullWallet;

public static final int MASKED_WALLET_REQUEST_CODE=888;
public static final int FULL_WALLET_REQUEST_CODE=889;
public static final String WALLET_FRAGMENT_ID="wallet_fragment";


@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);


    mWalletFregment=(SupportWalletFragment) getSupportFragmentManager()
            .findFragmentByTag(WALLET_FRAGMENT_ID);

    WalletFragmentInitParams startParams;
    WalletFragmentInitParams.Builder startParamsBuilder=WalletFragmentInitParams.newBuilder()
            .setMaskedWalletRequest(generateMaskedwalletRequest())
            .setMaskedWalletRequestCode(MASKED_WALLET_REQUEST_CODE);

    startParams=startParamsBuilder.build();

    Log.e("FREGMENT",""+mWalletFregment);
    if(mWalletFregment==null)
        WalletFragmentStyle walletFragmentStyle=new WalletFragmentStyle()
                .setBuyButtonText(BuyButtonText.BUY_WITH_GOOGLE)
                .setBuyButtonWidth(Dimension.MATCH_PARENT);

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

        mWalletFregment=SupportWalletFragment.newInstance(walletFragmentOptions);
        mWalletFregment.initialize(startParams);

    
    Log.e("FREGMENT",""+mWalletFregment);
    getSupportFragmentManager().beginTransaction()
            .replace(R.id.wallet_button_holder,mWalletFregment,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_HOLO_LIGHT)
                        .build())
            .build();

    setContentView(R.layout.activity_main);



@Override
protected void onStart() 
    mGoogleApiClient.connect();
    super.onStart();


@Override
protected 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);
                    break;
                case Activity.RESULT_CANCELED :
                    break;
                default:
                    Toast.makeText(this,"An Error Occured",Toast.LENGTH_LONG).show();
                    break;
            
            break;
        case FULL_WALLET_REQUEST_CODE :
            switch (resultCode)
                case Activity.RESULT_OK :
                    mFullWallet= data.getParcelableExtra(WalletConstants.EXTRA_FULL_WALLET);
                    Toast.makeText(this,mFullWallet.getProxyCard().getPan().toString(),Toast.LENGTH_LONG).show();
                    Wallet.Payments.notifyTransactionStatus(mGoogleApiClient,
                            generateNotifyTransactionStatusrequest(mFullWallet.getGoogleTransactionId(),
                                    NotifyTransactionStatusRequest.Status.SUCCESS));
                    break;
                default:
                    Toast.makeText(this,"An Error Occured",Toast.LENGTH_LONG).show();
                    break;

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




public static NotifyTransactionStatusRequest generateNotifyTransactionStatusrequest(
        String googleTransactionId,int status )

    return NotifyTransactionStatusRequest.newBuilder()
            .setGoogleTransactionId(googleTransactionId)
            .setStatus(status)
            .build();


private MaskedWalletRequest generateMaskedwalletRequest()
    MaskedWalletRequest maskWalletRequest=MaskedWalletRequest.newBuilder()
            .setMerchantName("Google I/O CoadeLab")
            .setPhoneNumberRequired(true)
            .setShippingAddressRequired(true)
            .setCurrencyCode("INR")
            .setEstimatedTotalPrice("10.00")
            .setCart(Cart.newBuilder()
                      .setCurrencyCode("INR")
                      .setTotalPrice("10.00")
                      .addLineItem(LineItem.newBuilder().setCurrencyCode("INR")
                                      .setQuantity("1")
                                      .setUnitPrice("10.00")
                                       .setTotalPrice("10.00")
                                       .build() )
                      .build())
            .build();
    return maskWalletRequest;



private FullWalletRequest generateFullWalletRequest(String googleTransactionId)
     FullWalletRequest fullWalletRequest=FullWalletRequest.newBuilder()
             .setCart(Cart.newBuilder()
                      .setCurrencyCode("INR")
                       .setTotalPrice("10.10")
                        .addLineItem(LineItem.newBuilder()
                                .setCurrencyCode("INR")
                                .setQuantity("1")
                                .setUnitPrice("10.00")
                                .setTotalPrice("10.00")
                                .build())
                     .addLineItem(LineItem.newBuilder()
                             .setCurrencyCode("INR")
                             .setDescription("Tax")
                             .setRole(LineItem.Role.TAX)
                             .setTotalPrice(".10")
                             .build())
                     .build())
             .build();
    return fullWalletRequest;



public void requestFullWallet(View view)
       if(mGoogleApiClient.isConnected())
           Wallet.Payments.loadFullWallet(mGoogleApiClient,
                   generateFullWalletRequest(mMaskedWallet.getGoogleTransactionId()),
                   FULL_WALLET_REQUEST_CODE);
       


@Override
public void onConnected(Bundle bundle) 



@Override
public void onConnectionSuspended(int i) 



@Override
public void onConnectionFailed(ConnectionResult connectionResult) 



希望这对你有用。

【讨论】:

以上是关于将优惠保存到 Google 电子钱包的主要内容,如果未能解决你的问题,请参考以下文章

当订单在 Google 电子钱包中收取费用时,我如何收到通知?

使用 Google 电子钱包进行定期结算

如何通过 PHP 使用 Google 电子钱包?

在我的网站中使用 Google 电子钱包进行定期付款,包括用户身份验证

通过 API 更新 Google Pay App Loyalty Card

使用脚本将 Google 电子表格导出到文本文件