How integrate Paypal in android Application?

前端 未结 3 1531
广开言路
广开言路 2020-12-13 02:53

I try to integrate paypal with Android app using sandbox. I\'m getting successfully longing with paypal but when I am doing payment then Screen will get invisible directly w

3条回答
  •  一个人的身影
    2020-12-13 03:33

    package com.paypal;
    
    import android.app.Activity;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.Toast;
    
    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.PaymentConfirmation;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import java.math.BigDecimal;
    
    public class MainActivity extends AppCompatActivity {
    
        public static  String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX;
        // note that these credentials will differ between live & sandbox environments.
    
        public static  String CONFIG_CLIENT_ID = "Add your Client ID";      /// add your paypal client id 
    
       private static  int REQUEST_CODE_PAYMENT = 1;
        public static PayPalConfiguration config = new PayPalConfiguration()
                .environment(CONFIG_ENVIRONMENT)
                .clientId(CONFIG_CLIENT_ID);
    
        Button btn_payment;
        String paypal_id,paypal_state,paypal_amount,paypal_currency_code;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            btn_payment = (Button)findViewById(R.id.btn_payment);
    
            btn_payment.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBuyPressed();
                }
            });
    
        }
    
        public void onBuyPressed() {
            PayPalPayment thingToBuy = getThingToBuy(PayPalPayment.PAYMENT_INTENT_SALE);
            Intent intent = new Intent(MainActivity.this, PaymentActivity.class);
            intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
            intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy);
            startActivityForResult(intent, REQUEST_CODE_PAYMENT);
        }
    
        private PayPalPayment getThingToBuy(String paymentIntent) {
            return new PayPalPayment(new BigDecimal("1"), "USD", "sample item  ",
                    paymentIntent);
        }
    
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            if (requestCode == REQUEST_CODE_PAYMENT) {
                if (resultCode == Activity.RESULT_OK) {
                    PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
                    if (confirm != null) {
                        try {
                            Log.e("Show", confirm.toJSONObject().toString(4));
                            Log.e("Show", confirm.getPayment().toJSONObject().toString(4));
    
                            JSONObject json=confirm.toJSONObject();
                            JSONObject responce = json.getJSONObject("response");
                            paypal_id = responce.getString("id");
                            paypal_state = responce.getString("state");
                            JSONObject payment=confirm.getPayment().toJSONObject();
                            paypal_amount=payment.getString("amount");
                            paypal_currency_code=payment.getString("currency_code");
    
                            Toast.makeText(getApplicationContext(), "PaymentConfirmation info received" + " from PayPal", Toast.LENGTH_LONG).show();
                        } catch (JSONException e) {
                            Toast.makeText(getApplicationContext(), "an extremely unlikely failure" +
                                    " occurred:", Toast.LENGTH_LONG).show();
                        }
                    }
                } else if (resultCode == Activity.RESULT_CANCELED) {
    
                } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) {
                    Toast.makeText(getApplicationContext(), "An invalid Payment or PayPalConfiguration" +
                            " was submitted. Please see the docs.", Toast.LENGTH_LONG).show();
                }
            }
        }
    
        @Override
        public void onDestroy() {
            // Stop service when done
            stopService(new Intent(this, PayPalService.class));
            super.onDestroy();
        }
    
    }
    
    
    ///////////////// decler paypal activity in manifest ///////////////////////////
    
    
    
    
        
        
        
        
        
        
    
        
        
    
        
            
                
                    
    
                    
                
            
    
    
            
            
    
            
            
            
            
            
            
            
    
        
    
    
    
    
    
    
    
    
    //////////////////// gradel ///////////////////
    
    apply plugin: 'com.android.application'
    
    android {
        compileSdkVersion 23
        buildToolsVersion "23.0.1"
    
        defaultConfig {
            applicationId "com.paypal"
            minSdkVersion 16
            targetSdkVersion 23
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        testCompile 'junit:junit:4.12'
        compile 'com.android.support:appcompat-v7:23.0.1'
        compile 'com.paypal.sdk:paypal-android-sdk:2.14.2'
    }
    

提交回复
热议问题