yabacon / paystack-php

A PHP Wrapper for Paystack API - https://www.paystack.com
MIT License
109 stars 70 forks source link

How to use metadata builder #29

Closed simioluwatomi closed 4 years ago

simioluwatomi commented 4 years ago

Problem/Motivation

There is no full example of how to use the MetaBuilder when initiating a transaction. Though there are examples of how the metadata would be built, I'm confused as to when to build the metadata.

I have the code below when initiating a transaction

        $paystack = new Paystack(config('paystack.secret_key'));
        $builder = new MetadataBuilder();
        $builder->withQuoteId(10); // will add as quote_id: 10 unless you turn auto_snake_case off
        $builder->withMobileNumber('08012345678'); // will add as mobile_number: 08012345678
        $builder->withCSRF('dontacceptpaymentunlessthismatches');
        $builder->withCustomField('Mobile Number', '080123456789');
        $builder->withCustomField('See Me', 'I\'m Visible on your Dashboard');
        $builder->build();
        try {
            $transaction = $paystack->transaction->initialize([
                'amount'     => (Cart::totalFloat() + 8000) * 100,
                'email'      => $request->input('email'),
                'reference'  => $orderCode,
            ]);
        } catch (\Yabacon\Paystack\Exception\ApiException $e) {
            return redirect()->back()->with('message', [
                'status'  => 'danger',
                'body'    => 'Failed to make request to payment provider. Please try again',
            ]);
        }

But when the transaction is verified, the metadata field is an empty string.

How should the builder be used when initiating a transaction? Where does the metadata go? How would the embedded metadata be retrieved from the transaction detail response?

nwogu commented 4 years ago

Hi @simioluwatomi, You should add the metadata key when initiating the transaction:

$metadata = new MetadataBuilder;

//Add metadata here

try {
            $transaction = $paystack->transaction->initialize([
                'amount'     => (Cart::totalFloat() + 8000) * 100,
                'email'      => $request->input('email'),
                'reference'  => $orderCode,
                'metadata' => $metadata->build()
            ]);
        } catch (\Yabacon\Paystack\Exception\ApiException $e) {
            return redirect()->back()->with('message', [
                'status'  => 'danger',
                'body'    => 'Failed to make request to payment provider. Please try again',
            ]);
        }
simioluwatomi commented 4 years ago

Thanks @nwogu