2014-05-22 49 views
2

我正在嘗試將貝寶整合到我的Android應用程序中。我正在遵循此方法 http://sunil-android.blogspot.in/2013/10/paypal-android-sdk-with-multiple-in-app.html在Android中集成paypal時出錯?

我在我的應用程序中添加了Paypal Android SDK庫。

但它引發了一個錯誤,許多方法如PaymentActivity.ENVIRONMENT_LIVE;無法解析。

感謝您的幫助

+0

有你'進口com.paypal.android .sdk.payments.PaymentActivity'包? –

+0

@SpringBreaker是的。 – user3467240

+0

'ENVIRONMENT_LIVE'在'PaymentActivity'中。我猜你正在使用較老的sdk。 –

回答

0

這博客文章指SDK的1.x版本,但你最有可能使用2.x版本我建議您使用我們的latest GitHub docs獲取最新的集成信息。

2

嘿,我已經在裏面做了一些變化給出的圖書館根據我的工作 你只是看看,並運行它,只是適用您的客戶ID 希望它會幫助你

MainActivity.java

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.Menu; 
import android.view.MenuItem; 
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 { 

    Button btn_apply; 


    private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_SANDBOX; 
    private static final String CONFIG_CLIENT_ID = "Your Client ID"; 
    // when testing in sandbox, this is likely the -facilitator email address. 
    private static final String CONFIG_RECEIVER_EMAIL = ""; 
    private static final int REQUEST_PAYPAL_PAYMENT = 1; 
    private static final int REQUEST_CODE_PAYMENT = 1; 
    private static PayPalConfiguration paypalConfig = new PayPalConfiguration() 
      .environment(CONFIG_ENVIRONMENT).clientId(
        CONFIG_CLIENT_ID); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     btn_apply = (Button) findViewById(R.id.buyItBtn); 



     btn_apply.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       try { 
        PayPalPayment thingToBuy = new PayPalPayment(new BigDecimal("1.75"), "USD", "hipster jeans", PayPalPayment.PAYMENT_INTENT_SALE); 

        Intent intent = new Intent(MainActivity.this, PaymentActivity.class); 

        intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, paypalConfig); 
        intent.putExtra(PaymentActivity.EXTRA_PAYMENT, thingToBuy); 


        startActivityForResult(intent, REQUEST_CODE_PAYMENT); 
       } catch (NumberFormatException e) { 
        e.printStackTrace(); 
       } 
      } 
     }); 
    } 


    @Override 
    protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
     if (requestCode == REQUEST_PAYPAL_PAYMENT) { 
      if (resultCode == Activity.RESULT_OK) { 
       PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); 
       if (confirm != null) { 


        try { 
         System.out.println("Responseeee" + confirm); 
         Log.i("paymentExample", confirm.toJSONObject().toString()); 
         JSONObject jsonObj = new JSONObject(confirm.toJSONObject().toString()); 
         String paymentId = jsonObj.getJSONObject("response").getString("id"); 
         System.out.println("payment id:-==" + paymentId); 
         Toast.makeText(getApplicationContext(), "Payment Successful", Toast.LENGTH_LONG).show(); 
         Intent main = new Intent(getApplicationContext(), MainActivity.class); 
         startActivity(main); 
         finish(); 
} catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } else if (resultCode == Activity.RESULT_CANCELED) { 
        Log.i("paymentExample", "The user canceled."); 
       } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) { 
        Log.i("paymentExample", "An invalid payment was submitted. Please see the docs."); 
       } 
      } 


     } 
    } 
} 

AndroidManifest.xml中

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.zalak.paypal_proj" > 

    <uses-permission android:name="android.permission.CAMERA" /> 
    <uses-permission android:name="android.permission.VIBRATE" /> 
    <uses-permission android:name="android.permission.INTERNET" /> 
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> 
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> 
    <uses-feature 
     android:name="android.hardware.camera" 
     android:required="false" /> 
    <uses-feature 
     android:name="android.hardware.camera.autofocus" 
     android:required="false" /> 
    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name=".MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 

     <service 
      android:name="com.paypal.android.sdk.payments.PayPalService" 
      android:exported="false" /> 

     <activity android:name="com.paypal.android.sdk.payments.PaymentActivity" /> 
     <activity android:name="com.paypal.android.sdk.payments.LoginActivity" /> 
     <activity android:name="com.paypal.android.sdk.payments.PaymentMethodActivity" /> 
     <activity android:name="com.paypal.android.sdk.payments.PaymentConfirmActivity" /> 

     <activity 
      android:name="io.card.payment.CardIOActivity" 
      android:configChanges="keyboardHidden|orientation" /> 
     <activity android:name="io.card.payment.DataEntryActivity" /> 
    </application> 

如果有任何變動做,讓我知道

1

如果您正在使用Android的工作室。然後,只需將貝寶sdk添加到應用程序級別的build.gradle文件即可。

compile 'com.paypal.sdk:paypal-android-sdk:2.14.2' 

現在您可以使用以下代碼接受付款。記住你還需要你的貝寶客戶端ID。您可以通過創建一個應用程序得到它的developer.paypal.com

private void getPayment() { 
    //Getting the amount from editText 
    paymentAmount = editTextAmount.getText().toString(); 

    //Creating a paypalpayment 
    PayPalPayment payment = new PayPalPayment(new BigDecimal(String.valueOf(paymentAmount)), "USD", "Simplified Coding Fee", 
      PayPalPayment.PAYMENT_INTENT_SALE); 

    //Creating Paypal Payment activity intent 
    Intent intent = new Intent(this, PaymentActivity.class); 

    //putting the paypal configuration to the intent 
    intent.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config); 

    //Puting paypal payment to the intent 
    intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment); 

    //Starting the intent activity for result 
    //the request code will be used on the method onActivityResult 
    startActivityForResult(intent, PAYPAL_REQUEST_CODE); 
} 

的activityresult代碼將

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    //If the result is from paypal 
    if (requestCode == PAYPAL_REQUEST_CODE) { 

     //If the result is OK i.e. user has not canceled the payment 
     if (resultCode == Activity.RESULT_OK) { 
      //Getting the payment confirmation 
      PaymentConfirmation confirm = data.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION); 

      //if confirmation is not null 
      if (confirm != null) { 
       try { 
        //Getting the payment details 
        String paymentDetails = confirm.toJSONObject().toString(4); 
        Log.i("paymentExample", paymentDetails); 

        //Starting a new activity for the payment details and also putting the payment details with intent 
        startActivity(new Intent(this, ConfirmationActivity.class) 
          .putExtra("PaymentDetails", paymentDetails) 
          .putExtra("PaymentAmount", paymentAmount)); 

       } catch (JSONException e) { 
        Log.e("paymentExample", "an extremely unlikely failure occurred: ", e); 
       } 
      } 
     } else if (resultCode == Activity.RESULT_CANCELED) { 
      Log.i("paymentExample", "The user canceled."); 
     } else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID) { 
      Log.i("paymentExample", "An invalid Payment or PayPalConfiguration was submitted. Please see the docs."); 
     } 
    } 
} 

來源:Android PayPal Integration Tutorial