Hope anyone gives answer my question. I am working with PayPal PHP SDK where a user can make a payout. Everything working fine only the problem I am facing is that I am not getting transaction id after a payout. if I do not get a transaction id in response then I will not able to identify last transaction status on IPN listener. Below my well-commented code, you can see what is going wrong.
require __DIR__ . '/../bootstrap.php';
// Create a new instance of Payout object
$payouts = new \PayPal\Api\Payout();
$senderBatchHeader = new \PayPal\Api\PayoutSenderBatchHeader();
// ### NOTE:
// You can prevent duplicate batches from being processed. If you specify a `sender_batch_id` that was used in the last 30 days, the batch will not be processed. For items, you can specify a `sender_item_id`. If the value for the `sender_item_id` is a duplicate of a payout item that was processed in the last 30 days, the item will not be processed.
// #### Batch Header Instance
$senderBatchHeader->setSenderBatchId(uniqid())
->setEmailSubject("You have a payment");
// #### Sender Item
// Please note that if you are using single payout with sync mode, you can only pass one Item in the request
$senderItem1 = new \PayPal\Api\PayoutItem();
$senderItem1->setRecipientType('Email')
->setNote('Thanks you for your paymnet')
->setReceiver('shakti.cmexpertise+0522@gmail.com')
->setSenderItemId("" . uniqid())
->setAmount(new \PayPal\Api\Currency('{
"value":"1.0",
"currency":"USD"
}'));
$payouts->setSenderBatchHeader($senderBatchHeader)
->addItem($senderItem1);
// For Sample Purposes Only.
$request = clone $payouts;
// ### Create Payout
try {
$output = $payouts->create(null, $apiContext);
/* get payout batch id of last payout */
$payoutBatchId = $output->getBatchHeader()->getPayoutBatchId();
try {
/* get last payout status using payoutBatchId */
$payoutBatch = \PayPal\Api\Payout::get($payoutBatchId, $apiContext);
$payoutItems = $payoutBatch->getItems();
/* get payout item id */
$payoutItemId = $payoutItems[0]->getPayoutItemId();
// ### Get Payout Item Status
try {
/* get payout item status of last payout */
## below if i put a sleep function of 30 seconds it will return me transaction id if i remove sleep function it will not give transaction id##
$output2 = \PayPal\Api\PayoutItem::get($payoutItemId, $apiContext);
echo $output2;
die;
} catch (Exception $ex) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printError("Get Payout Item Status", "PayoutItem", null, $payoutItemId, $ex);
exit(1);
}
die;
} catch (Exception $ex) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
//ResultPrinter::printError("Get Payout Batch Status", "PayoutBatch", null, $payoutBatchId, $ex);
exit(1);
}
} catch (Exception $ex) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printError("Created Batch Payout", "Payout", null, $request, $ex);
exit(1);
} exit(1);
}
} catch (Exception $ex) {
// NOTE: PLEASE DO NOT USE RESULTPRINTER CLASS IN YOUR ORIGINAL CODE. FOR SAMPLE ONLY
ResultPrinter::printError("Created Batch Payout", "Payout", null, $request, $ex);
exit(1);
}
What response i get if i put a sleep function before
sleep(30);
$output2 = \PayPal\Api\PayoutItem::get($payoutItemId, $apiContext);
echo $output2;
The response like this where i get transaction id:
{
"payout_item_id": "QVF223JGF75L4",
"transaction_id": "5H458734LM705962G",
"transaction_status": "SUCCESS",
"payout_item_fee": {
"currency": "USD",
"value": "0.25"
},
"payout_batch_id": "46G8LEN83TFBQ",
"payout_item": {
"recipient_type": "EMAIL",
"amount": {
"currency": "USD",
"value": "1.00"
},
"note": "Thanks you for your paymnet",
"receiver": "shakti.cmexpertise+0522@gmail.com",
"sender_item_id": "5b51cc1fdc599"
},
"time_processed": "2018-07-20T11:55:57Z",
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payouts-item/QVF223JGF75L4",
"rel": "self",
"method": "GET",
"enctype": "application/json"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payouts/46G8LEN83TFBQ",
"rel": "batch",
"method": "GET",
"enctype": "application/json"
}
]
}
If I remove sleep function like this then I have different output.
$output2 = \PayPal\Api\PayoutItem::get($payoutItemId, $apiContext);
echo $output2;
this above code will not give me transaction id and the response is like below.
{
"payout_item_id": "VWJD9YJC42728",
"transaction_status": "PENDING",
"payout_item_fee": {
"currency": "USD",
"value": "0.25"
},
"payout_batch_id": "3CZ766WZNRVZG",
"payout_item": {
"recipient_type": "EMAIL",
"amount": {
"currency": "USD",
"value": "1.00"
},
"note": "Thanks you for your paymnet",
"receiver": "shakti.cmexpertise+0522@gmail.com",
"sender_item_id": "5b51d2493b77b"
},
"links": [
{
"href": "https://api.sandbox.paypal.com/v1/payments/payouts-item/VWJD9YJC42728",
"rel": "self",
"method": "GET",
"enctype": "application/json"
},
{
"href": "https://api.sandbox.paypal.com/v1/payments/payouts/3CZ766WZNRVZG",
"rel": "batch",
"method": "GET",
"enctype": "application/json"
}
]
}
You can see I did not get any transaction id in the second code. I want to remove the sleep function. and if I remove then how can I make it work. my goal is that I need transaction id in response.
You should repeat your requests while "transaction_status": "PENDING" in response.
$response = \PayPal\Api\PayoutItem::get($payoutItemId, $apiContext);
while ($response['transaction_status'] === 'PENDING') {
$response = \PayPal\Api\PayoutItem::get($payoutItemId, $apiContext);
sleep(1);
}
A possible solution is, to use socket functions .
You can use your script to store Pending payments (by example Redis). Use socket's function to have PHP daemon that check Pending payments in Database.
It's a way.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.