# 綠界金流(套件版)
###### tags: `金流`
## 錯誤排除
有送出訂單,但跳轉至綠界田信用卡號前報錯:
錯誤代碼 : 10100060
錯誤原因 : MerchantID Is Null.
>清空預設直即可
`php artisan config:clear
`
記得重開(ctrl+c並php artisan serve )
## 參考資料
[綠界官方技術文件](https://www.ecpay.com.tw/Content/files/ecpay_011.pdf)
[綠界後台查詢流程](https://diary.taskinghouse.com/posts/woocommerce-ecpay-gateway-testmode-process/)
[綠界後台查詢訂單](https://vendor-stage.ecpay.com.tw/Frame/Index)
___
## 套件連結
[Laravel ECPay](https://github.com/tsaiyihua/laravel-ecpay)
___
## 1. 安裝套件
```
composer require tsaiyihua/laravel-ecpay
php artisan vendor:publish --tag=ecpay
```
:::success
<details>
<summary><font color="#055">☘看成功安裝畫面</font></summary>
![](https://i.imgur.com/FhGFzn4.png)
<details>
:::
## 2. 修改.env
```
ECPAY_MERCHANT_ID=
ECPAY_HASH_KEY=
ECPAY_HASH_IV=
ECPAY_INVOICE_HASH_KEY=
ECPAY_INVOICE_HASH_IV=
```
:::success
<details>
<summary><font color="#055">☘看完整文字</font></summary>
```
ECPAY_MERCHANT_ID=2000132
ECPAY_HASH_KEY=5294y06JbISpM5x9
ECPAY_HASH_IV=v77hoKGq4kWxNNIS
ECPAY_INVOICE_HASH_KEY=電子發票用
ECPAY_INVOICE_HASH_IV=電子發票用
```
<details>
:::
![](https://i.imgur.com/dB60MbN.png)
⚠記得修改env後要重跑:
php artisan config:clear
php artisan serve
## 3. 修改route/web.php
```php=
//新增以下兩個Route
Route::prefix('cart_ecpay')->group(function(){
//當消費者付款完成後,綠界會將付款結果參數以幕後(Server POST)回傳到該網址。
Route::post('notify', 'ShoppingCartController@notifyUrl')->name('notify');
//付款完成後,綠界會將付款結果參數以幕前(Client POST)回傳到該網址
Route::post('return', 'ShoppingCartController@returnUrl')->name('return');
});
```
## 4. 修改app\Http\Middleware\VerifyCsrfToken.php
```php=
protected $except = [
'cart_ecpay/return','cart_ecpay/notify'
];
```
## 5. 修改app\Http\Controllers\ShoppingCartController.php
```php=
<?php
namespace App\Http\Controllers;
use App\Order;
use App\Product;
use App\OrderDetail;
use App\OrderStatus;
use App\ShippingStatus;
use App\Mail\OrderShipped;
use Illuminate\Http\Request;
use TsaiYiHua\ECPay\Checkout;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Session;
use TsaiYiHua\ECPay\Services\StringService;
use TsaiYiHua\ECPay\Collections\CheckoutResponseCollection;
class ShoppingCartController extends Controller
{
public function __construct(Checkout $checkout,CheckoutResponseCollection $checkoutResponse)
{
$this->checkout = $checkout;
$this->checkoutResponse = $checkoutResponse;
}
//
public function add(Request $request)
{
$product= Product::find($request->productId);
// 新增一筆產品至購物車
if($product){
\Cart::add(array(
'id' => $product->id, // 商品id必須唯一
'name' => $product->name, // 商品名稱
'price' => $product->price, // 商品價格
'quantity' => 1, // 商品數量
'attributes' => array(
'img'=>$product->img
) // 自定義參數
));
return 'success';
}else{
return 'fail';
}
}
public function update(Request $request)
{
$qty = intval($request->qty) < 1 ? 1 : $request->qty;
if($request->productId){
\Cart::update($request->productId, array(
'quantity' => array(
'relative' => false,
'value' => $qty
),
));
$product = \Cart::get($request->productId);
return $product->quantity;
}else{
return 'fail';
}
}
public function delete(Request $request)
{
if($request->productId){
\Cart::remove($request->productId);
return 'success';
}else{
return 'fail';
}
}
public function list()
{
// 看購物車現有內容
$cartCollection = \Cart::getContent()->sortBy('id');
return view('front.shoppingcart.cart01',compact('cartCollection'));
}
public function payment()
{
return view('front.shoppingcart.cart02');
}
public function paymentCheck(Request $request)
{
Session::put('payment',$request->payment);
Session::put('shipment',$request->shipment);
return view('front.shoppingcart.cart03');
}
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,
]);
//第三方支付
$formData = [
'UserId' => $order->user_id, // 用戶ID , Optional
'ItemDescription' => '產品簡介',
'Items' => $items,
'OrderId' => $order->order_no,
'TotalAmount' => $order->price,
'PaymentMethod' => 'Credit', // ALL, Credit, ATM, WebATM
];
//清空購物車
\Cart::clear();
return $this->checkout->setNotifyUrl(route('notify'))->setReturnUrl(route('return'))->setPostData($formData)->send();
}
public function notifyUrl(Request $request)
{
$serverPost = $request->post();
$checkMacValue = $request->post('CheckMacValue');
unset($serverPost['CheckMacValue']);
$checkCode = StringService::checkMacValueGenerator($serverPost);
if ($checkMacValue == $checkCode) {
return '1|OK';
} else {
return '0|FAIL';
}
}
public function returnUrl(Request $request)
{
$serverPost = $request->post();
$checkMacValue = $request->post('CheckMacValue');
unset($serverPost['CheckMacValue']);
$checkCode = StringService::checkMacValueGenerator($serverPost);
if ($checkMacValue == $checkCode) {
if (!empty($request->input('redirect'))) {
return redirect($request->input('redirect'));
} else {
//付款完成,下面接下來要將購物車訂單狀態改為已付款
//目前是顯示所有資料將其DD出來
dd($this->checkoutResponse->collectResponse($serverPost));
}
}
}
}
```
## 6. 輸入付款信用卡資訊(含簡訊驗證)
測試資料請參考
![](https://i.imgur.com/0HewXD9.png)
![](https://i.imgur.com/sbuPBkX.png)
## 7. 交易完成後至"綠界後台"確認是否刷過
[綠界後台查詢流程](https://diary.taskinghouse.com/posts/woocommerce-ecpay-gateway-testmode-process/)
![](https://i.imgur.com/dgCDrn4.png)
https://diary.taskinghouse.com/posts/woocommerce-ecpay-gateway-testmode-process/
## 8. 登入後至"信用卡收單-交易明細查詢" 輸入廠商訂單編號
※ 廠商訂單編號即orders資料庫中的order_no
![](https://i.imgur.com/jgJLxuL.png)
※ 查詢結果👇
![](https://i.imgur.com/In4d9uS.png)