2015-01-21 66 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

相关问题