Search code examples
phpsqlpayment-gatewaybraintree

Braintree Payments - Checking transaction status


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.


Solution

  • Full disclosure: I work at Braintree. If you have any additional questions, please contact support

    If you have the transaction ID, you can access the status of a transaction by passing the id into a Transaction Find API Call, which returns the transaction object, and querying the status using:

    $transaction = Braintree_Transaction::find("the_transaction_id");
    $transaction->status;
    // "settled"
    

    That being said, it is not usually necessary to query the final status of a transaction after submitting for settlement during the transaction sale call.

    Settlement declined statuses are only possible for PayPal sales, Paypal refunds, and Credit card refunds. If a credit card transaction sale is authorized and submitted for settlement, it will also reach the settled status. Even for PayPal transactions, the transaction will reach the Settlement Declined status immediately, so you can query the status immediately after the transaction sale call using $transaction->status as well.

    Settlement pending transactions are only possible for PayPal transactions as well and will reach that status immediately after the sale call. They are possible for credit card transactions, but only if you request that we enable it.