gpt4 book ai didi

ios - 如何通过 firebase 函数将数据传递给 Paypal?

转载 作者:太空宇宙 更新时间:2023-11-03 16:04:59 25 4
gpt4 key购买 nike

我在同一个 Firebase 项目的 Android 和 iOS 上都有一个应用程序。该应用程序的最后一部分是将数据从我的应用程序发送到 firebase-function,然后再发送到 PayPal。它适用于 Android,但不适用于 iOS。请看下面的代码。

我什至与 PayPal 谈过,但他们认为这是 iOS 代码。我在 GitHub、PayPal、Stack Overflow 等网站上发帖,但看不到解决办法。

基本上,当我按下一个按钮 - GET PAYOUT 时,将执行 payoutRequest() 方法。它应该将“uid”和“email”发送到函数,然后发送到 PayPal。

函数中包含PayPal SDK。

它不会将数据发送到 PayPal,但会在函数中被识别(https 函数不是可调用函数)

Android 代码工作

public static final MediaType MEDIA_TYPE = MediaType.parse("application/json"); ProgressDialog progress;

private void payoutRequest() {

progress = new ProgressDialog(this);
progress.setTitle("Processing your payout ...");
progress.setMessage("Please Wait .....");
progress.setCancelable(false);
progress.show();

// HTTP Request ....
final OkHttpClient client = new OkHttpClient();

// in json - we need variables for the hardcoded uid and Email
JSONObject postData = new JSONObject();

try {
postData.put("uid", FirebaseAuth.getInstance().getCurrentUser().getUid());
postData.put("email", mPayoutEmail.getText().toString());

} catch (JSONException e) {
e.printStackTrace();
}

// Request body ...
RequestBody body = RequestBody.create(MEDIA_TYPE, postData.toString());

// Build Request ...
final Request request = new Request.Builder()
.url("https://us-central1-ryyde-sj.cloudfunctions.net/payout")
.post(body)
.addHeader("Content-Type", "application/json")
.addHeader("cache-control", "no-cache")
.addHeader("Authorization", "Your Token")
.build();

client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
// something went wrong right off the bat
progress.dismiss();
}

@Override
public void onResponse(Call call, Response response) throws IOException {
// response successful ....
// refers to response.status('200') or ('500')
int responseCode = response.code();
if (response.isSuccessful()) {
switch(responseCode) {
case 200:
Snackbar.make(findViewById(R.id.layout),
"Payout Successful!", Snackbar.LENGTH_LONG)
.show();
break;

case 500:
Snackbar.make(findViewById(R.id.layout),
"Error: no payout available", Snackbar
.LENGTH_LONG).show();
break;

default:
Snackbar.make(findViewById(R.id.layout),
"Error: couldn't complete the transaction",
Snackbar.LENGTH_LONG).show();
break;
}

} else {
Snackbar.make(findViewById(R.id.layout),
"Error: couldn't complete the transaction",
Snackbar.LENGTH_LONG).show();
}

progress.dismiss();
}
});
}

Firebase 函数:index.js

'use strict';
const functions = require('firebase-functions');
const paypal = require('paypal-rest-sdk');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

paypal.configure({
mode: 'sandbox',
client_id: functions.config().paypal.client_id,
client_secret: functions.config().paypal.client_secret
})

exports.newRequest = functions.database.ref('/history/{pushId}').onCreate((snapshot, context) => {
var requestSnapshot = snapshot.val();
var price = snapshot.child('price').val();
var pushId = context.params.pushId;

return snapshot.ref.parent.child(pushId).child('price').set(price);
});


function getPayoutsPending(uid) {
return admin.database().ref('Users/Drivers/' + uid + '/history').once('value').then((snap) => {
if(snap === null){
throw new Error("profile doesn't exist");
}
var array = [];
if(snap.hasChildren()){
snap.forEach(element => {
if (element.val() === true) {
array.push(element.key);
}
});
}
return array;
}).catch((error) => {
return console.error(error);
});
}

function getPayoutsAmount(array) {
return admin.database().ref('history').once('value').then((snap) => {
var value = 0.0;
if(snap.hasChildren()){
snap.forEach(element => {
if(array.indexOf(element.key) > -1) {
if(element.child('price').val() !== null){
value += element.child('price').val();
}
}
});
return value;
}
return value;
}).catch((error) => {
return console.error(error);
});
}

function updatePaymentsPending(uid, paymentId) {
return admin.database().ref('Users/Drivers/' + uid + '/history').once('value').then((snap) => {
if(snap === null){
throw new Error("profile doesn't exist");
}

if(snap.hasChildren()){
snap.forEach(element => {
if(element.val() === true) {
admin.database().ref('Users/Drivers/' + uid + '/history/' + element.key).set( {
timestamp: admin.database.ServerValue.TIMESTAMP,
paymentId: paymentId
});
admin.database().ref('history/' + element.key + '/driverPaidOut').set(true);
}
});
}
return null;
}).catch((error) => {
return console.error(error);
});
}

exports.payout = functions.https.onRequest((request, response) => {
return getPayoutsPending(request.body.uid)
.then(array => getPayoutsAmount(array))
.then(value => {
var valueTrunc = parseFloat(Math.round((value * 0.75) * 100) / 100).toFixed(2);
const sender_batch_id = Math.random().toString(36).substring(9);
const sync_mode = 'false';
const payReq = JSON.stringify({
sender_batch_header: {
sender_batch_id: sender_batch_id,
email_subject: "You have a payment"
},
items: [
{
recipient_type: "EMAIL",
amount: {
value: valueTrunc,
currency: "CAD"
},
receiver: request.body.email,
note: "Thank you.",
sender_item_id: "Payment"
}
]
});

return paypal.payout.create(payReq, sync_mode, (error, payout) => {
if (error) {
console.warn(error.response);
response.status('500').end();
throw error;
}
console.info("uid: " + request.body.uid + " email: " + request.body.email) // testing
console.info("payout created");
console.info(payout);
return updatePaymentsPending(request.body.uid, sender_batch_id)
});
}).then(() => {
response.status('200').end();
return null;
}).catch(error => {
console.error(error);
});
});

IOS代码:payoutRequest()

var params: Parameters = ["uid": FIRAuth.auth()?.currentUser!.uid as Any, "email": txtPayoutEmail.text!]

let url = URL(string: "https://us-central1-ryyde-sj.cloudfunctions.net/payout")

let headers: HTTPHeaders = [
"Content-Type": "application/json",
"Authorization": "Your Token",
"cache-control": "no-cache"
]

let request = NSMutableURLRequest(url: url!)
let session = URLSession.shared
request.httpMethod = "POST"
params = params as! Dictionary<String, String>
request.httpBody = try! JSONSerialization.data(withJSONObject: params, options: [])
request.allHTTPHeaderFields = headers

let task = session.dataTask(with: request as URLRequest) { data, response, error in
guard data != nil, error == nil else {
print("no data found: \(String(describing: error))")
return
}

if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
print("status code: \(httpStatus.statusCode)")
print("response: \(String(describing: response))")
}

let jsonData = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted)
let parsedObject = try! JSONSerialization.jsonObject(with: jsonData!, options: .allowFragments)
print(parsedObject)
}

task.resume()

会发生什么

执行方法 (payoutRequest()) 时,Firebase 函数中的日志显示“uid”和“email”参数被保留:

firebase logs

在控制台第3行可以看到,uid和email参数都保留了

接下来要将数据(参数)发送到 Paypal,并向电子邮件地址发送通知,说明已收到付款,如下所示:

paypal notification

但是没有这样的通知。

如果我检查 Sandbox 的 API 调用,我会看到:

PayPal API Call for Sandbox

然后 4 小时到一天后,我在日志中收到这个错误,基本上,它被退回了,因为 PayPal 没有保留发送的数据:

firebase logs 2

最佳答案

您在 iOS 代码中遇到的一个问题是 params 的值应该是 [String: String] 类型(这是 POST 所需的类型数据)。

我特别指的是这一行:

var params: Parameters = ["uid": FIRAuth.auth()?.currentUser!.uid as Any, "email": txtPayoutEmail.text!]

尝试将 uid 转换为字符串:

var params: Parameters = ["uid": FIRAuth.auth()?.currentUser!.uid as String, "email": txtPayoutEmail.text!]

关于ios - 如何通过 firebase 函数将数据传递给 Paypal?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53996980/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com