Skip to content

Snappy1

  • Home
  • Android
  • What
  • How
  • Is
  • Can
  • Does
  • Do
  • Why
  • Are
  • Who
  • Toggle search form

[FIXED] android – How to solve setting up this payment problem – paypal

Posted on November 11, 2022 By

Solution 1 :

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.

Problem :

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

Comments

Comment posted by Sumit Shukla

Have you got the solution? I am also facing same issue!

READ  [FIXED] android - OkHttpInterceptor navigating from kotlin interceptor to login fragment
Powered by Inline Related Posts

Comment posted by Alex97

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.

Android Tags:android, paypal, paypal-sandbox

Post navigation

Previous Post: [FIXED] java – The Android LoaderManager is deprecated. Now what?
Next Post: [FIXED] android – Is WorkManager the right solution for me?

Related Posts

[FIXED] opengl es – Changing background color in OpeGL ES Android changes color of texture Android
[FIXED] java – How to create an ImageView on finger touch? Android
[FIXED] java – MPAndroidChart moveViewToX not always working Android
[FIXED] android – Getting error when using a button to access a route in Flutter Android
[FIXED] android – facing getvibrantcolor(int) palette cannot be applied to () in androidx Android
[FIXED] java – Should my room database start at version 0? Android

Archives

  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuándo
  • ¿Cuántas
  • ¿Cuánto
  • ¿Qué
  • Android
  • Are
  • At
  • C'est
  • Can
  • Comment
  • Did
  • Do
  • Does
  • Est-ce
  • Est-il
  • For
  • Has
  • Hat
  • How
  • In
  • Is
  • Ist
  • Kann
  • Où
  • Pourquoi
  • Quand
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welchen
  • Welcher
  • Welches
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Who's
  • Why
  • Wie
  • Will
  • Wird
  • Wo
  • Woher
  • you can create a selvedge edge: You can make the edges of garter stitch more smooth by slipping the first stitch of every row.2022-02-04
  • you really only need to know two patterns: garter stitch

Recent Posts

  • Can VPN be traced by police?
  • Where were Kaiser-Frazer cars built?
  • How do you make gold rose gold paint?
  • What are the newest type of dentures?
  • Can you wear joggers as dress pants?

Recent Comments

No comments to show.

Copyright © 2023 Snappy1.

Powered by PressBook Grid Dark theme