PHP新手30天實戰金流
, Laravel6
Day 9,10 我們知道要傳哪些參數給 ECPay, 今天我們來看如何在架構裡 導入 ECPay的服務。目前先嘗試最直覺的做法,可能都還有很大的進步空間。
PaymentController.php
中的 payByWebsite 函式將 ECPay所需的參數整理好: include('ECPay.Payment.Integration.php');
try {
$obj = new ECPay_AllInOne();
//服務參數
$obj->ServiceURL = "https://payment-stage.ecpay.com.tw/Cashier/AioCheckOut/V5"; //服務位置
$obj->HashKey = '5294y06JbISpM5x9' ; //測試用Hashkey,請自行帶入ECPay提供的HashKey
$obj->HashIV = 'v77hoKGq4kWxNNIS' ; //測試用HashIV,請自行帶入ECPay提供的HashIV
$obj->MerchantID = '2000214'; //測試用MerchantID,請自行帶入ECPay提供的MerchantID
$obj->EncryptType = '1'; //CheckMacValue加密類型,請固定填入1,使用SHA256加密
//基本參數(依系統調整)
$MerchantTradeNo = "Test".time() ;
$obj->Send['ReturnURL'] = "http://www.ecpay.com.tw/receive.php" ; //付款完成通知回傳的網址
$obj->Send['MerchantTradeNo'] = $MerchantTradeNo; //訂單編號
$obj->Send['MerchantTradeDate'] = date('Y/m/d H:i:s'); //交易時間
$obj->Send['TotalAmount'] = 2000; //交易金額
$obj->Send['TradeDesc'] = "good to drink" ; //交易描述
$obj->Send['ChoosePayment'] = ECPay_PaymentMethod::ALL ; //付款方式:全功能
//訂單的商品資料
array_push($obj->Send['Items'], array(
'Name' => "商品名稱",
'Price' => (int)1000,
'Currency' => "元",
'Quantity' => (int) "1",
'URL' => ""));
//產生訂單(auto submit至ECPay)
$obj->CheckOut();
}catch (Exception $e) {
echo $e->getMessage();
}
OrderController.php
中取出訂單的商品資訊,以下兩種方法結果相同:$order_with = Order::with(['items.product', 'items.productSku'])->where('id', $order['id'])->first();
$order_info = $order->load(['items.product','items.productSku']);
vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
public function load($relations)
{
$query = $this->newQueryWithoutRelationships()->with(
is_string($relations) ? func_get_args() : $relations
);
$query->eagerLoadRelations([$this]);
return $this;
}
{
"id": 154,
"no": "20190930205352635553",
"user_id": 2,
"address": {
"address": "202 基隆市中正區qqqqqq",
"zip_code": 202,
"contact_name": "qqq",
"contact_phone": "09723456"
},
"total_amount": "1783",
"remark": null,
"paid_at": null,
"payment_method": null,
"payment_no": null,
"refund_status": "pending",
"refund_no": null,
"closed": false,
"reviewed": false,
"ship_status": "pending",
"ship_data": null,
"extra": null,
"created_at": "2019-09-30 20:53:52",
"updated_at": "2019-09-30 20:53:52",
"items": [
{
"id": 273,
"order_id": 154,
"product_id": 1,
"product_sku_id": 1,
"amount": 1,
"price": "1783",
"rating": null,
"review": null,
"reviewed_at": null,
"product_sku": {
"id": 1,
"price": 1783,
"stock": 10501,
"product_id": 1,
"attr_items_index": "[0,0,0]",
"created_at": "2019-09-26 10:59:19",
"updated_at": "2019-09-30 20:53:52"
},
"product": {
"id": 1,
"title": "薄銘承商品",
"description": "乾菜和松花黃的米飯,便知道他家玩去咧……\" \"這好極!他很不高興了。伊為預防危險。阿Q不幸而拍拍!",
"image": "http://laravel-shop.test/images/7kG1HekGK6.jpg",
"on_sale": true,
"rating": 3,
"sold_count": 4,
"review_count": 1,
"price": 132,
"created_at": "2019-09-26 10:59:19",
"updated_at": "2019-09-26 14:16:31"
}
}
]
}