如何验证MY服务器上的Android应用内结算交易?

l33*_*33t 34 php android in-app-billing

我制作了一个Android应用,可以使用应用内结算方式购买商品.购买商品后,可以轻松地在Android电子市场和手机之间同步交易 - 即可在应用中使用.但是,我需要我的服务器知道购买.提供特定于应用程序的数据的决定应该在我的服务器上进行,而不是在客户端应用程序中进行.

例如

  1. 用户从Android Market购买商品X.
  2. 交易数据Y被发送到客户端.
  3. 客户端将Y发送到我的服务器.
  4. 客户端要求服务器为X提供内容.
  5. 如果Y有效,服务器会提供内容.如何实现这一目标?

问:如何验证来自Android客户端(可能源自Google服务器)的交易数据不是假的?即黑客没有生成数据.

Google服务器 - > Android客户端 - >我的服务器 - > Android客户端

也许这更像是一个PHP问题而不是其他任何东西.究竟我的服务器脚本(PHP)应该怎么做才能验证检索到的数据是否真实?

abd*_*lar 20

使用openssl_verify($ data,$ signature,$ key)

变量$ data和$ signature应该使用https从android客户端发送到你的php服务器.该交易包含这两个项目.在您确认客户端上的交易之前将其发送到您的服务器.(请参阅此处的文档 - http://developer.android.com/guide/market/billing/billing_integrate.html)

变量$ key是您的发布商帐户可从"许可和应用内结算"面板中获取的Google公钥.复制公钥并在PHP代码中使用它,最好使用您安装在服务器上的配置文件而不是实际的PHP代码.

如果openssl_verify调用成功,则应将订单号存储在服务器上,并确保它们是唯一的,以便无法重放.请注意,单个数据收据和签名对可以包含许多订单号,但通常是一个订单.


Thu*_*bit 11

我们使用了AndroidBillingLibrary.

将其作为项目安装在Eclipse中,并让您的项目将其作为库导入.

我们实现了BillingController.IConfiguration,就像

import net.robotmedia.billing.BillingController;

public class PhoneBillingConfiguration implements BillingController.IConfiguration{
    @Override
    public byte[] getObfuscationSalt() {
        return new byte[] {1,-2,3,4,-5,6,-7,theseshouldallberandombyteshere,8,-9,0};
    }

    @Override
    public String getPublicKey() {
        return "superlongstringhereIforgothowwemadethis";
    }
}
Run Code Online (Sandbox Code Playgroud)

然后,对于我们的应用,我们扩展Application:

public class LocalizedApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();

//      BillingController.setDebug(true);
        BillingController.setConfiguration(new PhoneBillingConfiguration());
    }
}
Run Code Online (Sandbox Code Playgroud)

AndroidManifest包括这个(和所有其他的东西)

<application
    android:icon="@drawable/icon"
    android:label="@string/app_name"
    android:name=".LocalizedApplication"   <!-- use your specific Application  -->
    android:largeHeap="true"
    android:hardwareAccelerated="true"
    >

    <!-- For billing -->
    <service android:name="net.robotmedia.billing.BillingService" />
        <receiver android:name="net.robotmedia.billing.BillingReceiver">
        <intent-filter>
            <action android:name="com.android.vending.billing.IN_APP_NOTIFY" />
            <action android:name="com.android.vending.billing.RESPONSE_CODE" />
            <action android:name="com.android.vending.billing.PURCHASE_STATE_CHANGED" />
        </intent-filter>
    </receiver>
Run Code Online (Sandbox Code Playgroud)

我们实施了 ISignatureValidator

public class PhoneSignatureValidator implements ISignatureValidator {
    private final String TAG = this.getClass().getSimpleName();
    private PhoneServerLink mServerLink;


    private BillingController.IConfiguration configuration;

    public PhoneSignatureValidator(Context context, BillingController.IConfiguration configuration, String our_product_sku) {
        this.configuration = configuration;
        mServerLink = new PhoneServerLink(context);
        mServerLink.setSku(our_product_sku);
    }


    @Override
    public boolean validate(String signedData, String signature) {
        final String publicKey;
        if (configuration == null || TextUtils.isEmpty(publicKey = configuration.getPublicKey())) {
            Log.w(BillingController.LOG_TAG, "Please set the public key or turn on debug mode");
            return false;
        }
        if (signedData == null) {
            Log.e(BillingController.LOG_TAG, "Data is null");
            return false;
        }
        // mServerLink will talk to your server
        boolean bool = mServerLink.validateSignature(signedData, signature);
        return bool;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是上面的最后几行,它会调用你的类,它实际上会与你的服务器通信.

我们的PhoneServerLink开头是这样的:

public class PhoneServerLink implements GetJSONListener {

    public PhoneServerLink(Context context) {
        mContext = context;
    }

    public boolean validateSignature(String signedData, String signature) {
        return getPurchaseResultFromServer(signedData, signature, false);
    }

    private boolean getPurchaseResultFromServer(String signedData, String signature,  boolean async) {  
            // send request to server using whatever protocols you like 
    }

}
Run Code Online (Sandbox Code Playgroud)

  • 代码看起来不错,但我相信这更像是一个服务器端问题.我的服务器代码究竟如何确定请求(事务信息)是否有效? (3认同)