jeudi 31 décembre 2020

Laravel scheduler to charge users recurring payments via peach payment

I have a project that uses Ionic and Laravel as a backend. The user uses our app based on monthly or annual subscriptions. So far I have been successful in implementing the initial charge of the subscriptions via peach payment.

So now I want every month end to charge the recurring monthly or annual subscriptions.

The idea I have is as follow:

  1. Run Scheduler to check subscriptions table for all monthly active users where the expiry date is month-end (31/12/2020) or is the current month.
  2. Generate transactions for all those users and save them to the billing table ( this is to get transaction_id to send to a payment gateway).
  3. Run the scheduler using the billing info where created_at is today, send each transaction to payment gateway api for processing.
  4. On Api response get status of each payment request and save/update the db billing table using transaction_id as primaryKey.
  5. Update the subscriptions table (status, new expiry date) after the billing table is update

Any help or direction with this will be appreciated. The code below is the code to use for sending a single charge request.

/**
     * Monthly Subscriptions
     * @param Request $request
     */
    public function chargeMonthlySubscription(Request $request)
    {
        $curl = curl_init();
        $url = "$this->url/registrations/$request->registrationId/payments";
        $data = "entityId=$this->entityId" .
            "&amount=$request->subscription_amount" .
            "&currency=ZAR" .
            "&customer.merchantCustomerId=$request->subscription_no" .
            "&customer.givenName=$request->first_name" .
            "&customer.surname=$request->last_name" .
            "&recurringType=$request->recurring_type" .
            "&paymentType=DB";

        curl_setopt_array($curl, array(
            CURLOPT_URL => "$url",
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => "",
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => "POST",
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => array(
                $this->token,
                "Content-Type: application/x-www-form-urlencoded"
            ),
        ));

        $response = curl_exec($curl);

        curl_close($curl);
        echo $response;

    }



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire