I am creating my first ecommerce site and am using Braintree Payments as the Gateway.
I have set it up as shown here https://www.youtube.com/watch?v=dUAk5kwKfjs so that it is now accepting payments and then updating my orders table in the DB like so:
$result = Braintree_Transaction::sale([
'amount' => $total,
'orderId' => $order_id,
'merchantAccountId' => $active_country_braintree,
'paymentMethodNonce' => $nonce,
'customer' => [
'firstName' => $first_name,
'lastName' => $last_name,
'email' => $email
],
'options' => [
'submitForSettlement' => true
]
]);
if ($result->success === true) {
$transaction_id = $result->transaction->id;
$params = [$transaction_id,$order_id];
$sql = "UPDATE orders SET transaction_id=?, status='processing payment', date_last_status_change=now() WHERE id=?";
$stmt = DB::run($sql,$params);
}else{
$error = serialize($result->errors);
$params = [$error,$order_id];
$sql = "UPDATE orders SET errors=? WHERE id=?";
$stmt = DB::run($sql,$params);
}
This is working fine and all my payments get sent to Braintree for settlement. However, I then want to be able to send another request to Braintree (maybe once per day via cron job) to see if this transaction has been settled or declined and update my DB appropriately.
I've been trying to figure this out looking at Braintrees documentation (https://developers.braintreepayments.com/reference/response/transaction/php#result-object) but I'm not really getting it.
What I want is to be able to pass the transaction ID I stored back to Braintree and get the status of it.
$params = ['processing payment'];
$sql = "SELECT * FROM orders WHERE status=?";
$stmt = DB::run($sql,$params);
$orderCount = $stmt->rowCount();
if ($orderCount > 0) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$id = $row["id"];
$transaction_id = $row["transaction_id"];
$order_id = $row["order_id"];
// Pass transaction ID back to Braintree and get status
// $result = Braintree_Transaction::sale($transaction_id);
// var_dump($result);
}
}
If anyone can give me any help or assistance with this it would be greatly appreciated.