|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Omnipay\YiPAY\Message; |
| 4 | + |
| 5 | +use Omnipay\YiPAY\Hasher; |
| 6 | +use Omnipay\YiPAY\Traits\HasYiPAY; |
| 7 | + |
| 8 | +class PurchaseRequest extends AbstractRequest |
| 9 | +{ |
| 10 | + use HasYiPAY; |
| 11 | + |
| 12 | + /** |
| 13 | + * 取得付款方式 |
| 14 | + * |
| 15 | + * @return int|null 付款方式 |
| 16 | + */ |
| 17 | + public function getType() |
| 18 | + { |
| 19 | + return $this->getParameter('type'); |
| 20 | + } |
| 21 | + |
| 22 | + /** |
| 23 | + * 設定付款方式 |
| 24 | + * |
| 25 | + * 1 信用卡付款 |
| 26 | + * 2 信用卡 3D 付款 |
| 27 | + * 3 超商代碼繳費 |
| 28 | + * 4 ATM 虛擬帳號繳款 |
| 29 | + * |
| 30 | + * @param int $value 付款方式 (Int, 長度: 1, 參閱《附錄 C. 付款方式》) |
| 31 | + */ |
| 32 | + public function setType($value) |
| 33 | + { |
| 34 | + $this->setParameter('type', $value); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * 取得商家訂單編號 |
| 39 | + * |
| 40 | + * @return string|null 商家訂單編號 |
| 41 | + */ |
| 42 | + public function getOrderNo() |
| 43 | + { |
| 44 | + return $this->getTransactionId(); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * 設定商家訂單編號 |
| 49 | + * |
| 50 | + * @param string $value 商家自訂的訂單編號 (String, 長度: 20, 唯一值不可重複) |
| 51 | + */ |
| 52 | + public function setOrderNo($value) |
| 53 | + { |
| 54 | + $this->setTransactionId($value); |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * 取得交易內容 |
| 59 | + * |
| 60 | + * @return string|null 交易內容 |
| 61 | + */ |
| 62 | + public function getOrderDescription() |
| 63 | + { |
| 64 | + return $this->getDescription(); |
| 65 | + } |
| 66 | + |
| 67 | + /** |
| 68 | + * 設定交易內容 |
| 69 | + * |
| 70 | + * @param string $value 消費者購買內容 (String, 長度: 200) |
| 71 | + */ |
| 72 | + public function setOrderDescription($value) |
| 73 | + { |
| 74 | + $this->setDescription($value); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * 取得交易備註 1 |
| 79 | + * |
| 80 | + * @return string|null 交易備註 1 |
| 81 | + */ |
| 82 | + public function getOrderNote1() |
| 83 | + { |
| 84 | + return $this->getParameter('orderNote1'); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * 設定交易備註 1 |
| 89 | + * |
| 90 | + * @param string $value 商家自定義 1 (String, 長度: 200) |
| 91 | + */ |
| 92 | + public function setOrderNote1($value) |
| 93 | + { |
| 94 | + $this->setParameter('orderNote1', $value); |
| 95 | + } |
| 96 | + |
| 97 | + /** |
| 98 | + * 取得交易備註 2 |
| 99 | + * |
| 100 | + * @return string|null 交易備註 2 |
| 101 | + */ |
| 102 | + public function getOrderNote2() |
| 103 | + { |
| 104 | + return $this->getParameter('orderNote2'); |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * 設定交易備註 2 |
| 109 | + * |
| 110 | + * @param string $value 商家自定義 2 (String, 長度: 200) |
| 111 | + */ |
| 112 | + public function setOrderNote2($value) |
| 113 | + { |
| 114 | + $this->setParameter('orderNote2', $value); |
| 115 | + } |
| 116 | + |
| 117 | + public function getNotificationEmail() |
| 118 | + { |
| 119 | + return $this->getParameter('notificationEmail'); |
| 120 | + } |
| 121 | + |
| 122 | + // 設置與獲取 notificationEmail |
| 123 | + |
| 124 | + /** |
| 125 | + * 設定成功交易 Email 通知 |
| 126 | + * |
| 127 | + * 當交易成功時將交易結果寄送至商家指定的 Email,若有多筆 Email 以分號「;」區隔。請 注意此處為通知商家消費者已完成交易用 |
| 128 | + * |
| 129 | + * @param string $value 成功交易 Email (String, 長度: 200) |
| 130 | + */ |
| 131 | + public function setNotificationEmail($value) |
| 132 | + { |
| 133 | + $this->setParameter('notificationEmail', $value); |
| 134 | + } |
| 135 | + |
| 136 | + /** |
| 137 | + * 取得背景回傳網址 |
| 138 | + * |
| 139 | + * @return string|null 背景回傳網址 |
| 140 | + */ |
| 141 | + public function getBackgroundURL() |
| 142 | + { |
| 143 | + return $this->getNotifyUrl(); |
| 144 | + } |
| 145 | + |
| 146 | + /** |
| 147 | + * 設定背景回傳網址 |
| 148 | + * |
| 149 | + * @param string $value 交易完成後 YiPay 以背景方式 POST 回傳結果的網址 |
| 150 | + */ |
| 151 | + public function setBackgroundURL($value) |
| 152 | + { |
| 153 | + return $this->setNotifyUrl($value); |
| 154 | + } |
| 155 | + |
| 156 | + /** |
| 157 | + * 取得交易逾時秒數 |
| 158 | + * |
| 159 | + * @return int|null 交易逾時秒數 |
| 160 | + */ |
| 161 | + public function getTimeout() |
| 162 | + { |
| 163 | + return $this->getParameter('timeout'); |
| 164 | + } |
| 165 | + |
| 166 | + /** |
| 167 | + * 設定交易逾時秒數 |
| 168 | + * |
| 169 | + * @param int $value 交易逾時秒數,範圍 90 ~ 28800,無限制為 0 |
| 170 | + */ |
| 171 | + public function setTimeout($value) |
| 172 | + { |
| 173 | + $this->setParameter('timeout', $value); |
| 174 | + } |
| 175 | + |
| 176 | + /** |
| 177 | + * 取得進入頁面有效時間 |
| 178 | + * |
| 179 | + * @return string|null 交易有效時間 |
| 180 | + */ |
| 181 | + public function getValidTime() |
| 182 | + { |
| 183 | + return $this->getParameter('validTime'); |
| 184 | + } |
| 185 | + |
| 186 | + /** |
| 187 | + * 設定進入頁面有效時間 |
| 188 | + * |
| 189 | + * @param string $value 交易有效時間 (格式: yyyyMMddHHmm) |
| 190 | + */ |
| 191 | + public function setValidTime($value) |
| 192 | + { |
| 193 | + $this->setParameter('validTime', $value); |
| 194 | + } |
| 195 | + |
| 196 | + /** |
| 197 | + * 取得逾時返回網址 |
| 198 | + * |
| 199 | + * @return string|null 逾時返回網址 |
| 200 | + */ |
| 201 | + public function getTimeoutUrl() |
| 202 | + { |
| 203 | + return $this->getParameter('timeoutURL'); |
| 204 | + } |
| 205 | + |
| 206 | + /** |
| 207 | + * 設定逾時返回網址 |
| 208 | + * |
| 209 | + * @param string $value 交易逾時時返回商家指定的網址 |
| 210 | + */ |
| 211 | + public function setTimeoutURL($value) |
| 212 | + { |
| 213 | + $this->setParameter('timeoutURL', $value); |
| 214 | + } |
| 215 | + |
| 216 | + public function getData() |
| 217 | + { |
| 218 | + $this->validate('amount', 'transactionId', 'returnUrl'); |
| 219 | + |
| 220 | + return array_filter([ |
| 221 | + 'merchantId' => $this->getMerchantId(), |
| 222 | + 'type' => $this->getType() ?: 2, |
| 223 | + 'amount' => (string) ((int) $this->getAmount()), |
| 224 | + 'orderNo' => $this->getTransactionId(), |
| 225 | + 'orderDescription' => $this->getDescription(), |
| 226 | + 'orderNote1' => $this->getOrderNote1(), |
| 227 | + 'orderNote2' => $this->getOrderNote2(), |
| 228 | + 'notificationEmail' => $this->getNotificationEmail(), |
| 229 | + 'returnURL' => $this->getReturnUrl(), |
| 230 | + 'cancelURL' => $this->getCancelUrl(), |
| 231 | + 'backgroundURL' => $this->getNotifyUrl(), |
| 232 | + 'timeout' => $this->getTimeout(), |
| 233 | + 'validTime' => $this->getValidTime(), |
| 234 | + 'timeoutURL' => $this->getTimeoutUrl(), |
| 235 | + ], static function ($value) { |
| 236 | + return $value !== null; |
| 237 | + }); |
| 238 | + } |
| 239 | + |
| 240 | + public function sendData($data) |
| 241 | + { |
| 242 | + $hasher = new Hasher($this->getKey(), $this->getIv()); |
| 243 | + $data['checkCode'] = $hasher->make([ |
| 244 | + 'merchantId' => $data['merchantId'], |
| 245 | + 'amount' => $data['amount'], |
| 246 | + 'orderNo' => $data['orderNo'], |
| 247 | + 'returnURL' => $data['returnURL'], |
| 248 | + 'cancelURL' => $data['cancelURL'], |
| 249 | + 'backgroundURL' => $data['backgroundURL'], |
| 250 | + ]); |
| 251 | + |
| 252 | + return $this->response = new PurchaseResponse($this, $data); |
| 253 | + } |
| 254 | +} |
0 commit comments