Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get scanned Image path and scanned image URL (IOS Only) #78

Open
wants to merge 15 commits into
base: master
Choose a base branch
from
61 changes: 49 additions & 12 deletions android/src/main/java/com/cardio/RNCardIOModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,25 @@
import io.card.payment.CardIOActivity;
import io.card.payment.CreditCard;

import android.graphics.Bitmap;
import android.content.ContextWrapper;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;


public class RNCardIOModule extends ReactContextBaseJavaModule implements ActivityEventListener {

public static final int CARD_IO_SCAN = 1;

private Promise promise;
private ReactApplicationContext mReactContext;

public RNCardIOModule(ReactApplicationContext reactContext) {
super(reactContext);
mReactContext = reactContext;
reactContext.addActivityEventListener(this);
}

Expand All @@ -37,8 +48,11 @@ public void scanCard(ReadableMap config, Promise promise) {
this.promise = promise;
Activity activity = getCurrentActivity();
Intent scanIntent = new Intent(activity, CardIOActivity.class);
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, true);
scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, true);
//scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_EXPIRY, true);
//scanIntent.putExtra(CardIOActivity.EXTRA_REQUIRE_CVV, true);
scanIntent.putExtra(CardIOActivity.EXTRA_CAPTURED_CARD_IMAGE, true);
scanIntent.putExtra(CardIOActivity.EXTRA_RETURN_CARD_IMAGE, true);

parseConfig(config, scanIntent);
if (activity != null) {
activity.startActivityForResult(scanIntent, CARD_IO_SCAN);
Expand Down Expand Up @@ -97,24 +111,47 @@ public void parseConfig(ReadableMap config, Intent intent) {
if (config.hasKey("usePaypalActionbarIcon")) {
intent.putExtra(CardIOActivity.EXTRA_USE_PAYPAL_ACTIONBAR_ICON, config.getBoolean("usePaypalActionbarIcon"));
}
if (config.hasKey("borderImageOnly")) {
intent.putExtra(CardIOActivity.EXTRA_SUPPRESS_SCAN, config.getBoolean("borderImageOnly"));
}
}

@Override
public void onActivityResult(Activity activity, int requestCode, int resultCode, Intent data) {
if (requestCode != CARD_IO_SCAN) {
return;
}
if (data != null && data.hasExtra(CardIOActivity.EXTRA_SCAN_RESULT)) {
CreditCard scanResult = data.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);
if (data != null ) {


Bitmap resultCard = CardIOActivity.getCapturedCardImage(data);

ContextWrapper wrapper = new ContextWrapper(mReactContext);
File newImageFile = wrapper.getDir("images",0);
newImageFile = new File(newImageFile, "detectedCardImage"+ ".jpg");
try {
OutputStream outputStream = new FileOutputStream(newImageFile);
resultCard.compress(Bitmap.CompressFormat.JPEG,100,outputStream);
outputStream.flush();
outputStream.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// CreditCard scanResult = data.getParcelableExtra(CardIOActivity.EXTRA_SCAN_RESULT);

WritableMap res = Arguments.createMap();
res.putString("cardType", scanResult.getCardType().getDisplayName(data.getStringExtra(CardIOActivity.EXTRA_LANGUAGE_OR_LOCALE)));
res.putString("cardNumber", scanResult.cardNumber);
res.putString("redactedCardNumber", scanResult.getRedactedCardNumber());
res.putInt("expiryMonth", scanResult.expiryMonth);
res.putInt("expiryYear", scanResult.expiryYear);
res.putString("cvv", scanResult.cvv);
res.putString("postalCode", scanResult.postalCode);
res.putString("cardholderName", scanResult.cardholderName);
res.putString("scannedImagePath", newImageFile.getAbsolutePath());
// res.putString("cardNumber", scanResult.cardNumber);
// res.putString("redactedCardNumber", scanResult.getRedactedCardNumber());
// res.putInt("expiryMonth", scanResult.expiryMonth);
// res.putInt("expiryYear", scanResult.expiryYear);
// res.putString("cvv", scanResult.cvv);
// res.putString("postalCode", scanResult.postalCode);
// res.putString("cardholderName", scanResult.cardholderName);
promise.resolve(res);
} else {
promise.reject("user_cancelled", "The user cancelled");
Expand Down
25 changes: 23 additions & 2 deletions ios/RCTCardIOModule.m
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ - (void)parseConfig:(NSDictionary *)config viewController:(CardIOPaymentViewCont
[self parseConfig:config viewController:scanViewController];

UIViewController *rootViewController = RCTPresentedViewController();
scanViewController.modalPresentationStyle = UIModalPresentationFullScreen;
[rootViewController presentViewController:scanViewController animated:YES completion:nil];
}

Expand All @@ -122,11 +123,29 @@ - (void)userDidCancelPaymentViewController:(CardIOPaymentViewController *)scanVi
_reject(@"user_cancelled", @"The user cancelled", nil);
}

- (NSString *)documentsPathForFileName:(NSString *)name
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES);
NSString *documentsPath = [paths objectAtIndex:0];

return [documentsPath stringByAppendingPathComponent:name];
}

- (NSString *)saveScannedImageToDocumentsDirectory:(UIImage *)scannedImage {
NSData* imageData = UIImagePNGRepresentation(scannedImage);
NSString *filePath = [self documentsPathForFileName:@"scannedImage.png"]; //Add the file name

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the file name doesn't seem to be unique, is it meant to be a temporary location?

[imageData writeToFile:filePath atomically:YES]; //Write the file
return filePath;
}

- (void)userDidProvideCreditCardInfo:(CardIOCreditCardInfo *)cardInfo inPaymentViewController:(CardIOPaymentViewController *)scanViewController {
[scanViewController dismissViewControllerAnimated:YES completion:nil];

NSString *cardType = [CardIOCreditCardInfo displayStringForCardType:cardInfo.cardType usingLanguageOrLocale:scanViewController.languageOrLocale];

NSString *filePath = [self saveScannedImageToDocumentsDirectory:cardInfo.cardImage];
NSURL *filePathURL = [NSURL fileURLWithPath:filePath];
NSString *filePathURLStr = filePathURL.absoluteString;

_resolve(@{
@"cardType": cardType,
@"cardNumber": cardInfo.cardNumber ?: [NSNull null],
Expand All @@ -136,7 +155,9 @@ - (void)userDidProvideCreditCardInfo:(CardIOCreditCardInfo *)cardInfo inPaymentV
@"cvv": cardInfo.cvv ?: [NSNull null],
@"postalCode": cardInfo.postalCode ?: [NSNull null],
@"scanned": @(cardInfo.scanned),
@"cardholderName": cardInfo.cardholderName ?: [NSNull null]
@"cardholderName": cardInfo.cardholderName ?: [NSNull null],
@"scannedImagePath": filePath ?: [NSNull null],
@"scannedImageURL": filePathURLStr ?: [NSNull null]
});
}

Expand Down