123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122 |
- <?php
- /**
- * Created by PhpStorm.
- * User: z-yang
- * Date: 2017/12/9
- * Time: 12:02
- */
- namespace App\Libs\Pay\Merchants;
- use EasyWeChat\Foundation\Application;
- use EasyWeChat\Payment\Order as Wxorder;
- use Log;
- class Official
- {
- private $appid;
- private $secret;
- private $token;
- private $merchant_id;
- private $key;
- private $cert_path;
- private $key_path;
- private $trade_type;
- public $app;
- public function __construct($config)
- {
- $this->appid = $config['appid'];
- $this->merchant_id = $config['merchant_id'];
- $this->key = $config['key'];
- $this->secret = isset($config['secret']) ? $config['secret'] : '';
- $this->token = isset($config['token']) ? $config['token'] : '';
- $this->key_path = isset($config['key_path']) ? $config['key_path'] : '';
- $this->cert_path = isset($config['cert_path']) ? $config['cert_path'] : '';
- $this->trade_type = isset($config['trade_type']) ? $config['trade_type'] : 'JSAPI';
- $options = [
- 'app_id' => $this->appid,
- 'secret' => $this->secret,
- 'token' => $this->token,
- 'payment' => [
- 'merchant_id' => $this->merchant_id,
- 'key' => $this->key,
- 'cert_path' => public_path($this->cert_path),
- 'key_path' => public_path($this->key_path)
- ]
- ];
- $this->app = new Application($options);
- }
- function send($data)
- {
- $app = $this->app;
- $payment = $app->payment;
- $attributes = [
- 'trade_type' => $this->trade_type,
- 'body' => $data['body'],
- 'detail' => isset($data['detail']) ? $data['detail'] : '',
- 'out_trade_no' => $data['trade_no'],
- 'total_fee' => $data['price'],
- 'notify_url' => env('OFFICIAL_PAY_CALL_BACK_URL'),
- 'openid' => isset($data['openid']) ? $data['openid'] : '',
- 'spbill_create_ip' => $data['create_ip'],
- 'attach' => $data['remark']
- ];
- $order = new Wxorder($attributes);
- $result = $payment->prepare($order);
- Log::info('Official -------pay-----result---------- order is: ' . $data['trade_no']);
- Log::info($result);
- if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
- switch ($this->trade_type) {
- case 'JSAPI':
- $data = [
- 'appId' => $result->appid,
- 'package' => 'prepay_id=' . $result->prepay_id,
- 'nonceStr' => $result->nonce_str,
- 'timeStamp' => time(),
- 'signType' => 'MD5',
- ];
- $data['paySign'] = $this->MakeSign($data);
- break;
- case 'APP':
- $data = [
- 'appId' => $result->appid,
- 'mch_id' => $result->mch_id,
- 'prepay_id' => $result->prepay_id,
- 'nonce_str' => $result->nonce_str,
- 'sign' => $result->sign,
- 'trade_type' => $result->trade_type,
- ];
- break;
- }
- return $data;
- }
- Log::error('pay error order is: ' . $data['trade_no']);
- Log::error($result);
- return [];
- }
- protected function MakeSign($value)
- {
- $data = $value;
- //签名步骤一:按字典序排序参数
- ksort($data);
- $buff = "";
- foreach ($data as $k => $v) {
- if ($k != "sign" && $v != "" && !is_array($v)) {
- $buff .= $k . "=" . $v . "&";
- }
- }
- $buff = trim($buff, "&");
- //签名步骤二:在string后加入KEY
- $string = $buff . "&key=" . $this->key;
- //签名步骤三:MD5加密
- $string = md5($string);
- //签名步骤四:所有字符转为大写
- $result = strtoupper($string);
- return $result;
- }
- }
|