|
@@ -1,4 +1,5 @@
|
|
|
<?php
|
|
|
+
|
|
|
/**
|
|
|
* Created by PhpStorm.
|
|
|
* User: z-yang
|
|
@@ -22,26 +23,28 @@ class Official
|
|
|
private $key;
|
|
|
private $cert_path;
|
|
|
private $key_path;
|
|
|
+ private $trade_type;
|
|
|
+
|
|
|
public $app;
|
|
|
public function __construct($config)
|
|
|
{
|
|
|
$this->appid = $config['appid'];
|
|
|
- $this->secret = $config['secret'];
|
|
|
- $this->token = $config['token'];
|
|
|
$this->merchant_id = $config['merchant_id'];
|
|
|
$this->key = $config['key'];
|
|
|
- $this->cert_path = $config['cert_path'];
|
|
|
- $this->key_path = $config['key_path'];
|
|
|
-
|
|
|
+ $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)
|
|
|
+ '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);
|
|
@@ -49,36 +52,49 @@ class Official
|
|
|
|
|
|
function send($data)
|
|
|
{
|
|
|
- Log::info('Official---enter--------------');
|
|
|
$app = $this->app;
|
|
|
$payment = $app->payment;
|
|
|
$attributes = [
|
|
|
- 'trade_type' => 'JSAPI',
|
|
|
+ 'trade_type' => $this->trade_type,
|
|
|
'body' => $data['body'],
|
|
|
- 'detail' => $data['detail'],
|
|
|
+ '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' => $data['openid'],
|
|
|
+ '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('Official -------pay-----result---------- order is: ' . $data['trade_no']);
|
|
|
Log::info($result);
|
|
|
if ($result->return_code == 'SUCCESS' && $result->result_code == 'SUCCESS') {
|
|
|
- $data = [
|
|
|
- 'appId' => $result->appid,
|
|
|
- 'package' => 'prepay_id=' . $result->prepay_id,
|
|
|
- 'nonceStr' => $result->nonce_str,
|
|
|
- 'timeStamp' => time(),
|
|
|
- 'signType' => 'MD5',
|
|
|
- ];
|
|
|
- $data['paySign'] = $this->MakeSign($data);
|
|
|
+ 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('pay error order is: ' . $data['trade_no']);
|
|
|
Log::error($result);
|
|
|
return [];
|
|
|
}
|
|
@@ -96,11 +112,11 @@ class Official
|
|
|
}
|
|
|
$buff = trim($buff, "&");
|
|
|
//签名步骤二:在string后加入KEY
|
|
|
- $string = $buff . "&key=".$this->key;
|
|
|
+ $string = $buff . "&key=" . $this->key;
|
|
|
//签名步骤三:MD5加密
|
|
|
$string = md5($string);
|
|
|
//签名步骤四:所有字符转为大写
|
|
|
$result = strtoupper($string);
|
|
|
return $result;
|
|
|
}
|
|
|
-}
|
|
|
+}
|