Android 将收据发送至电子邮件
我在android应用程序上使用stripe进行付款。我希望在用户成功购买物品后,能够向他们保存在数据库(Firebase)中的电子邮件发送收据。我确实看了一下文档,看看如何操作,但我有点卡住了。我注意到我应该把这个添加到我的服务器“收据”电子邮件中:“jenny”。rosen@example.com,但我只是想知道如何告诉它将其发送到从数据库(Firebase)保存的电子邮件中Android 将收据发送至电子邮件,android,firebase,stripe-payments,Android,Firebase,Stripe Payments,我在android应用程序上使用stripe进行付款。我希望在用户成功购买物品后,能够向他们保存在数据库(Firebase)中的电子邮件发送收据。我确实看了一下文档,看看如何操作,但我有点卡住了。我注意到我应该把这个添加到我的服务器“收据”电子邮件中:“jenny”。rosen@example.com,但我只是想知道如何告诉它将其发送到从数据库(Firebase)保存的电子邮件中 //服务器设置 const express=要求(“express”); 常量app=express(); cons
//服务器设置
const express=要求(“express”);
常量app=express();
const{resolve}=require(“路径”);
//这是您真正的测试机密API密钥。
const stripe=要求(“stripe”)(“sk掼U测试”***********************************************************************************”);
应用程序使用(快速静态(“.”);
使用(express.json());
常量calculateOrderAmount=项目=>{
//用订单金额的计算替换此常量
//计算服务器上的订单总数以防止
//人们不能直接在客户机上操纵金额
console.log(项目[0]。金额)
退货项目[0]。金额;
};
app.post(“/create payment intent”),异步(请求、回复)=>{
const{items}=req.body;
const{currency}=req.body;
//使用订单金额和货币创建PaymentIntent
const paymentIntent=wait stripe.paymentIntents.create({
金额:计算订单数量(项目),
货币:货币,,
收到电子邮件:“珍妮。rosen@example.com',
});
res.send({
clientSecret:paymentIntent.client\u secret
});
});
app.get(“/greet”),异步(请求、回复)=>{
res.send('Good to go');
});
const PORT=process.env.PORT | | 5001;
app.listen(端口,()=>console.log('Node server监听端口$(端口)');
//PaymentPage.java
私有void startCheckout(){
Intent=getIntent();
最终字符串t=intent.getStringExtra(“天”);
int in=整数。值(t);
双倍金额=英寸*100;
Map payMap=newhashmap();
Map itemMap=newhashmap();
List itemList=new ArrayList();
payMap.put(“货币”、“美元”);
itemMap.put(“id”、“照片订阅”);
itemMap.put(“金额”,金额);
itemList.add(itemMap);
支付图。放置(“项目”,项目列表);
字符串json=new Gson().toJson(payMap);
RequestBody=RequestBody.create(mediaType,json);
Request Request=newrequest.Builder()
.url(后端_url+“创建支付意图”)
.职位(机构)
.build();
httpClient.newCall(请求)
.enqueue(新的PayCallback(本));
//将pay按钮连接到card小部件和stripe实例
//Button payButton=findViewById(R.id.payButton);
payButton.setOnClickListener((视图)->{
//字符串get_card=cardInputWidget.getCard().getAddressZip();
//Toast.makeText(PaymentPageActivity.this,get_card,Toast.LENGTH_SHORT).show();
PaymentMethodCreateParams params=cardInputWidget.getPaymentMethodCreateParams();
如果(参数!=null){
Map extraParams=newhashmap();
外部参数put(“设置未来使用”、“关闭会话”);
ConfirmPaymentIntentParams confirmParams=ConfirmPaymentIntentParams
.createWithPaymentMethodCreateParams(params,paymentIntentClientSecret);
条带。确认付款(本,确认参数);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
//处理stripe.confirmPayment的结果
stripe.onPaymentResult(请求代码、数据、新PaymentResultCallback(this));
}
公共作废戈巴克(视图){
onBackPressed();
}
私有静态最终类PayCallback实现回调{
@非空私有最终WeakReference activityRef;
PayCallback(@NonNull PaymentPageActivity活动){
activityRef=新的WeakReference(活动);
}
@凌驾
public void onFailure(@NonNull Call Call,@NonNull ioe异常){
final PaymentPageActivity=activityRef.get();
如果(活动==null){
返回;
}
activity.runOnUiThread(()->
Toast.makeText(
活动,“错误:+e.toString(),Toast.LENGTH\u LONG
).show()
);
}
@凌驾
公共void onResponse(@NonNull调用,@NonNull最终响应)
抛出IOException{
final PaymentPageActivity=activityRef.get();
如果(活动==null){
返回;
}
如果(!response.issusccessful()){
activity.runOnUiThread(()->
Toast.makeText(
活动,“错误:+response.toString(),Toast.LENGTH\u LONG
).show()
);
}否则{
活动。onPaymentSuccess(响应);
}
}
}
私有void onPaymentSuccess(@NonNull final Response Response)引发IOException{
Gson Gson=新的Gson();
Type Type=new-TypeToken(){}.getType();
Map responseMap=gson.fromJson(
Objects.requireNonNull(response.body()).string(),
类型
);
paymentIntentClientSecret=responseMap.get(“clientSecret”);
}
私人最终类付款结果回拨
实现ApiResultCallback{
@非空私有最终WeakReference activityRef;
PaymentResultCallback(@NonNull PaymentPageActivity活动){
activityRef=新的WeakReference(活动);
}
@凌驾
成功时公共无效(@NonNull PaymentIntentResult结果){
//Server set up
const express = require("express");
const app = express();
const { resolve } = require("path");
// This is your real test secret API key.
const stripe = require("stripe")("sk_test_************************************************************");
app.use(express.static("."));
app.use(express.json());
const calculateOrderAmount = items => {
// Replace this constant with a calculation of the order's amount
// Calculate the order total on the server to prevent
// people from directly manipulating the amount on the client
console.log(items[0].amount)
return items[0].amount;
};
app.post("/create-payment-intent", async (req, res) => {
const { items } = req.body;
const { currency } = req.body;
// Create a PaymentIntent with the order amount and currency
const paymentIntent = await stripe.paymentIntents.create({
amount: calculateOrderAmount(items),
currency: currency,
receipt_email: 'jenny.rosen@example.com',
});
res.send({
clientSecret: paymentIntent.client_secret
});
});
app.get("/greet", async (req, res) => {
res.send('Good to go');
});
const PORT= process.env.PORT || 5001;
app.listen(PORT, () => console.log('Node server listening on port $(PORT)'));
//PaymentPage.java
private void startCheckout() {
Intent intent = getIntent();
final String t = intent.getStringExtra("days");
int in = Integer.valueOf(t);
double amount=in*100;
Map<String,Object> payMap=new HashMap<>();
Map<String,Object> itemMap=new HashMap<>();
List<Map<String,Object>> itemList =new ArrayList<>();
payMap.put("currency","usd");
itemMap.put("id","photo_subscription");
itemMap.put("amount",amount);
itemList.add(itemMap);
payMap.put("items",itemList);
String json = new Gson().toJson(payMap);
RequestBody body = RequestBody.create(mediaType,json);
Request request = new Request.Builder()
.url(BACKEND_URL + "create-payment-intent")
.post(body)
.build();
httpClient.newCall(request)
.enqueue(new PayCallback(this));
// Hook up the pay button to the card widget and stripe instance
//Button payButton = findViewById(R.id.payButton);
payButton.setOnClickListener((View view) -> {
//String get_card=cardInputWidget.getCard().getAddressZip();
//Toast.makeText(PaymentPageActivity.this, get_card, Toast.LENGTH_SHORT).show();
PaymentMethodCreateParams params = cardInputWidget.getPaymentMethodCreateParams();
if (params != null) {
Map<String, String> extraParams = new HashMap<>();
extraParams.put("setup_future_usage", "off_session");
ConfirmPaymentIntentParams confirmParams = ConfirmPaymentIntentParams
.createWithPaymentMethodCreateParams(params, paymentIntentClientSecret);
stripe.confirmPayment(this, confirmParams);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
// Handle the result of stripe.confirmPayment
stripe.onPaymentResult(requestCode, data, new PaymentResultCallback(this));
}
public void goback(View view) {
onBackPressed();
}
private static final class PayCallback implements Callback {
@NonNull private final WeakReference<PaymentPageActivity> activityRef;
PayCallback(@NonNull PaymentPageActivity activity) {
activityRef = new WeakReference<>(activity);
}
@Override
public void onFailure(@NonNull Call call, @NonNull IOException e) {
final PaymentPageActivity activity = activityRef.get();
if (activity == null) {
return;
}
activity.runOnUiThread(() ->
Toast.makeText(
activity, "Error: " + e.toString(), Toast.LENGTH_LONG
).show()
);
}
@Override
public void onResponse(@NonNull Call call, @NonNull final Response response)
throws IOException {
final PaymentPageActivity activity = activityRef.get();
if (activity == null) {
return;
}
if (!response.isSuccessful()) {
activity.runOnUiThread(() ->
Toast.makeText(
activity, "Error: " + response.toString(), Toast.LENGTH_LONG
).show()
);
} else {
activity.onPaymentSuccess(response);
}
}
}
private void onPaymentSuccess(@NonNull final Response response) throws IOException {
Gson gson = new Gson();
Type type = new TypeToken<Map<String, String>>(){}.getType();
Map<String, String> responseMap = gson.fromJson(
Objects.requireNonNull(response.body()).string(),
type
);
paymentIntentClientSecret = responseMap.get("clientSecret");
}
private final class PaymentResultCallback
implements ApiResultCallback<PaymentIntentResult> {
@NonNull private final WeakReference<PaymentPageActivity> activityRef;
PaymentResultCallback(@NonNull PaymentPageActivity activity) {
activityRef = new WeakReference<>(activity);
}
@Override
public void onSuccess(@NonNull PaymentIntentResult result) {
final PaymentPageActivity activity = activityRef.get();
if (activity == null) {
return;
}
PaymentIntent paymentIntent = result.getIntent();
PaymentIntent.Status status = paymentIntent.getStatus();
if (status == PaymentIntent.Status.Succeeded) {
// Payment completed successfully
/*
Gson gson = new GsonBuilder().setPrettyPrinting().create();
activity.displayAlert(
"Payment completed",
gson.toJson(paymentIntent)
);
*/
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
final DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference("Ads");
final DatabaseReference update = rootRef.child(uid);
final DatabaseReference rootRef1 = FirebaseDatabase.getInstance().getReference("card_information");
final DatabaseReference update1 = rootRef1.child(uid);
update1.child("card_number").setValue(cardInputWidget.getCard().component1());
update1.child("cvc").setValue(cardInputWidget.getCard().component2());
update1.child("expiration_month").setValue(cardInputWidget.getCard().component3());
update1.child("expiration_year").setValue(cardInputWidget.getCard().component4());
update1.child("postal_code").setValue(cardInputWidget.getCard().getAddressZip());
Intent intent = getIntent();
Bundle extras = intent.getExtras();
String get_key = extras.getString("id-key");
update.child(get_key).child("status").setValue("Paid");
Intent intent2=new Intent(PaymentPageActivity.this,ProfileActivity.class);
startActivity(intent2);
} else if (status == PaymentIntent.Status.RequiresPaymentMethod) {
// Payment failed – allow retrying using a different payment method
activity.displayAlert(
"Payment failed",
Objects.requireNonNull(paymentIntent.getLastPaymentError()).getMessage()
);
}
}
@Override
public void onError(@NonNull Exception e) {
final PaymentPageActivity activity = activityRef.get();
if (activity == null) {
return;
}
// Payment request failed – allow retrying using the same payment method
activity.displayAlert("Error", e.toString());
}
}
private void displayAlert(@NonNull String title,
@Nullable String message) {
AlertDialog.Builder builder = new AlertDialog.Builder(this)
.setTitle(title)
.setMessage(message);
builder.setPositiveButton("Ok", null);
builder.create().show();
}