简体   繁体   中英

Omnipay Get transaction fee from stripe

Ive been trying to return the transaction fee from stripe using thephpleague/omnipay-stripe .

Im not looking to return or set an application fee, but the actual fee that Stripe take off the for each transaction.

Heres my code so far:

if ($request->input('stripeToken')) {

    $gateway = Omnipay::create('Stripe\PaymentIntents');
    $gateway->initialize([
        'apiKey' => env('STRIPE_SECRET'),
    ]);

    $token = $request->input('stripeToken');
    $paymentMethodId = $request->get('paymentMethodId');

    $response = $gateway->purchase([
        'amount' => session('cost'),
        'currency' => env('STRIPE_CURRENCY'),
        'description' => session('payment_title'),
        'paymentMethod' => $paymentMethodId,
        'token' => $token,
        'name' => \Auth::user()->name,
        'returnUrl' => route('customer.charge.stripe.return_url'),
        'confirm' => true,
    ])->send();


    if ($response->isSuccessful()) {

        $arr_payment_data = $response->getData();

        $data = [
            'type' => session('payment_type'),
            'cost' => session('cost'),
            'duration' => session('duration'),
            'description' => session('payment_title'),
            'transaction_id' => $arr_payment_data['id'],
            'status' => $arr_payment_data['status'],
            'fee' => // Whatever I need to call to get fee,
            'payment_details' => $arr_payment_data
        ];

        Payments::add_payment_to_db($data);

        $request->session()->forget(['cost', 'payment_title', 'duration']);

        return redirect()->route('customer.dashboard')->with([
            'status' => __('customer.payments.success'),
            'alert' => 'success',
        ]);


    } elseif($response->isRedirect()) {

        $response->redirect();

    } else {

        // payment failed: display message to customer
        return redirect()->back()->with([
            'status' => $response->getMessage(),
            'alert' => 'danger',
        ]);
    }

I've tried a few things but im not sure how to get the fee correctly.

Any help or ideas on how to get the fee would be grateful.

Many thanks

You'll need to retrieve the Payment Intent after the purchase has been successful in order to get the fee. I don't use Omnipay but maybe this method can help shoot you in the right direction. It's how I get the fee after a charge has been successful using the Stripe API.

protected function getFeeDetails($payment_intent_id, $user)
    {
        Stripe::setApiKey(env('STRIPE_SECRET'));

        $payment_intent = PaymentIntent::retrieve([
            'id' => $payment_intent_id,
            'expand' => ['charges.data.balance_transaction'],
            ], 
            [
                'stripe_account' => $user->stripe_user_id
            ]);

        return $payment_intent->charges->data[0]->balance_transaction->fee;
    }

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM