我在向客户添加卡时遇到问题。我制作了一个简单的 Express 服务器来处理对 stripe 的 post 请求。目前它正在注册一个新客户,但无法向新客户添加信用卡信息。
在我的 Angular 2 应用程序中,我收集信用卡信息并将其发送到我的 Express 服务器。
Express服务器
router.post('/charge', function(req, res) {
console.log('post incomming');
console.log('reg:', req.body)
var token = req.body.id;
stripe.customers.create({
token: token
}).then(function(customer) {
console.log('customer:', customer)
return stripe.charges.create({
amount: 1000, // Amount in cents
currency: "eur",
customer: customer.id
});
}).then(function(err, charge) {
if(err) {
console.log('shomething wrong', err);
}
if(charge) {
console.log('charge done');
};
});
});
从 Express 服务器获取以下响应
Something is happening.
post incomming
reg: { '{\n "id": "tok_19ASCZAgR5ZaHhRaLhfk7vcB",\n "object": "token",\n "card": {\n "id": "card_19ASCYAgR5ZaHhRaQZbBj5uj",\n "object": "card",\n "addr
ess_city": null,\n "address_country": null,\n "address_line1": null,\n "address_line1_check": null,\n "address_line2": null,\n "address_state": null
,\n "address_zip": null,\n "address_zip_check": null,\n "brand": "Visa",\n "country": "US",\n "cvc_check": "unchecked",\n "dynamic_last4": null,\
n "exp_month": 12,\n "exp_year": 2017,\n "funding": "credit",\n "last4": "4242",\n "metadata": {},\n "name": null,\n "tokenization_method": nu
ll\n },\n "client_ip": "90.210.15.78",\n "created": 1477868095,\n "livemode": false,\n "type": "card",\n "used": false\n}': '' }
customer: { id: 'cus_9TP0BdARBgZ0NF',
object: 'customer',
account_balance: 0,
created: 1477868096,
currency: null,
default_source: null,
delinquent: false,
description: null,
discount: null,
email: null,
livemode: false,
metadata: {},
shipping: null,
sources: { object: 'list',
data: [],
has_more: false,
total_count: 0,
url: '/v1/customers/cus_9TP0BdARBgZ0NF/sources' },
subscriptions:
{ object: 'list',
data: [],
has_more: false,
total_count: 0,
url: '/v1/customers/cus_9TP0BdARBgZ0NF/subscriptions' } }
Unhandled rejection Error: Cannot charge a customer that has no active card
我的 Angular 2 应用程序必须遵循以下组件和服务。
import { Component, OnInit, NgZone } from '@angular/core';
import { CheckoutService } from './checkout.service';
declare var Stripe:any;
@Component({
selector: 'app-stripe',
templateUrl: './stripe.component.html',
styleUrls: ['./stripe.component.css'],
})
export class StripeComponent implements OnInit {
private cardToken:any;
constructor(private checkoutService:CheckoutService, private _zone: NgZone) {
}
ngOnInit(){
}
setUpCard() {
//here we setup the stripe publish key.
//notice that this is a test key for my account so replace with production key(live)
Stripe.setPublishableKey('PUBKEYHERE');
}
getCardData(number, month, year, cvc) {
//I get the card data typed in here and pass it to the getCardToken method
this.getCardToken(number, month, year, cvc);
}
getCardToken(number, month, year, cvc) {
//set up the card data as an object
var dataObj = {"number": number, "exp_month": month, "exp_year": year, "cvc": cvc};
// Request a token from Stripe:
Stripe.card.createToken(dataObj,
(status, response) => { //I'm using an arrow function instead of stripeResponseHandler(a function also) cos it's kickass!
// basically you can do anything here with the reponse that has your token
// you can hit your backend api and initialize a charge etc
this._zone.run(() => {
if (status === 200) {
this.cardToken = response;
console.log("the card token: ", this.cardToken);
this.checkoutService.postToken(this.cardToken);
}
else {
console.log("error in getting card data: ", response.error)
}
});
}
);
}
}
结帐服务
import { Injectable } from '@angular/core';
import { Http, Headers } from '@angular/http';
import { Observable } from 'rxjs/Rx';
@Injectable()
export class CheckoutService {
constructor(private http:Http) { }
postToken(token) {
console.log('from the posttoken: ', token);
var headers = new Headers();
headers.append('Content-Type', 'application/x-www-form-urlencoded');
this.http.post('http://localhost:8080/api/charge', token, {
headers: headers
}).subscribe(
data => console.log('data: ', data),
err => this.logError(err),
() => console.log('Authentication Complete')
);
}
logError(err) {
console.error('There was an error: ' + err);
}
}
提交表单时,我收到以下回复:
from the posttoken: Object {id: "tok_19ASCZAgR5ZaHhRaLhfk7vcB", object: "token", card: Object, client_ip: "90.210.15.78", created: 1477868095…}
问题
为什么 Stripe 不将提供的信用卡信息添加到新创建的客户中?
最佳答案
我认为您在创建费用之前没有为客户创建卡。
router.post('/charge', function(req, res) {
console.log('post incomming');
console.log('reg:', req.body)
var token = req.body.id;
stripe.customers.create({
token: token,
}).then(function(customer) {
console.log('customer:', customer)
stripe.customers.createSource({
customer.id,
{source: token},
}).then(function(card) {
return stripe.charges.create({
amount: 1000, // Amount in cents
currency: "eur",
customer: customer.id,
source: card.id,
});
}
}).then(function(err, charge) {
if(err) {
console.log('shomething wrong', err);
}
if(charge) {
console.log('charge done');
};
});
});
关于javascript - Stripe 不向客户添加卡 - Angular 2/Express,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40334632/