NullPointerException\com.stripe.android.paymentsheet.PaymentSheet$CustomerConfiguration.<init&



错误信息:java.lang.NullPointerException:指定为non-null的参数为null: method com.stripe.android.paymentsheet.PaymentSheet$CustomerConfiguration。,参数ephemeralKeySecret

my code:

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.stripe.android.PaymentConfiguration;
import com.stripe.android.paymentsheet.PaymentSheet;
import com.stripe.android.paymentsheet.PaymentSheetResult;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MoneyDonation2 extends AppCompatActivity {
Button payment;
String PublishableKey ="sth";
String SecretKey = "sth";
String CustomerId;
String EphericalKey;
String ClientSecret;
PaymentSheet paymentSheet;
@SuppressLint("MissingInflatedId")
protected void onCreate (Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.donation_money);
payment = findViewById(R.id.payment);
PaymentConfiguration.init(this, PublishableKey);
paymentSheet = new PaymentSheet(this, paymentSheetResult -> {
onPaymentResult(paymentSheetResult);
});
payment.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
getEmphericalKey();
paymentFlow();
}
});
StringRequest request = new StringRequest(Request.Method.POST, "https://api.stripe.com/v1/customers", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject object = new JSONObject(response);
CustomerId = object.getString("id");
Toast.makeText(MoneyDonation2.this, CustomerId, Toast.LENGTH_SHORT).show();
//                    getEmphericalKey();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
public void onErrorResponse(VolleyError error){
Toast.makeText(MoneyDonation2.this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}){
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Authorization", "Bearer " + SecretKey);
//                header.put("Stripe-Version", "2022-08-01");
return header;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
private void paymentFlow() {
paymentSheet.presentWithPaymentIntent(ClientSecret, new PaymentSheet.Configuration("Learn with OWL", new PaymentSheet.CustomerConfiguration(
CustomerId, EphericalKey
)));
}
private void onPaymentResult(PaymentSheetResult paymentSheetResult) {
if(paymentSheetResult instanceof PaymentSheetResult.Completed) {
Toast.makeText(this, "Payment Success", Toast.LENGTH_SHORT).show();
}
}
private void getEmphericalKey(){
StringRequest request = new StringRequest(Request.Method.POST, "https://api.stripe.com/v1/ephemeral_keys", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject object = new JSONObject(response);
EphericalKey = object.getString("id");
Toast.makeText(MoneyDonation2.this, EphericalKey, Toast.LENGTH_SHORT).show();
getClientSecret(CustomerId, EphericalKey);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
public void onErrorResponse(VolleyError error){
Toast.makeText(MoneyDonation2.this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}){
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Authorization", "Bearer " + SecretKey);
return header;
}
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> params = new HashMap<>();
params.put("customer", "CustomerId");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
private void getClientSecret(String customerId, String ephericalKey){
StringRequest request = new StringRequest(Request.Method.POST, "https://api.stripe.com/v1/payment_intents", new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try{
JSONObject object = new JSONObject(response);
ClientSecret = object.getString("client_secret");
Toast.makeText(MoneyDonation2.this, ClientSecret, Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener(){
public void onErrorResponse(VolleyError error){
Toast.makeText(MoneyDonation2.this, error.getLocalizedMessage(), Toast.LENGTH_SHORT).show();
}
}){
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> header = new HashMap<>();
header.put("Authorization", "Bearer " + SecretKey);
return header;
}
protected Map<String, String> getParams() throws AuthFailureError{
Map<String, String> params = new HashMap<>();
params.put("customer", "CustomerId");
params.put("amount", "100"+"00");
params.put("currency", "RM");
params.put("automatic_payment_methods[enabled]", "true");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(request);
}
}

我是一个刚刚开始android开发的新手,我希望在项目中包括条纹支付网关,但我得到这个错误,我该如何解决它?

你需要将这个逻辑分割成一个服务器端点来创建支付意图(和可选的临时密钥),然后在你的客户端应用程序中使用这些值来收集支付细节。

还请注意,当您确实拥有这些值时,应该使用客户ID和临时键secret调用CustomerConfiguration,如示例中所示。请确保这两个值都有效。

相关内容