2015-01-21 63 views
1

我只是試圖讓測試工作,似乎無法做到任何幫助將不勝感激。我不斷收到一個條帶無效的請求錯誤。我嘗試了很多東西,但無法讓它工作。將條紋標記傳遞給服務器並在服務器上處理

的Android代碼:

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" => "[email protected]") 

); 

    //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(); 
?> 
+0

能否請你告訴我你是怎麼實現的響應類。我也正在編寫​​一個帶有條紋實現的應用程序。 – 2015-06-05 12:02:09

回答

1

通常運行在Android網絡相關的過程和或任務,你必須在一個單獨的線程中執行任務;無論這可能是一個後臺線程一個新的線程在一起。方便的是,在android的後臺線程上異步執行這樣的進程並不是那麼糟糕。 (只需擴展AsyncTask並在所述擴展類中執行操作)如果您嘗試執行的任務似乎需要超過幾秒的時間,建議您查看使用java.util.concurrent包。

Link to android API information for AsyncTask class

Link to android API information for java.util.concurrent

相關問題