Stripe refunds

This commit is contained in:
Benjamin Beganović 2020-06-24 16:07:12 +02:00
parent 4d2d41123b
commit f6f999b801

View File

@ -1,4 +1,5 @@
<?php <?php
/** /**
* Invoice Ninja (https://invoiceninja.com) * Invoice Ninja (https://invoiceninja.com)
* *
@ -73,7 +74,7 @@ class StripePaymentDriver extends BasePaymentDriver
* Initializes the Stripe API * Initializes the Stripe API
* @return void * @return void
*/ */
public function init() :void public function init(): void
{ {
Stripe::setApiKey($this->company_gateway->getConfigField('apiKey')); Stripe::setApiKey($this->company_gateway->getConfigField('apiKey'));
} }
@ -90,7 +91,7 @@ class StripePaymentDriver extends BasePaymentDriver
/** /**
* Returns the gateway types * Returns the gateway types
*/ */
public function gatewayTypes() :array public function gatewayTypes(): array
{ {
$types = [ $types = [
GatewayType::CREDIT_CARD, GatewayType::CREDIT_CARD,
@ -215,13 +216,13 @@ class StripePaymentDriver extends BasePaymentDriver
+"shipping": null +"shipping": null
+"source": null +"source": null
+"status": "succeeded" +"status": "succeeded"
*/ */
public function processPaymentResponse($request) //We never have to worry about unsuccessful payments as failures are handled at the front end for this driver. public function processPaymentResponse($request) //We never have to worry about unsuccessful payments as failures are handled at the front end for this driver.
{ {
return $this->payment_method->paymentResponse($request); return $this->payment_method->paymentResponse($request);
} }
public function createPayment($data, $status = Payment::STATUS_COMPLETED) :Payment public function createPayment($data, $status = Payment::STATUS_COMPLETED): Payment
{ {
$payment = parent::createPayment($data, $status); $payment = parent::createPayment($data, $status);
@ -243,7 +244,7 @@ class StripePaymentDriver extends BasePaymentDriver
* @param array $data The data array to be passed to Stripe * @param array $data The data array to be passed to Stripe
* @return PaymentIntent The Stripe payment intent object * @return PaymentIntent The Stripe payment intent object
*/ */
public function createPaymentIntent($data) :?\Stripe\PaymentIntent public function createPaymentIntent($data): ?\Stripe\PaymentIntent
{ {
$this->init(); $this->init();
@ -256,7 +257,7 @@ class StripePaymentDriver extends BasePaymentDriver
* *
* @return \Stripe\SetupIntent * @return \Stripe\SetupIntent
*/ */
public function getSetupIntent() :\Stripe\SetupIntent public function getSetupIntent(): \Stripe\SetupIntent
{ {
$this->init(); $this->init();
@ -268,7 +269,7 @@ class StripePaymentDriver extends BasePaymentDriver
* Returns the Stripe publishable key * Returns the Stripe publishable key
* @return NULL|string The stripe publishable key * @return NULL|string The stripe publishable key
*/ */
public function getPublishableKey() :?string public function getPublishableKey(): ?string
{ {
return $this->company_gateway->getPublishableKey(); return $this->company_gateway->getPublishableKey();
} }
@ -278,7 +279,7 @@ class StripePaymentDriver extends BasePaymentDriver
* *
* @return NULL|\Stripe\Customer A Stripe customer object * @return NULL|\Stripe\Customer A Stripe customer object
*/ */
public function findOrCreateCustomer() :?\Stripe\Customer public function findOrCreateCustomer(): ?\Stripe\Customer
{ {
$customer = null; $customer = null;
@ -311,36 +312,34 @@ class StripePaymentDriver extends BasePaymentDriver
$this->gateway(); $this->gateway();
$response = $this->gateway $response = $this->gateway
->refund(['transactionReference'=>$payment->transaction_reference, 'amount' => $amount, 'currency' => $payment->client->getCurrencyCode()]) ->refund(['transactionReference' => $payment->transaction_reference, 'amount' => $amount, 'currency' => $payment->client->getCurrencyCode()])
->send(); ->send();
if ($response->isSuccessful()) { if ($response->isSuccessful()) {
SystemLogger::dispatch( SystemLogger::dispatch([
[ 'server_response' => $response->getMessage(), 'data' => request()->all(),
'server_response' => $response->getMessage(), ], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->client);
'data' => request()->all(),
],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_SUCCESS,
SystemLog::TYPE_PAYPAL,
$this->client
);
return true; return [
'transaction_reference' => $response->getData()['id'],
'transaction_response' => json_encode($response->getData()),
'success' => $response->getData()['refunded'],
'description' => $response->getData()['description'],
'code' => $response->getCode(),
];
} }
SystemLogger::dispatch( SystemLogger::dispatch([
[ 'server_response' => $response->getMessage(), 'data' => request()->all(),
'server_response' => $response->getMessage(), ], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client);
'data' => request()->all(),
],
SystemLog::CATEGORY_GATEWAY_RESPONSE,
SystemLog::EVENT_GATEWAY_FAILURE,
SystemLog::TYPE_PAYPAL,
$this->client
);
return false; return [
'transaction_reference' => null,
'transaction_response' => json_encode($response->getData()),
'success' => false,
'description' => $response->getData()['error']['message'],
'code' => $response->getData()['error']['code'],
];
} }
public function verificationView(ClientGatewayToken $payment_method) public function verificationView(ClientGatewayToken $payment_method)