使用x-www-form-urlencoded的POST Parse.Cloud.httpRequest因SyntaxError而失败

时间:2015-12-17 10:41:46

标签: javascript parse-platform

我正在尝试执行以下Parse.Cloud.httpRequest请求,作为信用卡注册(令牌化)过程的一部分:

var cardRegistrationResponse = Parse.Cloud.httpRequest({
    method: 'POST',
    url: cardRegistrationURL,
    headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: {
        'accessKeyRef': accessKey,
        'cardNumber': cardInfo.number,
        'cardExpirationDate': cardInfo.expirationDate,
        'cardCvx': cardInfo.cvx,
        'data': preRegistrationData
    }
});

作为Parse Cloud功能的一部分,我使用PFCloud.callFunction在我的iOS应用程序中调用。但是Parse.Cloud方面的事情似乎在执行请求时失败(在记录所有body参数值后立即记录停止。见下文)并且iOS端报告以下错误:

2015-12-17 11:32:47.494 Nestor[37909:5680550] [Error]: Uncaught SyntaxError: Unexpected token d in <unknown file>:1 (Code: 141, Version: 1.11.0)

这里云控制台日志转储了body参数:

I2015-12-17T10:32:46.363Z]cardNumber: 4970100000000154
I2015-12-17T10:32:46.364Z]cardExpirationDate: 0417
I2015-12-17T10:32:46.365Z]cardCvx: 123
I2015-12-17T10:32:46.366Z]data: Qi7oou23Q8d9B3xUpjdMuYgoy_1_mCBE34uqPb7mkAmJqHGBx4ZVlLNGNPFiFYaJS4wCy-yiraxeE65tmxOe8A
I2015-12-17T10:32:46.367Z]typeof accessKeyRef is string
I2015-12-17T10:32:46.368Z]typeof cardNumber is string
I2015-12-17T10:32:46.369Z]typeof expirationDate is string
I2015-12-17T10:32:46.370Z]typeof cardCvx is string
I2015-12-17T10:32:46.371Z]typeof data is string

我不知道发生了什么事。任何帮助将不胜感激。

所以要把事情放在上下文中,这里是完整的Parse.Cloud函数:

&#13;
&#13;
// Register a credit card for a later use.
//
// Request params may include: 
// - 'credentials': a JSON object with a bearer 'access_token'
//
// Request params must include a 'cardInfo' dictionary with the following keys:
// - 'paymentUserId': the card owner user id in the payment platform
// - 'number': the card number 
// - 'expirationDate': the card expiration date in format 'MMYY'
// - 'cvx': the card security code in the format 'NNN'
// - 'currency': the card currency 3-letter code, eg 'EUR'
// - 'type': the card type, one of 'CB_VISA_MASTERCARD', 'DINERS', or 'MAESTRO'
// 
// Returns a JSON object that contains the registered card info:
// - 'cardId': the id of the registered card in the payment platform
// - 'partialNumber': the partially obfuscated card number
// - 'expirationDate': the card expiration date in the format 'MMYY'
// - 'provider': the card provider, one of 'VISA', 'MASTERCARD'...
Parse.Cloud.define("registerCard", function(request, response) {
	
	var credentials = request.params.credentials;
	var cardInfo = request.params.cardInfo;

	Parse.Promise.as(credentials).then(function(credentials) {
		// Make sure we have credentials

	 	if (typeof credentials === 'undefined') {
	 		return Parse.Cloud.run("authorize")
	 	} else {
	 		return Parse.Promise.as(credentials)
	 	}

	}).then(function(credentials) {
		// Step 1: PREREGISTRATION
	 	var cred = Parse.Promise.as(credentials);

	 	// var preCardRegistrationResponse = preCardRegistration(credentials, cardInfo);
		console.log("Pre-registration");
		console.log("CardType: " + cardInfo.type);
		console.log("Currency: " + cardInfo.currency);
		console.log("UserId: " + cardInfo.userId);

		var preCardRegistrationResponse = Parse.Cloud.httpRequest({
			method: 'POST',
			url: mango_api_url + '/v2/sbsquare2015/cardRegistrations',
			headers: {
				'Content-Type': 'application/json;charset=utf-8',
				'Authorization': 'Bearer ' + credentials.access_token
			},
			body: {
				'CardType': cardInfo.type,
				'Currency': cardInfo.currency,
				// mangopay user id
				'UserId': cardInfo.userId 
			}
		});

	 	return Parse.Promise.when(cred, preCardRegistrationResponse);

	}).then(function(credentials, preCardRegistrationResponse) {
		// Step 2: REGISTRATION
		var cred = Parse.Promise.as(credentials);
		var preCardRegistrationInfo = JSON.parse(preCardRegistrationResponse.text);

		// var cardRegistrationResponse = cardRegistration(credentials, cardInfo, preCardRegistrationInfo);
		var cardRegistrationURL = preCardRegistrationInfo.CardRegistrationURL;
		var preRegistrationData = preCardRegistrationInfo.PreregistrationData;
		var accessKey = preCardRegistrationInfo.AccessKey;

		console.log("Card registration");
		console.log("url: " + cardRegistrationURL);
		console.log("accessKeyRef: " + accessKey);
		console.log("cardNumber: " + cardInfo.number);
		console.log("cardExpirationDate: " + cardInfo.expirationDate);
		console.log("cardCvx: " + cardInfo.cvx);
		console.log("data: " + preRegistrationData);

		console.log("typeof accessKeyRef is " + (typeof accessKey) );
		console.log("typeof cardNumber is " + (typeof cardInfo.number) );
		console.log("typeof expirationDate is " + (typeof cardInfo.expirationDate) );
		console.log("typeof cardCvx is " + (typeof cardInfo.cvx) );

		console.log("typeof data is " + (typeof preRegistrationData) );
		
		var cardRegistrationResponse = Parse.Cloud.httpRequest({
			method: 'POST',
			url: cardRegistrationURL,
			// Default to url form encoded
			// headers: {
			// 	'Content-Type': 'application/x-www-form-urlencoded',
			// },
			body: {
				'accessKeyRef': accessKey,
				'cardNumber': cardInfo.number,
				'cardExpirationDate': cardInfo.expirationDate,
				'cardCvx': cardInfo.cvx,
				'data': preRegistrationData
			}
		});

		return Parse.Promise.when(cred, cardRegistrationResponse);

	}).then(function(credentials, cardRegistrationResponse) {
		// Step 3: POSTREGISTRATION
		var cardRegistrationInfo = JSON.parse(cardRegistrationResponse.text);

		// var postCardRegistrationResponse = postCardRegistration(credentials, cardRegistrationInfo);
		var cardId = cardRegistrationInfo.Id;

		var postCardRegistration = Parse.Cloud.httpRequest({
			method: 'PUT',
			url: mango_api_url + '/v2/sbsquare2015/cardRegistration/' + cardId,
			headers: {
				'Content-Type': 'application/json;charset=utf-8'
			},
			body: cardRegistrationInfo
		});

		return postCardRegistrationResponse;

	}).then(function(postCardRegistrationResponse) {
		// TRANSFORMATION
		var cardRegistrationInfo = JSON.parse(postCardRegistrationResponse.text);
		console.log("cardId: " + cardRegistrationInfo.CardId);
		console.log("expirationDate: " + cardInfo.expirationDate);
		console.log("provider: " + cardRegistrationInfo.CardProvider);

		var registeredCard = {
			'cardId': cardRegistration.CardId,
			'partialNumber': cardInfo.number.slice(0, 6) + '******' + cardInfo.number.slice(12, 16),
			'expirationDate': cardInfo.expirationDate,
			'provider': cardRegistrationInfo.CardProvider
		} 

		return Parse.Promise.as(registeredCard);

	 }).then(function(registeredCard) {

	 	console.log("Registered card " + registeredCard);
	 	response.success(registeredCard);

	 }, function(error) {

	 	console.log("Card registration failed: " + error);
	 	response.error(error);
	 	
	 });
});
&#13;
&#13;
&#13;

附注:我已经在Paw和iOS Swift客户端中测试了整个卡注册,但我试图在服务器端实现它,所以我不必编写Android客户端作为好吧......

但云版本在第2步停止,我在调用Parse Cloud函数&#39; registerCard&#39;时,在我的iOS客户端中收到上述错误。

旁注:我不确定我是否正确使用Promise。我希望能调用最后一个错误函数,但它不是......

感谢您的帮助

0 个答案:

没有答案