From 94ebf95caa576e971ddc692bcf463d21d9b1f15d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Benjamin=20Beganovi=C4=87?= Date: Fri, 18 Sep 2020 14:35:53 +0200 Subject: [PATCH] Stripe use SDK to refund --- app/PaymentDrivers/StripePaymentDriver.php | 46 ++++++++++++++-------- 1 file changed, 29 insertions(+), 17 deletions(-) diff --git a/app/PaymentDrivers/StripePaymentDriver.php b/app/PaymentDrivers/StripePaymentDriver.php index b41eb9acfe..1c221535c7 100644 --- a/app/PaymentDrivers/StripePaymentDriver.php +++ b/app/PaymentDrivers/StripePaymentDriver.php @@ -49,10 +49,14 @@ class StripePaymentDriver extends BasePaymentDriver public $can_authorise_credit_card = true; + /** @var \Stripe\StripeClient */ + protected $stripe; + protected $customer_reference = 'customerReferenceParam'; protected $payment_method; + public static $methods = [ GatewayType::CREDIT_CARD => CreditCard::class, GatewayType::BANK_TRANSFER => ACH::class, @@ -81,6 +85,10 @@ class StripePaymentDriver extends BasePaymentDriver */ public function init(): void { + $this->stripe = new \Stripe\StripeClient( + $this->company_gateway->getConfigField('apiKey') + ); + Stripe::setApiKey($this->company_gateway->getConfigField('apiKey')); } @@ -313,36 +321,40 @@ class StripePaymentDriver extends BasePaymentDriver public function refund(Payment $payment, $amount) { - $this->gateway(); + $this->init(); - $response = $this->gateway - ->refund(['transactionReference' => $payment->transaction_reference, 'amount' => $amount, 'currency' => $payment->client->getCurrencyCode()]) - ->send(); + $response = $this->stripe + ->refunds + ->create(['charge' => $payment->transaction_reference, 'amount' => $amount]); - if ($response->isSuccessful()) { - SystemLogger::dispatch([ - 'server_response' => $response->getMessage(), 'data' => request()->all(), + // $response = $this->gateway + // ->refund(['transactionReference' => $payment->transaction_reference, 'amount' => $amount, 'currency' => $payment->client->getCurrencyCode()]) + // ->send(); + + info($response); + + if ($response->status == $response::STATUS_SUCCEEDED) { + SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(), ], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_SUCCESS, SystemLog::TYPE_STRIPE, $this->client); return [ - 'transaction_reference' => $response->getData()['id'], - 'transaction_response' => json_encode($response->getData()), - 'success' => $response->getData()['refunded'], - 'description' => $response->getData()['description'], - 'code' => $response->getCode(), + 'transaction_reference' => $response->charge, + 'transaction_response' => json_encode($response), + 'success' => $response->status == $response::STATUS_SUCCEEDED ? true : false, + 'description' => $response->metadata, + 'code' => $response, ]; } - SystemLogger::dispatch([ - 'server_response' => $response->getMessage(), 'data' => request()->all(), + SystemLogger::dispatch(['server_response' => $response, 'data' => request()->all(), ], SystemLog::CATEGORY_GATEWAY_RESPONSE, SystemLog::EVENT_GATEWAY_FAILURE, SystemLog::TYPE_STRIPE, $this->client); return [ 'transaction_reference' => null, - 'transaction_response' => json_encode($response->getData()), + 'transaction_response' => json_encode($response), 'success' => false, - 'description' => $response->getData()['error']['message'], - 'code' => $response->getData()['error']['code'], + 'description' => $response->failure_reason, + 'code' => 422, ]; }