Reputation: 551
I have the following code where I want to implement Stripe payment in android application.please any tell how this problem i can solve if i giving card number dinamically in code like this:card = new Card("4242424242421111", 12, 2019, "123");
then after success token is creating but i can't give card number dynamically eachtime. so that i have added CardInputWidget
in my xml layout and writing this code:
CardInputWidget mCardInputWidget = (CardInputWidget) findViewById(R.id.card_input_widget);
card = mCardInputWidget.getCard();
then java.lang.RuntimeException: Required Parameter:
'card' is required to create a token how can i solve this issues i am trying to solve this since last one day but unable to solve
my full code:
Card card;
public static final String PUBLISHABLE_KEY = "pk_test_djaC9oh3D3xwh8FxOjh7pnew";
private ProgressDialog progress;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maincard);
btnsubmit= (Button) findViewById(R.id.btnsubmit);
CardInputWidget mCardInputWidget = (CardInputWidget) findViewById(R.id.card_input_widget);
card = mCardInputWidget.getCard();
// card = new Card("4242424242424242", 12, 2018, "123");
if (card == null) {
// mErrorDialogHandler.showError("Invalid Card Data");
Stripe stripe = null;
try {
stripe = new Stripe(MainActivity.this, PUBLISHABLE_KEY);
} catch (AuthenticationException e) {
e.printStackTrace();
}
stripe.createToken(
card,
new TokenCallback() {
public void onSuccess(Token token) {
// Send token to your server
Log.d("token","token"+token);
}
public void onError(Exception error) {
// Show localized error message
Log.d("token","excep"+error.getMessage());
}
}
);
}`
activity_maincard.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.mspl.stripepay.MainActivity"
xmlns:wallet="http://schemas.android.com/apk/res-auto">
<com.stripe.android.view.CardInputWidget
android:id="@+id/card_input_widget"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
This is my xml containing CardInputWidget
please try to help thanks in advance.
Upvotes: 2
Views: 2146
Reputation: 551
Finally I got my solution to get Card Information using CardInputWidget
code.
Here is how I did it:
if(mCardInputWidget.getCard()!=null){
String cvv= mCardInputWidget.getCard().getCVC();
int exp= mCardInputWidget.getCard().getExpMonth();
int exp_year= mCardInputWidget.getCard().getExpYear();
String card_num= mCardInputWidget.getCard().getNumber();
card = new Card(card_num, exp, exp_year, cvv);
}
Upvotes: 2