mirror of
				https://github.com/invoiceninja/invoiceninja.git
				synced 2025-11-04 00:07:35 -05:00 
			
		
		
		
	Merge pull request #3842 from beganovich/v2-2506-checkout-refunds
Checkout.com payments & refunds improvements
This commit is contained in:
		
						commit
						42f4d06d13
					
				@ -107,7 +107,6 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
 | 
				
			|||||||
        if ($request->has('token') && !is_null($request->token)) {
 | 
					        if ($request->has('token') && !is_null($request->token)) {
 | 
				
			||||||
            $method = new IdSource($state['token']);
 | 
					            $method = new IdSource($state['token']);
 | 
				
			||||||
            $payment = new CheckoutPayment($method, $state['currency']);
 | 
					            $payment = new CheckoutPayment($method, $state['currency']);
 | 
				
			||||||
            $payment->capture = false;
 | 
					 | 
				
			||||||
            $payment->amount = $state['value'];
 | 
					            $payment->amount = $state['value'];
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $method = new TokenSource($state['server_response']->cardToken);
 | 
					            $method = new TokenSource($state['server_response']->cardToken);
 | 
				
			||||||
@ -276,13 +275,31 @@ class CheckoutComPaymentDriver extends BasePaymentDriver
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
    public function refund(Payment $payment, $amount)
 | 
					    public function refund(Payment $payment, $amount)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $payment = new \Checkout\Models\Payments\Refund($payment->transaction_reference);
 | 
					        $this->init();
 | 
				
			||||||
        $payment->amount = $amount;
 | 
					
 | 
				
			||||||
 | 
					        $checkout_payment = new \Checkout\Models\Payments\Refund($payment->transaction_reference);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        try {
 | 
					        try {
 | 
				
			||||||
            $refund = $this->gateway->payments()->refund($payment);
 | 
					            $refund = $this->gateway->payments()->refund($checkout_payment);
 | 
				
			||||||
 | 
					            $checkout_payment = $this->gateway->payments()->details($refund->id);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            $response = ['refund_response' => $refund, 'checkout_payment_fetch' => $checkout_payment];
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					            return [
 | 
				
			||||||
 | 
					                'transaction_reference' => $refund->action_id,
 | 
				
			||||||
 | 
					                'transaction_response' => json_encode($response),
 | 
				
			||||||
 | 
					                'success' => $checkout_payment->status == 'Refunded',
 | 
				
			||||||
 | 
					                'description' => $checkout_payment->status,
 | 
				
			||||||
 | 
					                'code' => $checkout_payment->http_code,
 | 
				
			||||||
 | 
					            ];
 | 
				
			||||||
        } catch (CheckoutHttpException $e) {
 | 
					        } catch (CheckoutHttpException $e) {
 | 
				
			||||||
            // ..
 | 
					            return [
 | 
				
			||||||
 | 
					                'transaction_reference' => null,
 | 
				
			||||||
 | 
					                'transaction_response' => json_encode($e->getMessage()),
 | 
				
			||||||
 | 
					                'success' => false,
 | 
				
			||||||
 | 
					                'description' => $e->getMessage(),
 | 
				
			||||||
 | 
					                'code' => $e->getCode(),
 | 
				
			||||||
 | 
					            ];
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
				
			|||||||
@ -39,8 +39,6 @@ class RefundPayment
 | 
				
			|||||||
        $this->activity_repository = new ActivityRepository();
 | 
					        $this->activity_repository = new ActivityRepository();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    /**
 | 
					 | 
				
			||||||
     */
 | 
					 | 
				
			||||||
    public function run()
 | 
					    public function run()
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -51,7 +49,6 @@ class RefundPayment
 | 
				
			|||||||
            ->updateCreditables() //return the credits first
 | 
					            ->updateCreditables() //return the credits first
 | 
				
			||||||
            ->updatePaymentables() //update the paymentable items
 | 
					            ->updatePaymentables() //update the paymentable items
 | 
				
			||||||
            ->adjustInvoices()
 | 
					            ->adjustInvoices()
 | 
				
			||||||
            ->createActivity() // create the refund activity
 | 
					 | 
				
			||||||
            ->processGatewayRefund() //process the gateway refund if needed
 | 
					            ->processGatewayRefund() //process the gateway refund if needed
 | 
				
			||||||
            ->save();
 | 
					            ->save();
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
@ -70,19 +67,9 @@ class RefundPayment
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
                $this->payment->refunded = $this->total_refund;
 | 
					                $this->payment->refunded = $this->total_refund;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $activity = [
 | 
					                $this
 | 
				
			||||||
                    'payment_id' => $this->payment->id,
 | 
					                    ->createActivity($gateway)
 | 
				
			||||||
                    'user_id' => $this->payment->user->id,
 | 
					                    ->updateCreditNoteBalance();
 | 
				
			||||||
                    'company_id' => $this->payment->company->id,
 | 
					 | 
				
			||||||
                    'activity_type_id' => Activity::REFUNDED_PAYMENT,
 | 
					 | 
				
			||||||
                    'credit_id' => 1, // ???
 | 
					 | 
				
			||||||
                    'notes' => $response,
 | 
					 | 
				
			||||||
                ];
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                /** Persist activiy to database. */
 | 
					 | 
				
			||||||
                // $this->activity_repository->save($activity, ??);
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
                /** Substract credit amount from the refunded value. */
 | 
					 | 
				
			||||||
            }
 | 
					            }
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
            $this->payment->refunded += $this->total_refund;
 | 
					            $this->payment->refunded += $this->total_refund;
 | 
				
			||||||
@ -91,7 +78,21 @@ class RefundPayment
 | 
				
			|||||||
        return $this;
 | 
					        return $this;
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    private function createActivity()
 | 
					    public function updateCreditNoteBalance()
 | 
				
			||||||
 | 
					    {
 | 
				
			||||||
 | 
					        $this->credit_note->balance -= $this->total_refund;
 | 
				
			||||||
 | 
					        $this->credit_note->status_id = Credit::STATUS_APPLIED;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->credit_note->balance === 0
 | 
				
			||||||
 | 
					            ? $this->credit_note->status_id = Credit::STATUS_APPLIED
 | 
				
			||||||
 | 
					            : $this->credit_note->status_id = Credit::STATUS_PARTIAL;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        $this->credit_note->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        return $this;
 | 
				
			||||||
 | 
					    }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    private function createActivity($notes)
 | 
				
			||||||
    {
 | 
					    {
 | 
				
			||||||
        $fields = new \stdClass;
 | 
					        $fields = new \stdClass;
 | 
				
			||||||
        $activity_repo = new ActivityRepository();
 | 
					        $activity_repo = new ActivityRepository();
 | 
				
			||||||
@ -101,6 +102,7 @@ class RefundPayment
 | 
				
			|||||||
        $fields->company_id = $this->payment->company_id;
 | 
					        $fields->company_id = $this->payment->company_id;
 | 
				
			||||||
        $fields->activity_type_id = Activity::REFUNDED_PAYMENT;
 | 
					        $fields->activity_type_id = Activity::REFUNDED_PAYMENT;
 | 
				
			||||||
        $fields->credit_id = $this->credit_note->id;
 | 
					        $fields->credit_id = $this->credit_note->id;
 | 
				
			||||||
 | 
					        $fields->notes = json_encode($notes);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        if (isset($this->refund_data['invoices'])) {
 | 
					        if (isset($this->refund_data['invoices'])) {
 | 
				
			||||||
            foreach ($this->refund_data['invoices'] as $invoice) {
 | 
					            foreach ($this->refund_data['invoices'] as $invoice) {
 | 
				
			||||||
@ -264,6 +266,8 @@ class RefundPayment
 | 
				
			|||||||
                    $invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
 | 
					                    $invoice->service()->setStatus(Invoice::STATUS_PARTIAL);
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					                $invoice->save();
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $client = $invoice->client;
 | 
					                $client = $invoice->client;
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                $adjustment_amount += $refunded_invoice['amount'];
 | 
					                $adjustment_amount += $refunded_invoice['amount'];
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user