braintree_api/app/Http/Controllers/TransactionController.php
2025-05-30 14:14:53 +02:00

173 lines
6.9 KiB
PHP

<?php
namespace App\Http\Controllers;
use App\Models\BraintreePaymentRefund;
use App\Models\Country;
use App\Models\Order;
use App\Models\OrdersStatusHistory;
use App\Models\BraintreePayment;
use App\Models\BraintreePaymentStatus;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Braintree\Gateway;
use Illuminate\Support\Facades\Log;
class TransactionController extends Controller
{
protected Gateway $gateway;
public function __construct(Gateway $gateway)
{
$this->gateway = $gateway;
}
public function generateToken(): JsonResponse
{
try {
$clientToken = $this->gateway->clientToken()->generate();
return response()->json(['token' => $clientToken]);
} catch (\Exception) {
return response()->json(['error' => 'Token generation failed.'], 500);
}
}
public function startTransaction(Request $request): JsonResponse
{
Log::channel('braintree')->info('JUCHUUUU');
$gateway = $this->gateway;
$nonce = $request->input('payment_method_nonce');
$orderId = $request->input('oID');
try {
$order = Order::where('orders_id', $orderId)->first();
$billingName = explode(" ",$order->billing_name,2);
$sale = $gateway->transaction()->sale([
'amount' => $order->order_total,
'paymentMethodNonce' => $nonce,
'orderId' => $orderId,
//'merchantAccountId' => $order->currency
'customer' => [
'firstName' => $order->customers_firstname,
'lastName' => $order->customers_firstname,
'email' => "test" . $order->customers_email_address,
'phone' => $order->customers_telephone,
],
'billing' => [
'firstName' => $billingName[0],
'lastName' => $billingName[1],
'company' => $order->billing_company,
'streetAddress' => $order->billing_street_address . " " . $order->billing_street_addressNumber,
'extendedAddress' => $order->billing_suburb,
'locality' => $order->billing_city,
'region' => $order->state,
'postalCode' => $order->billing_postcode,
'countryCodeAlpha2' => Country::where('countries_name',$order->billing_country)->first()->countries_iso_code_2,
],
'options' => [
'submitForSettlement' => false
]
]);
$transaction = $sale->transaction;
$this->newTransactionDb($transaction, $orderId);
if ($sale->success) {
$settlementResult = $gateway->transaction()->submitForSettlement($transaction->id);
$this->updateStatusDb($settlementResult->transaction, $settlementResult->transaction->status);
}
return response()->json(['success' => true, 'transaction' => $settlementResult ?? $transaction]);
}
catch (\Exception $e) {
return response()->json(['success' => false, 'message' => 'Error: nonce:'. $nonce . ', amount: ' . $amount . $e->getMessage()], 500);
}
}
public function settle(Request $request): JsonResponse {
$result = $this->gateway->testing()->settle($request->input("transaction_id"));
return response()->json(['success' => true, 'result' => $result]);
}
public function void(Request $request): JsonResponse {
$result = $this->gateway->transaction()->void($request->input("transaction_id"));
return response()->json(['success' => true, 'result' => $result]);
}
public function startRefund(Request $request): JsonResponse {
$gateway = $this->gateway;
$response=null;
$transactionId = $request->input("transaction_id");
try {
$response = $gateway->transaction()->refund($transactionId, $request->input("amount"));
$refund = $response->transaction;
BraintreePaymentRefund::create([
'refund_id'=>$refund->id,
'transaction_id'=>$transactionId,
'refund_amount'=>$refund->amount,
'refund_currency'=>$refund->currencyIsoCode
]);
OrdersStatusHistory::create([
'orders_id'=>BraintreePayment::where('transaction_id', $transactionId)->first()->orders_id,
'orders_status_id'=> 44,
'customer_notified'=>1,
'comments'=>$refund->status,
'hidden'=>0,
'highlight'=>0,
'message_msg'=>'not yet analyzed',
'message_provider'=>'vat'
]);
BraintreePaymentStatus::create([
'transaction_id'=>$transactionId,
'status_name'=>'refund_submitted_for_settlement',
'gateway_rejection'=>$refund->gatewayRejectionReason
]);
if ($response->success) {
return response()->json(['success' => true, 'refund' => $refund]);
} else {
return response()->json(['success' => false, 'refund' => $refund]);
}
} catch (\Exception $e) {
return response()->json(['success' => false, 'message' => $e->getMessage(), "refund"=>$response], 500);
}
}
public function newTransactionDb($transaction, $orderId): void
{
OrdersStatusHistory::create([
'orders_id'=>$orderId,
'orders_status_id'=> 44,
'customer_notified'=>1,
'comments'=>$transaction->status,
'hidden'=>0,
'highlight'=>0,
'message_msg'=>'not yet analyzed',
'message_provider'=>'vat'
]);
BraintreePayment::create([
'transaction_id'=>$transaction->id,
'orders_id'=> $orderId,
'total'=>$transaction->amount,
'currency'=>$transaction->currencyIsoCode
]);
BraintreePaymentStatus::create([
'transaction_id'=>$transaction->id,
'status_id'=> $transaction->processorResponseCode,
'status_name'=>$transaction->status,
'status_message'=>$transaction->processorResponseText,
'gateway_rejection'=>$transaction->gatewayRejectionReason
]);
}
public function updateStatusDb($transaction) {
BraintreePaymentStatus::create([
'transaction_id'=>$transaction->id,
'status_name'=>$transaction->status,
]);
OrdersStatusHistory::create([
'orders_id'=>$transaction->orderId,
'orders_status_id'=> 44,
'customer_notified'=>1,
'comments'=>$transaction->status,
'hidden'=>0,
'highlight'=>0,
'message_msg'=>'not yet analyzed',
'message_provider'=>'vat'
]);
}
}