2012-12-25 107 views
1

我想支持從我的Android應用程序的條紋付款,但它不會運行。 java不兼容?我的代碼和錯誤是下面:集成條紋與Android應用程序

package io.noq.kiosk.android; 

import java.util.HashMap; 
import java.util.Map; 
import com.stripe.Stripe; 
import com.stripe.exception.APIConnectionException; 
import com.stripe.exception.AuthenticationException; 

import com.stripe.exception.CardException; 
import com.stripe.exception.InvalidRequestException; 
import com.stripe.exception.StripeException; 
import com.stripe.model.Charge; 

import io.noq.android.R; 
import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 

public class OnSwipeActivity extends Activity { 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     Stripe.apiKey = "the secret (intentionally omitting)"; 
     Map<String, Object> chargeMap = new HashMap<String, Object>(); 

     chargeMap.put("amount", 100); 
     chargeMap.put("currency", "usd"); 

     Map<String, Object> cardMap = new HashMap<String, Object>(); 
     cardMap.put("number", "4242424242424242"); 
     cardMap.put("exp_month", 12); 
     cardMap.put("exp_year", 2013); 
     chargeMap.put("card", cardMap); 

     try { 
      Charge charge = Charge.create(chargeMap); 
      System.out.println(charge); 
      Log.i("Status is: " + "exiting try onCreate-PayActivity", "let's see what goes out...."); 
      System.out.println("Status is: "+ "exiting try onCreate-PayActivity"); 
     } catch (CardException e) { 
      // Since it's a decline, CardException will be caught 
      System.out.println("CardException is: " + e.getCode()); 
      System.out.println("CardException is: " + e.getParam()); 
      Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCode()); 
     } catch (InvalidRequestException e) { 
      // Invalid parameters were supplied to Stripe's API 
      System.out.println("InvalidRequestException: " + e.getMessage()); 
      Log.i(OnSwipeActivity.class.getName(),"e.getCode() ...." + e.getCause()); 
     } catch (AuthenticationException e) { 
      // Authentication with Stripe's API failed (maybe you changed API keys recently) 
      System.out.println("AuthenticationException: " + e.getMessage()); 
      Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause()); 
     } catch (APIConnectionException e) { 
      // Network communication with Stripe failed 
      System.out.println("APIConnectionException: " + e.getMessage()); 
      Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause()); 
     } catch (StripeException e) { 
      // Display a very generic error to the user, and maybe send yourself an email 
      System.out.println("StripeException: " + e.getMessage()); 
      Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause()); 
     } catch (Exception e) { 
      // Something else happened, completely unrelated to Stripe 
      System.out.println("Exception: " + e.getMessage()); 
      Log.i(OnSwipeActivity.class.getName(), "e.getCode() ...." + e.getCause()); 
     } 
    } 
} 

這些都是錯誤:

5月12日至25日:34:22.625:W/dalvikvm(801):線程ID = 1:螺紋與未捕獲的異常退出(組= 0x40a70930)

5月12日至25日:34:22.647:E/AndroidRuntime(801):致命異常:主

5月12日至25日:34:22.647:E/AndroidRuntime(801):JAVA .lang.NoClassDefFoundError com.stripe.Stripe

5月12日至25日:34:22.647:E/AndroidRuntime(801):在io.noq.kiosk.android.OnSwipeActivity.onCreate(OnSwipeActivity.java:26)

5月12日至25日:34:22.647 :E/AndroidRuntime(801):at android.app.Activity.performCreate(Activity.java:5104)

12-25 05:34:22.647:E/AndroidRuntime(801):at android.app.Instrumentation。 callActivityOnCreate(Instrumentation.java:1080)

5月12日至25日:34:22.647:E/AndroidRuntime(801):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)

12-25 05:34:22.647:E/AndroidRuntime(801):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)

12-25 05:34:22.647:E/AndroidRuntime 801):at android.app.ActivityThread.access $ 600(ActivityThread.java:141)

12-25 05:34:22.647:E/AndroidRuntime(801):at android.app.ActivityThread $ H.handleMessage( ActivityThread.java:1234)

5月12日至25日:34:22.647:E/AndroidRuntime(801):在android.os.Handler.dispatchMessage(Handler.java:99)

5月12日至25日: 34:22.647:E/AndroidRunti我(801):在android.os.Looper.loop(Looper.java:137)

12-25 05:34:22.647:E/AndroidRuntime(801):at android.app.ActivityThread.main(ActivityThread的.java:5039)

12-25 05:34:22.647:E/AndroidRuntime(801):在java.lang.reflect.Method.invokeNative(本機方法)

12-25 5時34分: 22.647:E/AndroidRuntime(801):at java.lang.reflect.Method.invoke(Method.java:511)

12-25 05:34:22.647:E/AndroidRuntime(801):at com.android .internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)

5月12日至25日:34:22.647:E/AndroidRuntime(801):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)

5月12日至25日:34:22.647 :E/AndroidRuntime(801):at dalvik.system.NativeStart.main(Native Method)

+0

我無法確認java庫是否兼容,但有一個應用程序使用小條https://play.google.com/store/apps/details?id=br.com.dina。條紋 –

回答

2

您可以在android項目中使用Strip java庫。我從StripTest

實現POC的使用測試類,您必須導入庫,圖片:Imported libraries

您可以用示例代碼測試:

public class MainActivity extends Activity { 
    public static final String TAG     = "TestStrip"; 
    static Map<String, Object> defaultCardParams = new HashMap<String, Object>(); 
    static Map<String, Object> defaultChargeParams = new HashMap<String, Object>(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Stripe.apiKey = "tGN0bIwXnHdwOa85VABjPdSn8nWY7G7I"; 
     init(); 
     Map<String, Object> chargeMap = new HashMap<String, Object>(); 
     chargeMap.put("amount", 100); 
     chargeMap.put("currency", "usd"); 
     Map<String, Object> cardMap = new HashMap<String, Object>(); 
     cardMap.put("number", "4242424242424242"); 
     cardMap.put("exp_month", 12); 
     cardMap.put("exp_year", 2013); 
     chargeMap.put("card", cardMap); 

     try { 
      Charge createdCharge = Charge.create(defaultChargeParams); 
      Fee fee = createdCharge.getFeeDetails().get(0); 
      Preconditions.checkState("stripe_fee".equals(fee.getType()), "Stripe fee error."); 
      Preconditions.checkState(createdCharge.getFee() == fee.getAmount(), "Error "); 
     } catch (StripeException e) { 
      // Display a very generic error to the user, and maybe send yourself an email 
      Log.e(TAG, "e.getCode() ...." + e.getCause()); 
     } 
    } 

    private void init() { 
     defaultCardParams.put("number", "4242424242424242"); 
     defaultCardParams.put("exp_month", 12); 
     defaultCardParams.put("exp_year", 2015); 
     defaultCardParams.put("cvc", "123"); 
     defaultCardParams.put("name", "Java Bindings Cardholder"); 
     defaultCardParams.put("address_line1", "140 2nd Street"); 
     defaultCardParams.put("address_line2", "4th Floor"); 
     defaultCardParams.put("address_city", "San Francisco"); 
     defaultCardParams.put("address_zip", "94105"); 
     defaultCardParams.put("address_state", "CA"); 
     defaultCardParams.put("address_country", "USA"); 
     defaultChargeParams.put("amount", 100); 
     defaultChargeParams.put("currency", "usd"); 
     defaultChargeParams.put("card", defaultCardParams); 
    } 
} 

難道沒有忘記添加到Android清單。 如果您需要示例項目源,請發郵件給我。

+0

嗨,Anis,我正在開發一個Android應用程序,通過用戶將使用信用卡支付,我會收到錢。我如何使用條紋。請幫幫我。 –

+0

請參考條紋網站:https://stripe.com/ –

+0

@安妮:請提供我通過stripe sdk付款的源代碼。 –

0
In Android Studio 
add in gradle 
compile 'com.stripe:stripe-android:+' 
============================================== 

Card card = new Card(cardnumber,exp.month,exp.year,cvv); 
boolean validation = card.validateCard(); 
     if (validation) { 
      startProgress(); 
      new Stripe().createToken(
        card, 
        PUBLISHABLE_STRIPE_KEY, 
        new TokenCallback() { 
         public void onSuccess(Token token) { 
          // you can get transcation token 
         } 
         public void onError(Exception error) { 
          handleError(error.getLocalizedMessage()); 
          finishProgress(); 
         } 
        }); 
     } else if (!card.validateNumber()) { 
      handleError("The card number that you entered is invalid"); 
     } else if (!card.validateExpiryDate()) { 
      handleError("The expiration date that you entered is invalid"); 
     } else if (!card.validateCVC()) { 
      handleError("The CVC code that you entered is invalid"); 
     } else { 
      handleError("The card details that you entered are invalid"); 
     }