1
votes

Hi everyone I want to integrate the PayPal api I did all the setups etc but I get this error when I get to the final stage of the payment: problem setting up this payment and I don't understand why since I followed all the steps that I found online.

This is the error: enter image description here

DEBUG: E/paypal.sdk: request failure with http statusCode:422,exception: request failed with server response:{"name":"PAYMENT_CREATION_ERROR","debug_id":"9c95c8e6d1a65","message":"checkout-session not created","information_link":"https://developer.paypal.com/docs/api/#PAYMENT_CREATION_ERROR"} E/paypal.sdk: PAYMENT_CREATION_ERROR

This is the code :

   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();
        }
    }
}

Other activity :

    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();
        }
    }
}
1
Have you got the solution? I am also facing same issue!Sumit Shukla
Sumit Shukla you can't add PayPal to your Android app using their API since it got deprecated but you can use Google payment as a payment method . You can search on Google : Google payment Android GitHub and you will find the github repository with the code you need to implement the payment. It's the first link in the search result. Hope my comment was helpful.Alex97

1 Answers

0
votes

It appears you may be using a deprecated Android SDK that is not supported for new integrations

If you need a native SDK, PayPal Express Checkout via the Braintree Mobile SDK is supported.