将条带服务器代码添加到 Backendless Cloud
adding stripe server code to Backendless Cloud
我正在尝试将 stripe 集成到我的 android 应用程序中,服务器代码在 Backendless 中设置为自定义 API 服务。对卡充值后,通过成功的回调方法,我无法在我的条带仪表板上的付款列表中看到它。不确定问题出在哪里。任何建议都是 appreciated.Below 是我目前所拥有的:
ChargeItem://Charge Class
package com.mbaas.service;
public class ChargeItem {
public String token;
public int price;
public String description;
}
ChargeService //无后端服务
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import java.util.HashMap;
import java.util.Map;
public class ChargeService implements IBackendlessService
{
public boolean makeCharge(ChargeItem charges){
Stripe.apiKey = "my stripe secret key";
// Get the credit card details submitted by the form
String token = charges.token;
double price = charges.price;
String desc = charges.description;
String userId = charges.userId;
String orderId = charges.orderId;
// Create a charge: this will charge the user's card
try {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("orderId",orderId);
chargeParams.put("userId",userId);
chargeParams.put("amount", price); // Amount in cents
chargeParams.put("currency", "usd");
chargeParams.put("source", token);
chargeParams.put("description", desc);
@SuppressWarnings("unused")
Charge charge = Charge.create(chargeParams);
}
catch (StripeException e) {
// The card has been declined
return false;
}
return true;
}
}
//我的stripe token回调方法
private void convertCardToToken(Card card, final Orders order){
Stripe stripe = new Stripe(getApplicationContext(), CustomApplication.PUBLISHABLE_KEY);
stripe.createToken(
card,
new TokenCallback() {
public void onSuccess(Token token) {
// Send token to your server
ChargeItem chargeItem = new ChargeItem();
chargeItem.setToken(token.getId());
chargeItem.setOrderId(order.getObjectId());
chargeItem.setPrice(order.getOrder_price());
chargeItem.setUserName(order.getOwnerId());
chargeItem.setDescription("Delivery Fee");
ChargeService.initApplication();
ChargeService chargeService = ChargeService.getInstance();
chargeService.makeChargeAsync(chargeItem, new AsyncCallback<Boolean>() {
@Override
public void handleResponse(Boolean response) {
Toast.makeText(getApplicationContext(),
"Payment Successful",
Toast.LENGTH_LONG
).show();
}
@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(getApplicationContext(), fault.getMessage(),
Toast.LENGTH_LONG
).show();
}
});
}
public void onError(Exception error) {
// Show localized error message
Toast.makeText(CheckoutActivity.this,
error.getLocalizedMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
}
我不熟悉 Backendless,所以我不能在这里提供太多帮助,但是您的代码存在一些问题:
price
是一个 double
。为避免舍入错误,Stripe 的 API 中的所有金额均以美分为单位(或更一般地说,以您使用的货币的最小单位表示),因此 price
应为 int
。
当 creating a charge. You likely want to pass these variables as metadata values. 时,userId
和 orderId
不是有效参数
为了帮助您进一步调试,您还应该在 https://dashboard.stripe.com/test/logs?method=not_get 上检查您的 Stripe 帐户仪表板中的日志。您应该看到对 POST /v1/tokens
的请求(由您的 Android 应用程序发送)和对 POST /v1/charges
的请求(由 Backendless 发送)。
我正在尝试将 stripe 集成到我的 android 应用程序中,服务器代码在 Backendless 中设置为自定义 API 服务。对卡充值后,通过成功的回调方法,我无法在我的条带仪表板上的付款列表中看到它。不确定问题出在哪里。任何建议都是 appreciated.Below 是我目前所拥有的: ChargeItem://Charge Class
package com.mbaas.service;
public class ChargeItem {
public String token;
public int price;
public String description;
}
ChargeService //无后端服务
import com.stripe.Stripe;
import com.stripe.exception.StripeException;
import com.stripe.model.Charge;
import java.util.HashMap;
import java.util.Map;
public class ChargeService implements IBackendlessService
{
public boolean makeCharge(ChargeItem charges){
Stripe.apiKey = "my stripe secret key";
// Get the credit card details submitted by the form
String token = charges.token;
double price = charges.price;
String desc = charges.description;
String userId = charges.userId;
String orderId = charges.orderId;
// Create a charge: this will charge the user's card
try {
Map<String, Object> chargeParams = new HashMap<String, Object>();
chargeParams.put("orderId",orderId);
chargeParams.put("userId",userId);
chargeParams.put("amount", price); // Amount in cents
chargeParams.put("currency", "usd");
chargeParams.put("source", token);
chargeParams.put("description", desc);
@SuppressWarnings("unused")
Charge charge = Charge.create(chargeParams);
}
catch (StripeException e) {
// The card has been declined
return false;
}
return true;
}
}
//我的stripe token回调方法
private void convertCardToToken(Card card, final Orders order){
Stripe stripe = new Stripe(getApplicationContext(), CustomApplication.PUBLISHABLE_KEY);
stripe.createToken(
card,
new TokenCallback() {
public void onSuccess(Token token) {
// Send token to your server
ChargeItem chargeItem = new ChargeItem();
chargeItem.setToken(token.getId());
chargeItem.setOrderId(order.getObjectId());
chargeItem.setPrice(order.getOrder_price());
chargeItem.setUserName(order.getOwnerId());
chargeItem.setDescription("Delivery Fee");
ChargeService.initApplication();
ChargeService chargeService = ChargeService.getInstance();
chargeService.makeChargeAsync(chargeItem, new AsyncCallback<Boolean>() {
@Override
public void handleResponse(Boolean response) {
Toast.makeText(getApplicationContext(),
"Payment Successful",
Toast.LENGTH_LONG
).show();
}
@Override
public void handleFault(BackendlessFault fault) {
Toast.makeText(getApplicationContext(), fault.getMessage(),
Toast.LENGTH_LONG
).show();
}
});
}
public void onError(Exception error) {
// Show localized error message
Toast.makeText(CheckoutActivity.this,
error.getLocalizedMessage(),
Toast.LENGTH_LONG
).show();
}
}
);
}
我不熟悉 Backendless,所以我不能在这里提供太多帮助,但是您的代码存在一些问题:
price
是一个double
。为避免舍入错误,Stripe 的 API 中的所有金额均以美分为单位(或更一般地说,以您使用的货币的最小单位表示),因此price
应为int
。
当 creating a charge. You likely want to pass these variables as metadata values. 时,userId
和orderId
不是有效参数
为了帮助您进一步调试,您还应该在 https://dashboard.stripe.com/test/logs?method=not_get 上检查您的 Stripe 帐户仪表板中的日志。您应该看到对 POST /v1/tokens
的请求(由您的 Android 应用程序发送)和对 POST /v1/charges
的请求(由 Backendless 发送)。