Привет всем, я хочу интегрировать API PayPal. Я выполнил все настройки и т.д. c, но я получаю эту ошибку, когда добираюсь до финальной стадии платежа: проблема с настройкой этого платежа, и я не понимаю, почему с тех пор Я выполнил все шаги, которые я нашел в Интернете.
Это ошибка:
ОТЛАДКА: E / paypal.sdk: сбой запроса с http statusCode: 422, исключение: сбой запроса с ответом сервера: {"name": "PAYMENT_CREATION_ERROR", "debug_id": "9c95c8e6d1a65", "message": "checkout-сессия не создана", "information_link": "https://developer.paypal.com/docs/api/#PAYMENT_CREATION_ERROR"} E / paypal.sdk : PAYMENT_CREATION_ERROR
Это код:
package com.example.ipill;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;
import org.json.JSONException;
import java.math.BigDecimal;
import java.net.HttpCookie;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
public class CartActivity extends AppCompatActivity implements OnRemoveItemClickListener{
private TextView total;
private ImageButton removeFromCartt;
private Button pay;
private RecyclerView mResultList2;
private DatabaseReference mUserDatabase;
public int Totalprice;
private UsersAdapter adapter;
private ArrayList<Users_get> array_data = new ArrayList<>();
final Handler handler = new Handler();
public static final int PAYPAL_REQUEST_CODE = 7171;
public static final String CONFIG_CLIENT_ID = ".....;
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
.merchantName("MedicineStore")
.merchantPrivacyPolicyUri(
Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(
Uri.parse("https://www.example.com/legal"));
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cart);
mUserDatabase = FirebaseDatabase.getInstance().getReference("Cart");
total = findViewById(R.id.TotalPrice);
total.setText(0+"");
removeFromCartt = findViewById(R.id.removeFromCart);
mResultList2 = findViewById(R.id.cartList);
pay = findViewById(R.id.pay);
mResultList2.setHasFixedSize(true);
mResultList2.setLayoutManager(new LinearLayoutManager(this));
Intent intent = new Intent (this,PayPalService.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION,config);
startService(intent);
// Attach a listener to read the data at our posts reference
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
int flag=1;
if (adapter == null) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
Users_get post = childSnapshot.getValue(Users_get.class);
array_data.add(new Users_get(post.getNome(), post.getPrice(), childSnapshot.getKey()));
getTotalPrice(post.getPrice(),flag);
total.setText(Totalprice+"");
}
setAdapter();
} else {
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
pay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
processPayment();
}
});
}
private void processPayment() {
PayPalPayment payPalPayment = new PayPalPayment(new BigDecimal(String.valueOf(Totalprice)),"EUR",
"Make the payment",PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent (CartActivity.this,PaymentActivity.class);
intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION,config);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT,payPalPayment);
startActivityForResult(intent,PAYPAL_REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode,Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PAYPAL_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirmation = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirmation != null) {
try {
String paymenDetails = confirmation.toJSONObject().toString(4);
startActivity(new Intent(this, PaymentDetails.class)
.putExtra("PaymentDetails", paymenDetails)
.putExtra("PaymentAmount", Totalprice));
} catch (JSONException e) {
e.printStackTrace();
}
}
} else if (requestCode == Activity.RESULT_CANCELED) {
Toast.makeText(this, "Cancel", Toast.LENGTH_SHORT).show();
}
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID)
Toast.makeText(this, "Invalid", Toast.LENGTH_SHORT).show();
}
private void setAdapter() {
adapter = new UsersAdapter(array_data,CartActivity.this);
mResultList2.setAdapter(adapter);
}
public void getTotalPrice(Long price,int flag){
if(flag==1)
{
Totalprice+=price;
}
else {
Totalprice-=price;
}
System.out.println(Totalprice);
}
@Override
public void onRemoveItemClicked(final int position) {
int flag=0;
FirebaseDatabase.getInstance().getReference("Cart").child(array_data.get(position).getKey()).removeValue();
getTotalPrice(array_data.get(position).getPrice(),flag);
total.setText(Totalprice+"");
array_data.remove(position);
adapter.notifyItemRemoved(position);
}
@Override
protected void onDestroy() {
stopService(new Intent(this,PayPalService.class));
super.onDestroy();
if(adapter !=null){
adapter.removeListener();
}
}
}
Другая деятельность:
package com.example.ipill;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import org.json.JSONException;
import org.json.JSONObject;
public class PaymentDetails extends AppCompatActivity {
TextView txtID, txtStatus,txtAmount;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_payment_details);
txtID = findViewById(R.id.txtID);
txtAmount= findViewById(R.id.txtAmount);
txtStatus=findViewById(R.id.txtStatus);
Intent intent = getIntent();
try{
JSONObject jsonObject = new JSONObject(intent.getStringExtra("PaymentDetails"));
showDetails(jsonObject.getJSONObject("response"),intent.getStringExtra("PaymentAmount"));
}
catch (JSONException e) {
e.printStackTrace();
}
}
private void showDetails(JSONObject response, String PaymentAmount) {
try {
txtID.setText(response.getString("id"));
txtStatus.setText(response.getString("state"));
txtAmount.setText("$"+PaymentAmount);
} catch (JSONException e) {
e.printStackTrace();
}
}
}