Android将Stripe令牌和卡数据发送到服务器

时间:2017-07-12 12:59:08

标签: php android stripe-payments

我正在尝试整合Stripe以在我的Android应用中获得付款,并且我可以为给定的信用卡成功生成Stripe Token。我的问题是当我使用Volley发送Post请求时,可以对卡进行收费。我使用PHP代码来收取信用卡费用。 我在执行POST请求时收到以下错误:E / Volley:[295] BasicNetwork.performRequest:意外的响应代码500

我的PHP代码负责:

<?php

require_once('stripe-php/init.php');

\Stripe\Stripe::setApiKey('MY API KEY');

// Get the credit card details submitted by the form

$token = $_POST['stripeToken'];

$amount = $_POST['amount'];

$type = $_POST['type'];

$name = $_POST['name'];

// Create the charge on Stripe's servers - this will charge the user's card

try {

$charge = \Stripe\Charge::create(array(

"amount" => $amount,

"currency" => "usd",

"source" => $token,)

);

//email the purcahse and code info to your email


// create a json output with completion variable, (this will be read from the ios app as response)

$json = array(

'completion' => 'done'

);

echo json_encode($json);

} catch(\Stripe\Error\Card $e) {

// The card has been declined

$errorMessage = $e->getMessage();

$json = array(

'completion' => $errorMessage

);

echo json_encode($json);

}

?>

排球要求:

public void postPayment (final Token token, final String url) {



        try {
            final JSONObject jsonBody = new JSONObject();

            jsonBody.put("stripeToken", token.getId());
            jsonBody.put("amount", "200");
            jsonBody.put("name", "Ali Akkawi");
            jsonBody.put("type", "mytype");


            final String mRequestBody = jsonBody.toString();

            StringRequest sendRequest = new StringRequest(Request.Method.POST, url,  new Response.Listener<String>() {



            @Override
                public void onResponse(String response) {

                Log.i(TAG, response.toString());

                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                    error.printStackTrace();
                }
            }) {




                @Override
                public byte[] getBody() {

                    try {

                        return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
                    } catch (UnsupportedEncodingException uee) {

                        VolleyLog.wtf("Unsupported Encoding", mRequestBody, "utf-8");
                        return null;
                    }
                }

//                @Override
//                public String getBodyContentType() {
//
//                    return "application/json; charset=utf-8";
//                }


            };



            Volley.newRequestQueue(PaymentActivity.this).add(sendRequest);

        }catch (Exception e) {


            e.printStackTrace();

}

}

0 个答案:

没有答案