验证Google App Engine上的交易

时间:2014-05-21 08:41:22

标签: java android google-app-engine paypal

我试图在Google Play无法使用的国家/地区尝试在Android上进行应用内购买。

使用沙盒API时,一切似乎都能在我的本地Google App Engine开发服务器上运行良好。服务器使用事务ID向PayPal发送请求,并返回包含事务状态的JSON响应。

但是,当我将代码上传到Google App Engine时,它会使用带有相应凭据的实时API。当我在那里进行交易验证时,我从Paypal REST SDK中收到以下错误:

Response Code : 401 with response : {"error":"invalid_client","error_description":"The client credentials are invalid"}

在HttpServlet中,我使用它将常量DEBUG设置为true或false:

@Override
public void init(ServletConfig config) throws ServletException {
    super.init(config);
    String dev = config.getServletContext().getServerInfo();
    if (dev.contains("Development")) {
        Constants.DEBUG = true;
    } else {
        Constants.DEBUG = false;
    }
}

使用以下代码验证交易ID。它获取用于进行API调用的访问令牌,然后验证事务ID。我还创建了一个自定义配置,将GOOGLE_APP_ENGINE设置为true,将MODE设置为沙箱或生活,具体取决于DEBUG常量(这是因为我无法获得getServletContext().getResourceAsStream("sdk_config.properties")工作,因为它给出了拒绝访问错误)。

public static String getAccessToken() throws PayPalRESTException {
    String clientSecret, clientID;
    if (Constants.DEBUG) {
        clientSecret = Constants.CLIENT_SECRET_SANDBOX;
        clientID = Constants.CLIENT_ID_SANDBOX;
    } else {
        clientSecret = Constants.CLIENT_SECRET_LIVE;
        clientID = Constants.CLIENT_ID_LIVE;
    }
    return new OAuthTokenCredential(clientID, clientSecret,
            getPaypalConfig()).getAccessToken();
}

public static Map<String, String> getPaypalConfig() {
    Map<String, String> config = new HashMap<>();
    config.put(com.paypal.core.Constants.GOOGLE_APP_ENGINE,
            String.valueOf(true));
    if (Constants.DEBUG) {
        config.put(com.paypal.core.Constants.MODE,
                com.paypal.core.Constants.SANDBOX);
    } else {
        config.put(com.paypal.core.Constants.MODE,
                com.paypal.core.Constants.LIVE);
    }
    return config;
}

public static boolean payPalVerifier(String saleId)
        throws PayPalRESTException {
    if (accessToken == null) {
        accessToken = Utils.getAccessToken();
        apiContext = new APIContext(accessToken);
        apiContext.setConfigurationMap(Utils.getPaypalConfig());
    }
    boolean completed = false;
    Payment pay = Payment.get(apiContext, saleId);

    for (Transaction transaction : pay.getTransactions()) {
        for (RelatedResources relatedResources : transaction
                .getRelatedResources()) {
            if (com.pixplicity.Constants.DEBUG) {
                completed = relatedResources.getSale().getState()
                        .equals("completed")
                        || relatedResources.getSale().getState()
                                .equals("pending");
            } else {
                completed = relatedResources.getSale().getState()
                        .equals("completed");
            }
        }
    }
    return completed;
}

如何解决这个问题?

0 个答案:

没有答案