---
# System prepended metadata

title: 綠界金流(參考SDK版)
tags: [金流]

---

# 綠界金流(參考SDK版)

###### tags: `金流`

## 參考連結

[金流應用](https://www.ecpay.com.tw/Service/API_Dwnld)

___

```php=
    public function infomationCheck(Request $request)
    {

        $user = Auth::user();
        $shipping_status = ShippingStatus::orderBy('sort', 'asc')->first();
        $order_status = OrderStatus::orderBy('sort', 'asc')->first();
        $order = Order::create([
            'user_id' => $user->id,
            'order_no' => 'DP' . time() . rand(1, 9999),
            'name' => $request->name,
            'phone' => $request->phone,
            'email' => $request->email,
            'county' => $request->county,
            'district' => $request->district,
            'zipcode' => $request->zipcode,
            'address' => $request->address,
            'price' => 99999999,
            'pay_type' => Session::get('payment'),
            'shipping' => Session::get('shipment'),
            'shipping_fee' => 99999999,
            'shipping_status_id' => $shipping_status->id,
            'order_status_id' => $order_status->id,
            'remark' => '',
        ]);

        $subPrice = 0;
        $cartData = \Cart::getContent();
        // items提供給金流商顯示商品
        $items = [];

        foreach ($cartData as $data) {
            $productId = $data->id;
            $product = Product::find($productId);
            $subPrice += $data->quantity * $product->price;
            OrderDetail::create([
                'order_id' => $order->id,
                'product_id' => $product->id,
                'qty' => $data->quantity,
                'old' => $product->tojson()
            ]);

            $new_ary = [
                'name' => $product->name,
                'qty' => $data->quantity,
                'price' => $product->price,
                'unit' => '個'
            ];
            array_push($items, $new_ary);
        }

        $fee = $subPrice > 1000 ? 0 : 60;
        $order->update([
            'price' => $subPrice + $fee,
            'shipping_fee' => $fee,
        ]);
        $array = [
            'MerchantID' => env('ECPAY_MERCHANT_ID'),
            'MerchantTradeNo' => $order->order_no,
            'MerchantTradeDate' => date('Y/m/d H:i:s'),
            'PaymentType'=> 'aio',
            'TotalAmount' => $order->price,
            'TradeDesc' => 'ecpay 商城購物',
            'ItemName' => '手機 20 元 X2#隨身碟60 元 X1',
            'ReturnURL' => route('return'),
            'ChoosePayment' => 'Credit',
            'EncryptType'=>1
        ];
        $CheckMacValue = $this->generate($array,env('ECPAY_HASH_KEY'),env('ECPAY_HASH_IV'));
        $url='https://payment-stage.ecpay.com.tw/Cashier/AioCheckOut/V5';
        
        return $this->HtmlEncode($array,$url,$CheckMacValue);

    }
    public function generate($arParameters = array(), $HashKey = '', $HashIV = '', $encType = 0)
    {
        $sMacValue = '';
        if (isset($arParameters)) {
            // arParameters 為傳出的參數，並且做字母 A-Z 排序
            unset($arParameters['CheckMacValue']);
            ksort($arParameters);
            
            // 組合字串
            $sMacValue = 'HashKey=' . $HashKey;
            foreach ($arParameters as $key => $value) {
                $sMacValue .= '&' . $key . '=' . $value;
            }
            $sMacValue .= '&HashIV=' . $HashIV;
            // URL Encode 編碼
            $sMacValue = urlencode($sMacValue);
            // 轉成小寫
            $sMacValue = strtolower($sMacValue);
            // 取代為與 dotNet 相符的字元
            $sMacValue = str_replace('%2d', '-', $sMacValue);
            $sMacValue = str_replace('%5f', '_', $sMacValue);
            $sMacValue = str_replace('%2e', '.', $sMacValue);
            $sMacValue = str_replace('%21', '!', $sMacValue);
            $sMacValue = str_replace('%2a', '*', $sMacValue);
            $sMacValue = str_replace('%28', '(', $sMacValue);
            $sMacValue = str_replace('%29', ')', $sMacValue);
            // 編碼
            $sMacValue = hash('sha256', $sMacValue);
            $sMacValue = strtoupper($sMacValue);
        }
        return $sMacValue;
    }
    public function HtmlEncode($arParameters, $ServiceURL, $szCheckMacValue, $paymentButton = '') {
        $target = "_self";
        //生成表單，自動送出
        $szHtml =  '<!DOCTYPE html>';
        $szHtml .= '<html>';
        $szHtml .=     '<head>';
        $szHtml .=         '<meta charset="utf-8">';
        $szHtml .=     '</head>';
        $szHtml .=     '<body>';
        $szHtml .=         "<form id=\"__ecpayForm\" method=\"post\" target=\"{$target}\" action=\"{$ServiceURL}\">";

        foreach ($arParameters as $keys => $value) {
            $szHtml .=         "<input type=\"hidden\" name=\"{$keys}\" value=\"". htmlentities($value) . "\" />";
        }

        $szHtml .=             "<input type=\"hidden\" name=\"CheckMacValue\" value=\"{$szCheckMacValue}\" />";

        if(!empty($paymentButton))
        {
            $szHtml .=          "<input type=\"submit\" id=\"__paymentButton\" value=\"{$paymentButton}\" />";
        }

        $szHtml .=         '</form>';

        if(empty($paymentButton))
        {
            $szHtml .=         '<script type="text/javascript">document.getElementById("__ecpayForm").submit();</script>';
        }

        $szHtml .=     '</body>';
        $szHtml .= '</html>';


        return $szHtml;
    }
```