Skip to content

Instantly share code, notes, and snippets.

@skeltonmod
Created August 9, 2021 08:35
Show Gist options
  • Save skeltonmod/94cad05742f9473bcfc66408a4371d60 to your computer and use it in GitHub Desktop.
Save skeltonmod/94cad05742f9473bcfc66408a4371d60 to your computer and use it in GitHub Desktop.
$paymentStatus = false;
$paymentResult = [];
$principalAmount = 0;
$surcharge = 0;
$order_gst = 0;
$discount = 0;
$order_vat = 0;
$gst = 0;
$vat = 0;
$taxExemption = false;
// Check order service type
if ($input['order_type'] == 'delivery') {
// Get highest delivery fee form menus
$delivery_fee = $this->menusForDeliveryFee($input->ordered_items, $input['venue_id'], $input['order_type']);
} else {
$delivery_fee = 0;
}
// Get Customer Number
$user = User::where('id', $input->attr->user_id[0])
->with('profile')
->first();
$accounts = $user->profile->attr->accounts;
$customerAccount = $this->getSelectedPaymentMethod($accounts);
if ($input->is_gcash == 0) {
if (empty($customerAccount)) {
$paymentResult['message'] = 'Payment card not found.';
$paymentResult['status'] = 422;
return $paymentResult;
}
}
$customerNumber = $customerAccount->account_id ?? '';
$paymentPerVenue['customerNumber'] = $customerNumber;
$principalAmount = $input['order_total'];
$isDiscountApplied = false;
// SURCHARGE CALCULATION
if ($input->multiple_order_id != null) {
// Calculate Surcharge of multiple order
$multiple_order = ShopOrder::where('multiple_order_id', $input->multiple_order_id)
->with(
'ordered_items',
'ordered_items.item',
'payment_transaction',
'promo_schedule.promo'
)
->get();
$surcharge = $this->getSurchargeOfMultipleOrders($multiple_order);
if ($input['promo_schedule'] != null) {
$isDiscountApplied = $this->isDiscountApplied($multiple_order);
// Discount for multiple orders
if (!$isDiscountApplied) {
$res['isDiscountApplied'] = $isDiscountApplied;
if ($input['promo_schedule']['promo']['discount_type'] == 'amount') {
$discount = $input['promo_schedule']['promo']['discount'];
} else {
$discount = $this->getDiscountOfMultipleorders($multiple_order, $surcharge);
}
}
}
} else {
// Calculate Surcharge of one order
$setting = AppSetting::where('setting', 'Tabletime Service Fee')
->where('country', $input->country)
->first();
$computation = empty($setting->value) ? [] : json_decode($setting->value);
if (count($computation) > 0) {
foreach ($computation as $key => $compute) {
if ($principalAmount >= $compute->min && $principalAmount <= $compute->max) {
if ($compute->type == 'amount') {
$surcharge = $compute->value;
} else {
$surcharge = ($compute->value / 100) * $principalAmount;
}
}
}
} else {
$surcharge = (2.95 / 100) * $principalAmount;
}
// Discount for single orders
if ($input['promo_schedule'] != null) {
if ($input['promo_schedule']['promo']['discount_type'] == 'amount') {
$discount = $input['promo_schedule']['promo']['discount'];
} else {
$tmpDiscount = $input['promo_schedule']['promo']['discount'] / 100;
$discount = $principalAmount * $tmpDiscount;
}
}
}
// Initial total amount
$newTotal = $principalAmount;
// Included discount to computation
$newTotal = $newTotal - $discount;
$surcharge = number_format($surcharge, 2, '.', '');
// Included surcharge to computation
$newTotal += floatval($surcharge);
// Formatted initial total amount
$paymentAmount = number_format($principalAmount, 2, '.', '');
// Include GST computation for Australia Server only
if ($input->attr->currency == 'AUD') {
$order_gst = $input['order_gst'];
$gst = number_format($order_gst, 2, '.', '');
} else {
$gst = 0;
}
if ($input->attr->currency == 'PHP') {
if ($input['tax_exemption'] == 0 || $input['tax_exemption'] == false) {
$vat = $input['order_vat'];
}
} else {
$vat = 0;
}
$taxSettings = AppSetting::where('setting', 'Tax')
->where('country', $input->country)
->first();
// Formatted Grand total amount
$formattedTotal = number_format(($newTotal + $delivery_fee), 2, '.', '');
if (in_array('payway', $payment_type) && $input->is_gcash == 0) {
$params = [
'principalAmount' => $formattedTotal,
'paymentAmount' => $paymentAmount,
'shop_id' => $input->shop_id,
'venue_id' => $input->venue_id,
'order_id' => $input->id,
'counter_id' => $input->counter_id,
'payee_id' => $input->attr->user_id[0], //To be updated when split bill is implemented
'item_id' => null, //To be updated
'customerNumber' => $customerNumber,
'perform-action' => 'payment-customer',
'surcharge' => $surcharge,
];
if ($input->attr->currency == 'AUD') {
$params['gst'] = $gst;
} else {
$params['gst'] = 0;
}
$paymentPerVenue = $this->performProcesses($params);
// SEND EMAIL RECEIPT, TEMPORARY IMPLEMENTATION
Mail::to($user->email)->queue(
new PatronTaxInvoiceMail(
$user,
$input,
$principalAmount,
$surcharge,
$formattedTotal,
$gst,
$input->attr->currency,
$input->country,
$vat,
$taxSettings->value
)
);
} elseif (in_array('stripe', $payment_type) && $input->is_gcash == 0) {
$params = [
'principalAmount' => $formattedTotal, //Total without Tabletim Fee
'paymentAmount' => $paymentAmount, // Total with fee and discount
'order_id' => $input->id,
'shop_id' => $input->shop_id,
'venue_id' => $input->venue_id,
'order_id' => $input->id,
'counter_id' => $input->counter_id,
'payee_id' => $input->attr->user_id[0], //To be updated when split bill is implemented
'item_id' => null, //To be updated
'customerNumber' => $customerNumber,
'surcharge' => $surcharge,
'currency' => $input->attr->currency,
'customer_ip' => @$input->attr->ip_address,
'isVoidable' => 0,
'isRefundable' => 0,
];
if ($input->attr->currency == 'AUD') {
$params['gst'] = $gst;
} else {
$params['gst'] = 0;
}
$stripeService = new StripeService;
$paymentPerVenue = $stripeService->purchase($params);
// // SEND EMAIL RECEIPT, TEMPORARY IMPLEMENTATION
if ($paymentPerVenue['status'] == 201 && @$customerAccount->email_receipt == true) {
Mail::to($user->email)->queue(
new PatronTaxInvoiceMail(
$user,
$input,
$principalAmount,
$surcharge,
$formattedTotal,
$gst,
$input->attr->currency,
$input->country,
$vat,
$taxSettings->value
)
);
}
} elseif ((in_array('fatzebra', $payment_type) || in_array('fatzebra-ph', $payment_type)) && $input->is_gcash == 0) {
$fat_zebra = new FatZebraService($input->country);
$params = [
'type' => 'perform-purchase-with-token',
'card_token' => @$customerNumber,
'principalAmount' => @$formattedTotal,
'paymentAmount' => @$paymentAmount,
'account_id' => @$customerNumber,
'surcharge' => @$surcharge,
'reference' => @$input->id,
'customer_ip' => @$input->attr->ip_address,
'currency' => @$input->attr->currency,
'item_id' => null,
'shop_id' => @$input->shop_id,
'venue_id' => @$input->venue_id,
'order_id' => @$input->id,
'counter_id' => @$input->counter_id,
'payee_id' => @$input->attr->user_id[0],
'isVoidable' => 0,
'isRefundable' => 0,
'delivery_fee' => $delivery_fee,
];
$paymentPerVenue['input'] = $input;
if ($input->attr->currency == 'AUD') {
$params['gst'] = $gst;
} else {
$params['gst'] = 0;
}
if ($input->attr->currency == 'PHP') {
if ($input['tax_exemption'] == 0 || $input['tax_exemption'] == false) {
$params['vat'] = $vat;
}
} else {
$params['vat'] = 0;
}
if (isset($promo) && !$isDiscountApplied) {
$params['promo_id'] = $promo['id'];
}
event(new ActivityLoggerEvent('Accept FZ payment params:', json_encode($params), $user->id));
$paymentPerVenue = $fat_zebra->fatZebraUtility($params);
if ( @$customerAccount->email_receipt == true){
Mail::to($user->email)->queue(
new PatronTaxInvoiceMail(
$user,
$input,
$principalAmount,
$surcharge,
$formattedTotal,
$gst,
$input->attr->currency,
$input->country,
$vat,
$taxSettings->value,
$delivery_fee
)
);
}
} elseif (in_array('gcash', $payment_type) && $input->is_gcash == 1) {
$paymentPerVenue['input'] = $input;
if ($input->attr->currency == 'AUD') {
$params['gst'] = $gst;
} else {
$params['gst'] = 0;
}
if ($input->attr->currency == 'PHP') {
if ($input['tax_exemption'] == 0 || $input['tax_exemption'] == false) {
$params['vat'] = $vat;
}
} else {
$params['vat'] = 0;
}
if (isset($promo) && !$isDiscountApplied) {
$params['promo_id'] = $promo['id'];
}
// SEND EMAIL RECEIPT, TEMPORARY IMPLEMENTATION
if (@$customerAccount->email_receipt == true) {
if (!$isDiscountApplied && $input['promo_schedule'] != null) {
$result['payment_total'] = $this->storePromoTransaction($input, $discount);
}
Mail::to($user->email)->queue(
new PatronTaxInvoiceMail(
$user,
$input,
$principalAmount,
$surcharge,
$formattedTotal,
$gst,
$input->attr->currency,
$input->country,
$vat,
$taxSettings->value
)
);
}
}
$paymentPerVenue['surcharge'] = $surcharge;
$paymentPerVenue['paymentAmount'] = $paymentAmount;
$paymentPerVenue['formattedTotal'] = $formattedTotal;
$paymentPerVenue['gst'] = $gst;
$paymentPerVenue['vat'] = $vat;
return $paymentPerVenue;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment