| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- <?php
- namespace Payment\Lib;
- use Dever;
- class Payfort extends Core
- {
- private $config = array();
- private $language = 'en';
- private $url = 'https://checkout.payfort.com/FortAPI/paymentPage';
- public function __construct($name, $config)
- {
- $this->name = $name;
- $this->config = $config;
- }
- private function getType($type, $currency)
- {
- if ($type < 5 && $currency == 'SAR') {
- $type = 4;
- }
- $name = $value = $method = $command = '';
- switch ($type) {
- case 1:
- # 目前不支持
- $name = 'Pay with credit cards (Redirection)';
- $method = 'redirection';
- break;
- case 2:
- $name = 'Pay with installments (Redirection)';
- $method = 'redirection';
- break;
- case 3:
- $name = 'Pay with NAPS';
- $value = 'NAPS';
- $method = 'redirection';
- break;
- case 4:
- $name = 'Pay with SADAD';
- $value = 'SADAD';
- $currency = 'SAR';
- $method = 'redirection';
- $command = 'AUTHORIZATION';
- break;
- case 11:
- $name = 'Pay with credit cards (Merchant Page)';
- $method = 'page';
- $currency = '';
- $command = 'TOKENIZATION';
- break;
- case 12:
- $name = 'Pay with installments (Merchant Page)';
- $method = 'page';
- break;
- case 21:
- $name = 'Pay with credit cards (Merchant Page 2.0)';
- $method = 'page_v2';
- break;
- }
- $currency = strtoupper($currency);
- return array
- (
- 'id' => $type,
- 'name' => $name,
- 'value' => $value,
- 'method' => $method,
- 'currency' => $currency,
- 'command' => $command,
- );
- }
- /**
- * 获取统一下单的基本信息
- */
- public function order($uid, $account, $product_name, $amount, $currency, $type = 1)
- {
- $type = $this->getType($type, $currency);
- $order_id = $this->createOrder($uid, $this->name, $product_name, $amount, $type['currency'], $type['name']);
- $method = $type['method'];
- $param = array();
- if ($type['currency']) {
- $param['amount'] = $amount;
- $param['currency'] = $type['currency'];
- }
- $param = $this->param($order_id, $type['command'], $type['value'], $param);
- return $this->$method($param);
- }
- /**
- * 通知回调
- *
- * @return mixed
- */
- public function notify()
- {
- return 1;
- }
- /**
- * 生成基本参数
- *
- * @return mixed
- */
- private function param($order_id, $command, $payment_option, $param = array())
- {
- $param['access_code'] = $this->config['access_code'];
- $param['merchant_identifier'] = $this->config['merchant_id'];
- $param['merchant_reference'] = $order_id;
- $param['language'] = $this->language;
- if ($payment_option) {
- $param['payment_option'] = $payment_option;
- }
- # AUTHORIZATION(授权)、PURCHASE(购买) TOKENIZATION CHECK_STATUS
- $param['command'] = $command;
- $param['return_url'] = Dever::url('api.notify?order_id=' . $order_id, 'payment');
- $param['signature'] = $this->signature($this->config['request_phrase'], $param);
- return $param;
- }
- /**
- * 页面付款,下单后需要填写用户信息
- *
- * @return mixed
- */
- public function page($param)
- {
- print_r($param);die;
- $result = Dever::curl($this->url, $param, 'post');
- print_r($result);die;
- }
- /**
- * 直接付款 Redirection
- *
- * @return mixed
- */
- public function redirection($param)
- {
- $html = '<div>loading...</div><form method="post" action="'.$this->url.'" id="payment" name="payment" style="display:none;">';
- foreach ($param as $k => $v) {
- $html .= '<p>'.$k.':<input type="text" name="'.htmlentities($k).'" value="'.htmlentities($v).'"></p>';
- }
-
- $html .= '<input type="submit" />';
- $html .= '</form><script>document.getElementById("payment").submit();</script>';
- echo $html;die;
- }
- /**
- * signature
- *
- * @return mixed
- */
- private function signature($phrase, $request)
- {
- ksort($request);
- $signature_string = '';
- foreach ($request as $k => $v) {
- $signature_string .= $k . '=' . $v;
- }
- $signature_string = $phrase . $signature_string . $phrase;
- return hash('sha256', $signature_string);
- }
- }
|