Eway recurring payment notification silent post

312 Views Asked by At

I am currently doing a PHP payment gateway interface for Eway, the interface includes:

interface RecurringPaymentGateway
{
/**
 * save subscription information to database
 *
 * @param Subscription $subscription
 * @return bool returns true on success, false on failure
 */
public function saveToDb(Subscription $subscription);

/**
 * create new subscription
 *
 * @param Subscription $subscription
 * @return string|bool returns gateway id on success, false on failure
 */
public function createSubscription(Subscription $subscription);

/**
 * update existing subscription
 *
 * @param Subscription $subscription
 * @param Subscription $existingSubscription
 * @return mixed returns true on success, false on failure
 */
public function updateSubscription(Subscription $subscription, Subscription $existingSubscription);

/**
 * update only payment method for an existing subscription
 *
 * @param Subscription $subscription
 * @return bool returns true on success, false on failure
 */
public function updateSubscriptionPayment(Subscription $subscription);

/**
 * cancel existing subscription
 *
 * @param Subscription $subscription
 * @return bool returns true on success, false on failure
 */
public function cancelSubscription(Subscription $subscription);

/**
 * handles an recurring payment pingback
 *
 * @param array $data POST data from webhook / silent post
 * @return PaymentResponse
 */
public function handleRecurringPayment($data = array());

}

I was able to finished all other functions except for the handleRecurringPayment function, the function should get silent post from Eway when each recurring payment is processed and get a status, like failed or success so I can update information on our side.

Now, I have done this with Stripe API see below, because it was very clear of the event object in their API doc also you can see my code below:

public function handleRecurringPayment($data = array())
{
    $response = new PaymentResponse();

    // validate the incoming event
    $event = StripeEvent::retrieve($data['id']);

    // failed to validate event, ignore event
    if ($event === false) {
        $response->setStatus('invalid_event');
        return $response;
    }

    $event_type = $event->type;

    if ($event_type != 'invoice.payment_succeeded' && $event_type != 'invoice.payment_failed') {
        // those are the only 2 events that we are interested in, other events can be ignored
        $response->setStatus('invalid_event');
        return $response;
    }

    /** @var Subscription $subscription */
    $subscriptionId = $event->data->object->subscription;
    $subscription = $this->getSubscriptionFromId($subscriptionId);

    // subscription doesn't exist in database, ignore this event
    if ($subscription === false || $subscription->id <= 0) {
        $response->setStatus('not_found');
        return $response;
    }

    $response->setSubscription($subscription);

    if ($event_type == 'invoice.payment_failed') {
        /**
         * payment error
         */
        $chargeId = $event->data->object->charge;
        $charge = Charge::retrieve($chargeId);

        $errorMessage = $charge->failure_message;

        // need to return a response object
        $response->setStatus('payment_failed');
        $response->setMessage($errorMessage);

        $subscription->error = true;
        $subscription->errorMessage = $errorMessage;

        // next payment attempt
        $nextAttempt = $event->data->object->next_payment_attempt;

        if ((int)$nextAttempt > 0) {
            $nextAttemptDate = \DateTime::createFromFormat('U', (int)$nextAttempt);
            $subscription->nextPayment = $nextAttemptDate;
        }

        $subscription->update();
        $response->setSubscription($subscription);

        return $response;
    }

    if ($event_type == 'customer.subscription.deleted') {
        /**
         * subscription cancelled
         */
        $subscription->enabled = false;
        $subscription->update();

        $response->setSubscription($subscription);
        $response->setStatus('cancelled');
        return $response;
    }

    // get stripe subscription
    $customer = StripeCustomer::retrieve($event->data->object->customer);
    $stripeSubscription = $customer->subscriptions->retrieve($event->data->object->subscription);

    // update next payment date
    $nextPayment = $stripeSubscription->current_period_end;

    if ((int)$nextPayment > 0) {
        $nextPaymentDate = \DateTime::createFromFormat('U', (int)$nextPayment);
        $subscription->nextPayment = $nextPaymentDate;
        $subscription->update();

        $response->setSubscription($subscription);
    }

    // check subscription status
    if ($stripeSubscription->status == 'trialing') {
        $response->setStatus('invalid_event');
        return $response;
    }

    // update previous payment date to now
    $subscription->prevPayment = new \DateTime();
    $subscription->error = false;
    $subscription->errorMessage = '';
    $subscription->update();

    $response->setSubscription($subscription);
    $response->setStatus('payment_success');
    $response->setTransactionId($event->data->object->charge);

    return $response;
}

After looking around the site, I have not found any related questions to what I am looking for except for this post, in that post i did not quite understand "you could store your user's card details as Tokens in eWAY, then manage the scheduling in your application.", also the link in that post is broken i think.

From what I understand of the process is: First create token customer on Eway -> create rebill customer using that token customer -> create rebill event using rebill customer.

I have tried to email them , they said if you are not in AU we can not help you. now i really don't know where to start. Maybe Eway just don't have a silent post or notification for each recurrping payments ? Or did I miss something when construct a recurring biliting event ?

Since recurring billing method only allowed in SOAP, my codes are really really long, so i did not attach codes for other functions. Sorry I am very new to paymentgateways and PHP in general.

0

There are 0 best solutions below