简体   繁体   English

Android中Card.io的用法。 显示信用卡号和有效期

[英]Card.io usage in android. Displaying Credit card number and Expiry date

I am doing an android app, app reads the 16 digit credit card number and Expiry date and display it. 我正在做一个Android应用,该应用会读取16位数字的信用卡号和有效期并显示出来。 For that I'm using Card.io. 为此,我正在使用Card.io。 app scans the card continuously but onActivityResult function is not calling to display card number and Expiry date. 应用程序连续扫描卡,但是onActivityResult函数未调用显示卡号和有效期。

My code is as follow 我的代码如下

import io.card.payment.CardIOActivity;
import io.card.payment.CreditCard;
import org.my.scanExample.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.SlidingDrawer;
import android.widget.TextView;

public class MyScanActivity extends Activity
{
// You MUST register with card.io to get an app token. Go to https://card.io/apps/new/
private static final String MY_CARDIO_APP_TOKEN = "0ac6f74xxxxxxxbefe74e92c7eca"; // My Token number

final String TAG = getClass().getName();

private Button scanButton;
private TextView resultTextView;

private int MY_SCAN_REQUEST_CODE = 100; // arbitrary int

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    resultTextView = (TextView)findViewById(R.id.resultTextView);
    scanButton = (Button)findViewById(R.id.scanButton);

    resultTextView.setText("card.io library version: " + CardIOActivity.sdkVersion() + "\nBuilt: " + CardIOActivity.sdkBuildDate());
}

@Override
protected void onResume() {
    super.onResume();

    if (CardIOActivity.canReadCardWithCamera(this)) {
        scanButton.setText("Scan a credit card with card.io");
    }
    else {
        scanButton.setText("Enter credit card information");
    }
}

public void onScanPress(View v) {
    // This method is set up as an onClick handler in the layout xml
    // e.g. android:onClick="onScanPress"

    Intent scanIntent = new Intent(this, CardIOActivity.class);

    // required for authentication with card.io
    scanIntent.putExtra(CardIOActivity.EXTRA_APP_TOKEN, MY_CARDIO_APP_TOKEN);

    // customize these values to suit your needs.
    scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, true); // default: true
    scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, false); // default: false
    scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_ZIP, false); // default: false

    // hides the manual entry button
    // if set, developers should provide their own manual entry mechanism in the app
    scanIntent.putExtra(CardIOActivity.EXTRA_SUPPRESS_MANUAL_ENTRY, false); // default: false

    // MY_SCAN_REQUEST_CODE is arbitrary and is only used within this activity.
    startActivityForResult(scanIntent, MY_SCAN_REQUEST_CODE);

}

// Here OnActivityResult is not called. //这里不调用OnActivityResult。

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    String resultStr;
    if (data != null && data.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT)) {
        CreditCard scanResult = data.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);

        // Never log a raw card number. Avoid displaying it, but if necessary use getFormattedCardNumber()
        resultStr = "Card Number: " + scanResult.getRedactedCardNumber() + "\n";

        // Do something with the raw number, e.g.:
        // myService.setCardNumber( scanResult.cardNumber );

        if (scanResult.isExpiryValid()) {
            resultStr += "Expiration Date: " + scanResult.expiryMonth + "/" + scanResult.expiryYear + "\n"; 
        }

        if (scanResult.cvv != null) { 
            // Never log or display a CVV
            resultStr += "CVV has " + scanResult.cvv.length() + " digits.\n";
        }

        if (scanResult.zip != null) {
            resultStr += "Zip: " + scanResult.zip + "\n";
        }
    }
    else {
        resultStr = "Scan was canceled.";
    }
    resultTextView.setText(resultStr);

}
}

Please help.. 请帮忙..

Maybe I missed it but I dont see anything wrong with the code. 也许我错过了,但是我没有发现代码有什么问题。 The problem may be in your XML so I thought I would make it easy for you. 问题可能出在您的XML中,所以我想我会为您提供方便。 Send the user to another activity when the scan is completed. 扫描完成后,将用户转到其他活动。 Copy this in the appropriate place. 将其复制到适当的位置。 Create a new xml.file and see if this works. 创建一个新的xml.file,看看是否可行。 If you have any problems tell me. 如果您有任何问题,请告诉我。 This is working fine for me. 这对我来说很好。

private Button scanButton;    

}                   
@Override
protected void onResume() {
super.onResume();


if (CardIOActivity.canReadCardWithCamera(this)) {
scanButton.setText("Charge");
}
else {
scanButton.setText("Charge");
}
}
final String TAG = getClass().getName();
private int MY_SCAN_REQUEST_CODE = 100; // arbitrary int            


public void onScanPress(View v) {
// This method is set up as an onClick handler in the layout xml
// e.g. android:onClick="onScanPress"

Intent scanIntent = new Intent(this, CardIOActivity.class);                 

// required for authentication with card.io
scanIntent.putExtra(CardIOActivity.EXTRA_APP_TOKEN, MY_CARDIO_APP_TOKEN);
//Talking points
// customize these values to suit your needs.
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, true); // default: true
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, false); // default: false
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_ZIP, false); // default: false

// hides the manual entry button
// if set, developers should provide their own manual entry mechanism in the app
scanIntent.putExtra(CardIOActivity.EXTRA_SUPPRESS_MANUAL_ENTRY, false); // default: false

// MY_SCAN_REQUEST_CODE is arbitrary and is only used within this activity.
startActivityForResult(scanIntent, MY_SCAN_REQUEST_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

String resultStr;
if (data != null && data.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT)) {
CreditCard scanResult = data.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);

// Never log a raw card number. Avoid displaying it, but if necessary use getFormattedCardNumber()   ,FYI-getRedactedCardNumber displays partial number.
resultStr = "Card: " + scanResult.getRedactedCardNumber() + "\n";

// Do something with the raw number, e.g.:
// myService.setCardNumber( scanResult.cardNumber );

if (scanResult.isExpiryValid()) {


final TextView xx =(TextView)findViewById(R.id.storeview);
String twox = xx.getText().toString();


Intent intent= new Intent(MyScanActivity.this,WhateverNewActivityYouWant.class)
//Adds the redacted card number
.putExtra("Card Number",resultStr)
startActivity(intent);
finish();


resultStr += "Expiration Date: " + scanResult.expiryMonth + "/" + scanResult.expiryYear + "\n"; 
}
if (scanResult.cvv != null) { 
// Never log or display a CVV
resultStr += "CVV has " + scanResult.cvv.length() + " digits.\n";
}
if (scanResult.zip != null) {
resultStr += "Zip: " + scanResult.zip + "\n";
}
}
else {
resultStr = "Scan was canceled.";
}
resultTextView.setTag(resultStr);




    }}

In your WhateverNewActivityYouWant.class you want to use intent so 在您的WhateverNewActivityYouWant.class中,您想要使用意图

Intent iscool = getIntent();
    if (iscool.getCharSequenceExtra("Card Number") != null) {
    final TextView setmsg = (TextView)findViewById(R.id.resultTextView);
    setmsg.setText(iscool.getCharSequenceExtra("Card Number"));             
    }  

whatevernewactivityyouwant.xml whatnewactivityyouwant.xml

<TextView
    android:id="@+id/resultTextView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:textSize="16sp" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM