Fixes for stripe payment currency amount

This commit is contained in:
David Bomba 2021-06-30 13:56:11 +10:00
parent 9a63a9c44d
commit 7f13a79623
8 changed files with 28 additions and 58 deletions

View File

@ -63,50 +63,11 @@ class ZipInvoices implements ShouldQueue
/**
* Execute the job.
*
*
* @return void
* @throws \ZipStream\Exception\FileNotFoundException
* @throws \ZipStream\Exception\FileNotReadableException
* @throws \ZipStream\Exception\OverflowException
*/
// public function handle()
// {
// $tempStream = fopen('php://memory', 'w+');
// $options = new Archive();
// $options->setOutputStream($tempStream);
// // create a new zipstream object
// $file_name = date('Y-m-d').'_'.str_replace(' ', '_', trans('texts.invoices')).'.zip';
// $invoice = $this->invoices->first();
// $invitation = $invoice->invitations->first();
// $path = $invoice->client->invoice_filepath($invitation);
// $zip = new ZipStream($file_name, $options);
// foreach ($this->invoices as $invoice) {
// //$zip->addFileFromPath(basename($invoice->pdf_file_path()), TempFile::path($invoice->pdf_file_path()));
// $zip->addFileFromPath(basename($invoice->pdf_file_path($invitation)), $invoice->pdf_file_path());
// }
// $zip->finish();
// Storage::disk('public')->put($path.$file_name, $tempStream);
// fclose($tempStream);
// $nmo = new NinjaMailerObject;
// $nmo->mailable = new DownloadInvoices(Storage::disk('public')->url($path.$file_name), $this->company);
// $nmo->to_user = $this->user;
// $nmo->settings = $this->settings;
// $nmo->company = $this->company;
// NinjaMailerJob::dispatch($nmo);
// UnlinkFile::dispatch('public', $path.$file_name)->delay(now()->addHours(1));
// }
public function handle()
{
@ -145,8 +106,6 @@ class ZipInvoices implements ShouldQueue
NinjaMailerJob::dispatch($nmo);
nlog("sending email");
UnlinkFile::dispatch(config('filesystems.default'), $path.$file_name)->delay(now()->addHours(1));
}
}

View File

@ -128,7 +128,7 @@ class ACH
$data['currency'] = $this->stripe->client->getCurrencyCode();
$data['payment_method_id'] = GatewayType::BANK_TRANSFER;
$data['customer'] = $this->stripe->findOrCreateCustomer();
$data['amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision);
$data['amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency());
return render('gateways.stripe.ach.pay', $data);
}
@ -151,7 +151,7 @@ class ACH
$state = [
'payment_method' => $request->payment_method_id,
'gateway_type_id' => $request->company_gateway_id,
'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision),
'amount' => $this->stripe->convertToStripeAmount($request->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'currency' => $request->currency,
'customer' => $request->customer,
];
@ -196,7 +196,7 @@ class ACH
$data = [
'payment_method' => $state['source'],
'payment_type' => PaymentType::ACH,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->amount, $this->stripe->client->currency()->precision),
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => $state['charge']->id,
'gateway_type_id' => GatewayType::BANK_TRANSFER,
];

View File

@ -37,7 +37,7 @@ class Alipay
$data['gateway'] = $this->stripe;
$data['return_url'] = $this->buildReturnUrl();
$data['currency'] = $this->stripe->client->getCurrencyCode();
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision);
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency());
$data['invoices'] = $this->stripe->payment_hash->invoices();
$this->stripe->payment_hash->data = array_merge((array) $this->stripe->payment_hash->data, ['stripe_amount' => $data['stripe_amount']]);
@ -74,7 +74,7 @@ class Alipay
$data = [
'payment_method' => $this->stripe->payment_hash->data->source,
'payment_type' => PaymentType::ALIPAY,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision),
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => $source,
'gateway_type_id' => GatewayType::ALIPAY,
@ -104,7 +104,7 @@ class Alipay
$this->stripe->client,
$server_response,
$this->stripe->client->company,
$this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision)
$this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency())
);
$message = [

View File

@ -68,7 +68,7 @@ class Charge
try {
$data = [
'amount' => $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision),
'amount' => $this->stripe->convertToStripeAmount($amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'currency' => $this->stripe->client->getCurrencyCode(),
'payment_method' => $cgt->token,
'customer' => $cgt->gateway_customer_reference,

View File

@ -60,7 +60,7 @@ class CreditCard
public function paymentView(array $data)
{
$payment_intent_data = [
'amount' => $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision),
'amount' => $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'currency' => $this->stripe->client->getCurrencyCode(),
'customer' => $this->stripe->findOrCreateCustomer(),
'description' => ctrans('texts.invoices') . ': ' . collect($data['invoices'])->pluck('invoice_number'), // TODO: More meaningful description.
@ -115,7 +115,7 @@ class CreditCard
$data = [
'payment_method' => $this->stripe->payment_hash->data->server_response->payment_method,
'payment_type' => PaymentType::parseCardType(strtolower($stripe_method->card->brand)),
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->server_response->amount, $this->stripe->client->currency()->precision),
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->server_response->amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => optional($this->stripe->payment_hash->data->payment_intent->charges->data[0])->id,
'gateway_type_id' => GatewayType::CREDIT_CARD,
];

View File

@ -40,7 +40,7 @@ class SOFORT
{
$data['gateway'] = $this->stripe;
$data['return_url'] = $this->buildReturnUrl();
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision);
$data['stripe_amount'] = $this->stripe->convertToStripeAmount($data['total']['amount_with_fee'], $this->stripe->client->currency()->precision, $this->stripe->client->currency());
$data['client'] = $this->stripe->client;
$data['country'] = $this->stripe->client->country->iso_3166_2;
@ -80,7 +80,7 @@ class SOFORT
$data = [
'payment_method' => $this->stripe->payment_hash->data->source,
'payment_type' => PaymentType::SOFORT,
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision),
'amount' => $this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency()),
'transaction_reference' => $source,
'gateway_type_id' => GatewayType::SOFORT,
];
@ -107,7 +107,7 @@ class SOFORT
$this->stripe->client,
$server_response,
$this->stripe->client->company,
$this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision)
$this->stripe->convertFromStripeAmount($this->stripe->payment_hash->data->stripe_amount, $this->stripe->client->currency()->precision, $this->stripe->client->currency())
);
$message = [

View File

@ -14,13 +14,24 @@ namespace App\PaymentDrivers\Stripe;
trait Utilities
{
public function convertFromStripeAmount($amount, $precision)
/*Helpers for currency conversions, NOTE* for some currencies we need to change behaviour */
public function convertFromStripeAmount($amount, $precision, $currency)
{
if($currency->code == "JPY")
return $amount;
return $amount / pow(10, $precision);
}
public function convertToStripeAmount($amount, $precision)
public function convertToStripeAmount($amount, $precision, $currency)
{
return (int)($amount * pow(10, $precision));
if($currency->code == "JPY")
return $amount;
return round(($amount * pow(10, $precision)),0);
}
}

View File

@ -337,7 +337,7 @@ class StripePaymentDriver extends BaseDriver
try {
$response = $this->stripe
->refunds
->create(['charge' => $payment->transaction_reference, 'amount' => $this->convertToStripeAmount($amount, $this->client->currency()->precision)], $meta);
->create(['charge' => $payment->transaction_reference, 'amount' => $this->convertToStripeAmount($amount, $this->client->currency()->precision, $this->client->currency())], $meta);
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, $this->client->company);