android - 将 Stripe token 传递给服务器并在服务器上处理

标签 android stripe-payments

我只是想让测试工作,但似乎无法做到任何帮助,我们将不胜感激。我不断收到 strip 无效请求错误。我尝试了多种方法,但无法正常工作。

安卓代码:

private void deposit() throws AuthenticationException {
    //bp.purchase("android.test.purchased");
    //bp.consumePurchase("android.test.purchased");

    Card card = new Card("4242-4242-4242-4242", 12, 2016, "123");

    if ( !card.validateCard() ) {
        // Show errors
        Toast.makeText(getApplicationContext(), "Incorrect card Information", Toast.LENGTH_SHORT).show();
    }
    else {
        Stripe stripe = new Stripe("pk_test_t9ODKMyhv3BwKiEHdQE0bmHi");
        stripe.createToken(
                card,
                new TokenCallback() {
                    public void onSuccess(Token token) {
                        // Send token to your server
                        String url = "/BettingXChange/charge.php";

                        //populate json object with user entered data
                        try {
                            chargeInfo.put("stripeToken", token);
                            Log.e("token", String.valueOf(token));
                            chargeInfo.put("user", savedUserID);

                        } catch (JSONException e) {
                            //Log the exception
                            Log.e("JSON Exception", e.toString());
                        }

                        //Set the response listener
                        Response.Listener responseListener = new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                //Handle the json response
                                try {
                                    //Assign member variables upon Json Response
                                    responseSuccess = response.getInt("success");
                                } catch (JSONException e) {
                                    //Log the exception
                                    Log.e("JSON Exception", e.toString());

                                }

                                //If charge successful, move to main app
                                if (responseSuccess == 1) {
                                    Toast.makeText(getApplicationContext(), "YAYY", Toast.LENGTH_SHORT).show();
                                }
                            }
                        };

                        //Set the error response listener
                        Response.ErrorListener responseErrorListener = new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                //Log the error
                                Log.e("Response Error", error.toString());

                                //Make a toast
                                Toast.makeText(getApplicationContext(),
                                        "sorry",
                                        Toast.LENGTH_SHORT).show();
                            }
                        };

                        //Use the request handler to send the Volley json POST request
                        requestHandler.post(url, chargeInfo, responseListener, responseErrorListener);
                    }

                    public void onError(Exception error) {
                        // Show localized error message
                    /*\
                    Toast.makeText(getContext(),
                            error.getLocalizedString(getContext()),
                            Toast.LENGTH_LONG
                    ).show();*/
                    }
                }
        );
    }
}

php代码:

<?php
require_once("stripe-php-1.17.5/lib/Stripe.php");

mysql_connect("localhost","root","password");
mysql_select_db("testapp");


    $body = file_get_contents('php://input');
    $postvars = json_decode($body, true);
    $token = $postvars["stripeToken"];
    $user = $postvars["user"];

// Set your secret key: remember to change this to your live secret key in production
// See your keys here https://dashboard.stripe.com/account
Stripe::setApiKey("sk_test_BrWE3ndM19knCYGAGj27Wix7");

// Create the charge on Stripe's servers - this will charge the user's card
try {
$charge = Stripe_Charge::create(array(
  "amount" => 1000, // amount in cents, again
  "currency" => "usd",
  "card" => $token,
  "description" => "payinguser@example.com")

);

  //add money to users account
  //get total money from people involved in bet
$sql = mysql_query("SELECT currency FROM `people` WHERE id = '".$user."'");

while($e=mysql_fetch_assoc($sql)){
        $output1[]=$e;
    }

$userAmount = $output1[0]['currency'];
$remainingUserBalance = (1000/100.0)+$userAmount;

$sql = "UPDATE `people` set currency = '".$remainingUserBalance."' WHERE id = '".$user."'";
if(mysql_query($sql)){

        $response["success"] = $token;
        echo $response;

}

} catch(Stripe_CardError $e) {
  // The card has been declined
    echo "carderror";
}
catch (Stripe_InvalidRequestError $e) {
    // You screwed up in your programming. Shouldn't happen!
    echo "uh oh";
} 
catch(Stripe_Error $e){
    //
    //echo "stripe error";
    //echo $e;
}

mysql_close();
?>

最佳答案

通常要在 android 上运行与网络相关的进程或任务,您必须在单独的线程上执行该任务;这是否可能是一个后台线程,一个新线程。方便的是,在 android 的后台线程上异步执行这样的过程并不是那么糟糕。 (只需扩展 AsyncTask 并在所述扩展类中执行操作)如果您尝试执行的任务似乎花费的时间超过几秒钟,建议您考虑使用 java.util.concurrent 包。

Link to android API information for AsyncTask class

Link to android API information for java.util.concurrent

关于android - 将 Stripe token 传递给服务器并在服务器上处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28058767/

相关文章:

javascript - Stripe Checkout.js token 回调是否会阻塞,直到付款处理完毕?

java - 调试期间的 Android Studio 表达式评估

android:从 JNI 打开/dev/video4(网络摄像头)时权限被拒绝

android - 如何在 Android 上显示警报对话框?

android - NoWhenBranchMatchedException 在 Kotlin 中具有详尽的 `when` block

javascript - Uncaught ReferenceError : Stripe is not defined - STRIPE ERROR

java - 为什么我不能从 AsyncTask 加载位图?

stripe-payments - 从 Stripe API 检索支持的国家/地区列表?

php - Laravel Cashier 10 - 尝试显示 Stripe 元素时出错

stripe-payments - 使用 API ID 通过 Stripe 支付一次