Skip to content

Understanding the Direct Checkout Process

Hassaan Ali edited this page Jul 21, 2023 · 1 revision

Introduction:

The Direct Checkout process for Payfast provides a secure and convenient method for merchants to accept online payments. By following a few simple steps, merchants can integrate Payfast into their websites and offer their customers a seamless payment experience. In this article, we will explore the key aspects of the Direct Checkout process and provide a sample PHP code implementation.

Collecting Customer Data:

Next, merchants should gather relevant customer data, such as the order ID, transaction amount, customer mobile number, and email address. This information is crucial for accurately processing the payment and communicating with the customer regarding the transaction. Forexample for now we can use the random ( demo ) data for processing:

Data Required For Customer Validation

The PayfastValidateRequest is the FormRequest created with php artisan make:request PayfastValidateRequest it validates and shows the required validation error messages. The request saves the data to the database for easy access and initiating transaction.

    /**
     * Validate Customer and get OTP Screen.
     * Step 1
     */
    public function checkout(PayfastValidateRequest $request) {
        try {
            $payfast = new Payfast();
            $response = $payfast->getToken();
            if ($response != null && $response->code == "00") { $payfast->setAuthToken($response->token); } else { abort(403, 'Error: Auth Token Not Generated.');            }
            $show_otp = $payfast->customer_validate($request->all());
            $fetch = [
                'data_3ds_secureid' => $show_otp->getData()->response->data_3ds_secureid,
                'transaction_id' => $show_otp->getData()->response->transaction_id,
                'token' => json_decode($show_otp->getContent())->token
            ];
            Model::create(array_merge($request->all(), $fetch));
            $data_3ds_html = $show_otp->getData()->response->data_3ds_html;
            return $data_3ds_html;
        } catch(\Exception $e) {
            return response()->json(['message' => 'Error Processing your request.'],Response::HTTP_BAD_REQUEST);
        }

    }

Data Required For Initiate Transeaction

Once The call back is received then the required transaction is fetched from the database.

*** We can also Add the Transaction first and delete the requested transaction upon success. So that we can clean the database side by side.***

        $fetch = Model::where('transaction_id', $request->PaRes)->first();
        $pares = $request->PaRes;
        $payfast = new Payfast();
        $result = $payfast->setAuthToken($fetch->token);

        $data = [
            'user_id' => $fetch->user_id,
            'basket_id' => $fetch->basket_id,
            'txnamt' => $fetch->txnamt,
            'customer_mobile_no' => $fetch->customer_mobile_no,
            'customer_email_address' => $fetch->customer_email_address,
            'account_type_id' => $fetch->account_type_id,
            'account_title' => $fetch->account_title,
            'card_number' =>$fetch->card_number,
            'expiry_year' =>$fetch->expiry_year,
            'expiry_month' => $fetch->expiry_month,
            'cvv' => $fetch->cvv,
            'transaction_id' => $fetch->transaction_id,
            'data_3ds_secureid' => $fetch->data_3ds_secureid,
            'data_3ds_pares' => $pares,
            'store_id' => $fetch->store_id,
            'currency_code' => $fetch->currency_code,
        ];
        $result = $payfast->initiate_transaction($data);
        return response()->json([$result]);

Finalizing the Integration:

Once the payment form is generated, it can be displayed on the merchant's website. Merchants should ensure that the form is presented to customers at the appropriate stage of the checkout process. Additionally, the success and failure URLs should be specified to redirect customers to the appropriate pages after completing or canceling the payment.

Conclusion:

The Direct Checkout process for Payfast offers a secure and reliable solution for merchants to accept online payments. By following the steps outlined in this article and using the provided PHP code sample, merchants can seamlessly integrate Payfast into their websites and provide customers with a smooth and trustworthy payment experience.