1

我目前正在为Eway做一个PHP支付网关接口,接口包括:

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());

}

我能够完成除 handleRecurringPayment 功能之外的所有其他功能,该功能应该在处理每次定期付款时从 Eway 获得静默帖子并获得状态,如失败或成功,以便我可以更新我们这边的信息。

现在,我已经用 Stripe API 完成了这个,见下文,因为他们的API 文档中的事件对象非常清楚,你也可以在下面看到我的代码:

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;
}

浏览网站后,除了这篇文章外,我没有发现任何与我要查找的内容相关的问题,在那篇文章中我不太明白“您可以将用户的卡详细信息作为代币存储在 eWAY 中,然后在您的应用程序。”,我认为该帖子中的链接也已损坏。

根据我对流程的理解:首先在 Eway 上创建代币客户 -> 使用该代币客户创建重新计费客户 -> 使用重新计费客户创建重新计费事件。

我试过给他们发邮件,他们说如果你不在澳大利亚,我们就帮不了你。现在我真的不知道从哪里开始。也许 Eway 只是没有针对每次定期付款的静默帖子或通知?或者我在构建一个重复的 biling 事件时错过了什么?

由于重复计费方法只允许在 SOAP 中,我的代码真的很长,所以我没有附加其他功能的代码。抱歉,我对支付网关和 PHP 很陌生。

4

0 回答 0