我在Angular中使用了插件NativeScript Stripe(https://github.com/triniwiz/nativescript-stripe)。当我从前端动态获取数据时,卡号和cvv很好,但是到期的月份和年份使卡验证过程失败。当我用硬编码的值替换动态的月份和年份数据时,一切都很好。这是我的代码:
import { Component, OnInit, ViewChild, ElementRef } from
"@angular/core";
import { Router } from '@angular/router';
import {Page} from "ui/page";
import {View} from "ui/core/view";
import { CreditCardView, Card, Stripe } from 'nativescript-stripe';
import { EventData } from 'data/observable';
@Component({
selector: "Payment",
moduleId: module.id,
templateUrl: "./payment.component.html"
})
export class PaymentComponent implements OnInit {
@ViewChild("card") card: ElementRef;
constructor(page: Page) {}
ngOnInit(): void {
// Init your component properties here.
}
submit(args: EventData): void {
let cardView = <CreditCardView>this.card.nativeElement;
let card = <Card>cardView.card.card;
const stripe = new Stripe('...');
stripe.createToken(card,(error,token)=>{
if (!error) {
console.log(token.getId());
console.log(token.getCard());
} else {
console.log(error);
}
});
}
HTML:
<CreditCardView id="card"></CreditCardView>
<Button text="Button" (tap)="submit($event)"></Button>
当我尝试打印卡的属性时,我得到了正确的卡号和Cvv,但是card.expireMonth和card.expireYear都给了我一个10位数字,而不是2位数字。我不确定我做错了什么,还是这是一个错误。我只在Android上进行过测试。
编辑: 我尝试过的另一种方式是我认为可以更好地显示问题。 card.expMonth和card.expYear的值是10位数字,而card.number和card.cvc是用户输入的值。如果我用硬编码的两位数字值替换月份和年份,则一切正常。
submit(args: EventData): void {
let cardView = <CreditCardView>this.card.nativeElement;
let card = cardView.card;
const stripe = new Stripe('...');
const cc:Card = new Card(card.number, card.expMonth, card.expYear, card.cvc);
stripe.createToken(cc.card,(error,token)=>{
if (!error) {
console.log(token.getId());
console.log(token.getCard());
} else {
console.log(error);
}
});
}
}
最好的问候, 弗拉德