我希望通过IPN或Webhooks创建以下情况。用户想要提取一些钱,当他点击提款按钮时,将在我的Paypal账户中生成发票,我将有选择接受或拒绝发送资金的选项。接受或拒绝后,我的应用程序将收到通知,以确定是否已发送资金。
我正在阅读他们的文档,但没有找到我想要的内容。
我正在阅读他们的文档,但没有找到我想要的内容。
我想你对于需要使用哪些PayPal功能有些困惑。
IPN和Webhooks是事务后处理工具。只有在发票已经创建、收到付款、提交争议等之后才会触发它们。
此外,您不希望使用实际的发票API,因为PayPal会对其收取更高的费用。
如果您为用户提供了一个提款按钮,您可以使用Payouts API直接触发支付。
您没有说明您正在使用哪种语言,但以下是一个PHP脚本示例,可以触发支付:
<?php
$paypal_client_id = "your_client_id";
$paypal_secret = "your_secret";
$payee_email = "payee@example.com";
$amount = 10.00;
$currency = "USD";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic " . base64_encode("$paypal_client_id:$paypal_secret"),
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$access_token = json_decode($response)->access_token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/payments/payouts",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"sender_batch_header\": {\"sender_batch_id\":\"batch_" . time() . "\",\"email_subject\":\"You have a payment\"},\"items\":[{\"recipient_type\":\"EMAIL\",\"amount\":{\"value\":$amount,\"currency\":\"$currency\"},\"receiver\":\"$payee_email\",\"note\":\"Thank you.\",\"sender_item_id\":\"item_" . time() . "\"}]}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer $access_token"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$payout = json_decode($response);
if ($payout->batch_header->batch_status == "SUCCESS") {
echo "Payout sent successfully!";
} else {
echo "Payout failed: " . $payout->batch_header->failure_reason;
}
}
}
为了让您的应用程序在支付完成时接收到通知,您可以订阅PAYMENT.PAYOUTSBATCH.SUCCESS
Webhook。
以下是一个示例脚本,可订阅该Webhook:
<?php
$paypal_client_id = "your_client_id";
$paypal_secret = "your_secret";
$webhook_url = "https://www.example.com/webhooks/payouts_success";
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/oauth2/token",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "grant_type=client_credentials",
CURLOPT_HTTPHEADER => array(
"Authorization: Basic " . base64_encode("$paypal_client_id:$paypal_secret"),
"Content-Type: application/x-www-form-urlencoded"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$access_token = json_decode($response)->access_token;
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://api.paypal.com/v1/notifications/webhooks",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => "{\"url\":\"$webhook_url\",\"event_types\":[{\"name\":\"PAYMENT.PAYOUTSBATCH.SUCCESS\"}]}",
CURLOPT_HTTPHEADER => array(
"Content-Type: application/json",
"Authorization: Bearer $access_token"
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
if ($err) {
echo "cURL Error #:" . $err;
} else {
$webhook = json_decode($response);
if ($webhook->name == "PAYMENT.PAYOUTSBATCH.SUCCESS") {
echo "Webhook subscribed successfully!";
} else {
echo "Webhook subscription failed: " . $webhook->name;
}
}
}
然后您将在提供的Webhook URL上设置Webhook处理程序。以下是可能的示例:
<?php
$webhook_data = json_decode(file_get_contents('php://input'), true);
if ($webhook_data["event_type"] == "PAYMENT.PAYOUTSBATCH.SUCCESS") {
// handle the payout success event
$batch_id = $webhook_data["resource"]["batch_header"]["payout_batch_id"];
$status = $webhook_data["resource"]["batch_header"]["batch_status"];
// log the batch ID and status for reference
error_log("Batch ID: $batch_id");
error_log("Status: $status");
// process the successful payout
// ...
} else {
// handle other event types
// ...
}