<commit_before><?php
/**
 * Created by PhpStorm.
 * User: rbsl-neelam
 * Date: 25/8/16
 * Time: 3:06 PM
 */

namespace Laravel\Cashier\Helpers;

use Illuminate\Support\Facades\Event;
use Laravel\Cashier\Invoice;
use Laravel\Cashier\Wallet;

class WalletHelper
{
    protected $refund_support   = false;

    public function payCart($request, $user, $cart)
    {
        try{
            // get the amount to be paid
            $amount = $request->get("amount");

            // get the group_id of wallet used
            $group_id = $request->get("vod-payment-wallet-gid");

            // get the wallet to be used for transaction
            $wallet = Wallet::where("user_id", $user->id)
                ->where("group_id", $group_id)
                ->first();

            // payment_details
            $payment_details                = json_decode($wallet->payment_details, true);
            $payment_details["wallet_type"] = $group_id == NO_GROUP ? "self" : "group";
            $payment_details["group_id"]    = $group_id;

            // create invoice
            $invoice = Invoice::createInvoice($user, $amount, $cart->id);

            // create transaction
            $txn = $invoice->createTransaction(PROCESSOR_WALLET, $payment_details);

            // make payment
            $status = $this->processTransaction($invoice, $wallet, $txn);

            if ($status == INVOICE_STATUS_PAID) {
                // update Cart
                $cart->updateStatus(CART_STATUS_PAID, $group_id, $txn);
            }

            return [
                "status"     => true,
                "invoice_id" => $invoice->id,
            ];
        } catch(\Exception $e){
            return [
                "status"  => false,
                "message" => $e->getMessage(),
            ];
        }
    }

    public function processTransaction($invoice, $wallet, $txn)
    {
        // make payment
        try {
            $wallet->balance = $wallet->balance - $invoice->total;
            $wallet->save();

            $txn->updateStatus(TRANSACTION_STATUS_PAYMENT_COMPLETE);
            $invoice->markPaid();
        } catch (\Exception $e) {
            $txn->updateStatus(TRANSACTION_STATUS_PAYMENT_FAILED);
        }

        return $invoice->status;
    }

    public function getUserPaymentDetails($user_id, $for_checkout = false)
    {
        // we don't need to display any payment details for the user for allowing payments through wallet
        return [];
    }

    // return the possible gateway transaction fees if this amount had been paid through Stripe
    public static function getTransactionFees($processor, $amount)
    {
        // when payment is done through wallet no extra fees would be deducted afterwards
        return 0;
    }

    public function isRefundSupported()
    {
        return $this->refund_support;
    }
}
<commit_msg>Task::Handle group wallet updation<commit_after><?php
/**
 * Created by PhpStorm.
 * User: rbsl-neelam
 * Date: 25/8/16
 * Time: 3:06 PM
 */

namespace Laravel\Cashier\Helpers;

use App\User;
use App\vod\model\Group;
use Illuminate\Support\Facades\Event;
use Laravel\Cashier\Invoice;
use Laravel\Cashier\Wallet;

class WalletHelper
{
    protected $refund_support   = false;

    public function payCart($request, $user, $cart)
    {
        try{
            // get the amount to be paid
            $amount = $request->get("amount");

            // get the group_id of wallet used
            $group_id = $request->get("vod-payment-wallet-gid");

            // get the wallet to be used for transaction
            $wallet = Wallet::where("user_id", $user->id)
                ->where("group_id", $group_id)
                ->first();

            // get the group member detail who used wallet
            $group         = Group::where("id", "=", $group_id)->first();

            $user_obj      = new User(null, env("DB_TENANT_CONNECTION"));

            $g_member      = $user_obj->where("id", $wallet->user_id)->first();

            // payment_details
            $payment_details                = json_decode($wallet->payment_details, true);
            $payment_details["wallet_type"] = $group_id == NO_GROUP ? "self" : "group";
            $payment_details["group_id"]    = $group_id;

            // create invoice
            $invoice = Invoice::createInvoice($user, $amount, $cart->id);

            // create transaction
            $txn = $invoice->createTransaction(PROCESSOR_WALLET, $payment_details);

            // make payment
            $status = $this->processTransaction($invoice, $wallet, $txn, $group, $g_member);

            if ($status == INVOICE_STATUS_PAID) {
                // update Cart
                $cart->updateStatus(CART_STATUS_PAID, $group_id, $txn);
            }

            return [
                "status"     => true,
                "invoice_id" => $invoice->id,
            ];
        } catch(\Exception $e){
            return [
                "status"  => false,
                "message" => $e->getMessage(),
            ];
        }
    }

    public function processTransaction($invoice, $wallet, $txn, $group, $g_member)
    {
        // make payment
        try {
            $wallet->balance         = $wallet->balance - $invoice->total;
            $wallet->consumed_amount = $wallet->consumed_amount + $invoice->total;
            $wallet->save();

            // update group member wallet
            $g_member_array = array_values(json_decode($group->members, TRUE));
            for($i = 0; $i < count($g_member_array); $i++){

                if($g_member->email == $g_member_array[$i]['email']){
                    $members                   = [
                        'email'                => $g_member_array[$i]['email'],
                        'limit'                => $g_member_array[$i]['limit'] - $invoice->total,
                        'is_admin'             => $g_member_array[$i]['is_admin'],
                    ];

                    unset($g_member_array[$i]);
                    $g_member_array[$i] = $members;
                }
            }
            $group->update(['members' => json_encode($g_member_array)]);

            $txn->updateStatus(TRANSACTION_STATUS_PAYMENT_COMPLETE);
            $invoice->markPaid();
        } catch (\Exception $e) {
            $txn->updateStatus(TRANSACTION_STATUS_PAYMENT_FAILED);
        }

        return $invoice->status;
    }

    public function getUserPaymentDetails($user_id, $for_checkout = false)
    {
        // we don't need to display any payment details for the user for allowing payments through wallet
        return [];
    }

    // return the possible gateway transaction fees if this amount had been paid through Stripe
    public static function getTransactionFees($processor, $amount)
    {
        // when payment is done through wallet no extra fees would be deducted afterwards
        return 0;
    }

    public function isRefundSupported()
    {
        return $this->refund_support;
    }
}

{'fraction_non_alphanumeric': 0.12372013651877133, 'fraction_numerical': 0.00270193401592719, 'mean_word_length': 1.8347440548166063, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 68, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5974770', 'n_tokens_mistral': 2026, 'n_tokens_neox': 1972, 'n_words': 437}