/** * @param string $action */ private function processBills($action, array $bills) { foreach ($bills as $bill) { $existingPayment = $this->paymentRepository->getPaymentBySourceId($bill['id']); if ($existingPayment) { if ($bill['status'] == 'failed' || $bill['status'] == 'cancelled') { //Payment failed or cancelled - either way we don't have the money! //We need to retrieve the payment from the user somehow but don't want to cancel the subscription. //$this->handleFailedCancelledBill($existingPayment); $this->paymentRepository->recordPaymentFailure($existingPayment->id, $bill['status']); } elseif ($bill['status'] == 'pending' && $action == 'retried') { //Failed payment is being retried $subCharge = $this->subscriptionChargeRepository->getById($existingPayment->reference); if ($subCharge) { if ($subCharge->amount == $bill['amount']) { $this->subscriptionChargeRepository->markChargeAsProcessing($subCharge->id); } else { //@TODO: Handle partial payments \Log::warning("Sub charge handling - gocardless partial payment"); } } } elseif ($bill['status'] == 'refunded') { //Payment refunded //Update the payment record and possible the user record } elseif ($bill['status'] == 'withdrawn') { //Money taken out - not our concern } } else { \Log::info("GoCardless Webhook received for unknown payment: " . $bill['id']); } } }
private function updateSubPayment($paymentId, $userId, $status) { $payment = $this->paymentRepository->getById($paymentId); $subCharge = $this->subscriptionChargeRepository->findCharge($userId); if (!$subCharge) { \Log::warning('Subscription payment without a sub charge. Payment ID:' . $paymentId); return; } //The sub charge record id gets saved onto the payment if (empty($payment->reference)) { $payment->reference = $subCharge->id; $payment->save(); } else { if ($payment->reference != $subCharge->id) { throw new PaymentException('Attempting to update sub charge (' . $subCharge->id . ') but payment (' . $payment->id . ') doesn\'t match. Sub charge has an existing reference on it.'); } } if ($status == 'paid') { $this->subscriptionChargeRepository->markChargeAsPaid($subCharge->id); } else { if ($status == 'pending') { $this->subscriptionChargeRepository->markChargeAsProcessing($subCharge->id); } } //The amount isn't stored on the sub charge record until its paid or processing if ($payment->amount != $subCharge->amount) { $this->subscriptionChargeRepository->updateAmount($subCharge->id, $payment->amount); } }