123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395 |
- <?php
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- namespace app\common\server;
-
-
- use Alipay\EasySDK\Kernel\Config;
- use app\common\enum\IntegralOrderEnum;
- use app\common\logic\IntegralOrderRefundLogic;
- use app\common\logic\PayNotifyLogic;
- use app\common\model\Client_;
- use app\common\enum\PayEnum;
- use app\common\model\integral\IntegralOrder;
- use think\facade\Db;
- use think\facade\Log;
- use Yansongda\Pay\Exceptions\GatewayException;
- use Yansongda\Pay\Exceptions\InvalidConfigException;
- use Yansongda\Pay\Exceptions\InvalidSignException;
- use Yansongda\Pay\Pay;
- use Yansongda\Supports\Collection;
-
- class AliPayServer
- {
-
-
- protected $error = '未知错误';
-
- private $alipay = null;
-
- public function getError()
- {
- return $this->error;
- }
-
-
- public function __construct()
- {
- $this->alipay = Pay::alipay(YansongdaAliPayServer::config());
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- static function getAliPayData($attach, $order_id) : array
- {
- if($attach == 'trade') {
- $trade = Db::name('order_trade')->where(['id' => $order_id])->find();
- $sn = $trade['t_sn'];
- $order_amount = $trade['order_amount'];
- }
-
- if($attach == 'order') {
- $order = Db::name('order')->where(['id' => $order_id])->find();
- $sn = $order['order_sn'];
- $order_amount = $order['order_amount'];
- }
-
- if($attach == 'recharge') {
- $order = Db::name('recharge_order')->where(['id' => $order_id])->find();
- $sn = $order['order_sn'];
- $order_amount = $order['order_amount'];
- }
-
- if ($attach == 'integral') {
- $order = IntegralOrder::where(['id' => $order_id])->find();
- $sn = $order['order_sn'];
- $order_amount = $order['order_amount'];
- }
-
- return [
- 'sn' => $sn ?? '',
- 'order_amount' => $order_amount ?? 0,
- ];
- }
-
-
-
- public function pagePay($attach, $order_id)
- {
- $domain = request()->domain();
-
- $data = static::getAliPayData($attach, $order_id);
-
- $ali_data = [
- 'out_trade_no' => $data['sn'],
- 'total_amount' => $data['order_amount'],
- 'subject' => '订单:' . $data['sn'],
- 'return_url' => $domain.'/pc/user/order',
- 'passback_params' => $attach,
- ];
-
- return $this->alipay->web($ali_data)->getContent();
- }
-
-
-
-
- public function appPay($attach, $order_id)
- {
-
- $data = static::getAliPayData($attach, $order_id);
-
- $ali_data = [
- 'out_trade_no' => $data['sn'],
- 'total_amount' => $data['order_amount'],
- 'subject' => $data['sn'],
- 'passback_params' => $attach,
- ];
-
- return $this->alipay->app($ali_data)->getContent();
- }
-
-
-
-
- public function wapPay($attach, $order_id)
- {
-
- $data = static::getAliPayData($attach, $order_id);
-
- $domain = request()->domain();
-
- $ali_data = [
- 'out_trade_no' => $data['sn'],
- 'total_amount' => $data['order_amount'],
- 'subject' => $data['sn'],
- 'passback_params' => $attach,
- 'return_url' => $domain . '/mobile/bundle/pages/user_order/user_order',
- 'quit_url' => $domain . '/mobile/bundle/pages/user_order/user_order',
- ];
-
- return $this->alipay->wap($ali_data)->getContent();
- }
-
- function mnpPay($attach, $order_id)
- {
- $data = static::getAliPayData($attach, $order_id);
-
- $ali_data = [
- 'out_trade_no' => $data['sn'],
- 'total_amount' => $data['order_amount'],
- 'subject' => $data['sn'],
- 'passback_params' => $attach,
- ];
-
- return $this->alipay->wap($ali_data)->getContent();
- }
-
- function oaPay($attach, $order_id)
- {
- $data = static::getAliPayData($attach, $order_id);
-
- $ali_data = [
- 'out_trade_no' => $data['sn'],
- 'total_amount' => $data['order_amount'],
- 'subject' => $data['sn'],
- 'passback_params' => $attach,
- ];
-
- return $this->alipay->wap($ali_data)->getContent();
- }
-
-
-
-
- public function pay($from, $order_id, $order_source)
- {
- try{
- switch ($order_source){
- case Client_::pc:
- $result = $this->pagePay($from, $order_id);
- break;
- case Client_::ios:
- case Client_::android:
- $result = $this->appPay($from, $order_id);
- break;
- case Client_::h5:
- $result = $this->wapPay($from, $order_id);
- break;
- case Client_::mnp:
- $result = $this->mnpPay($from, $order_id);
- break;
- case Client_::oa:
- $result = $this->oaPay($from, $order_id);
- break;
- default:
- throw new \Exception('支付方式错误');
- }
- return $result;
- } catch (\Exception $e) {
- $this->error = $e->getMessage();
- return false;
- }
- }
-
-
-
-
- public function verifyNotify($data)
- {
- try {
- $this->alipay->verify($data);
-
- if (!in_array($data['trade_status'], ['TRADE_SUCCESS', 'TRADE_FINISHED'])) {
- return true;
- }
- $extra['transaction_id'] = $data['trade_no'];
-
- switch ($data['passback_params']) {
- case 'order':
- $order = Db::name('order')->where(['order_sn' => $data['out_trade_no']])->find();
- if (!$order || $order['pay_status'] >= PayEnum::ISPAID) {
- return true;
- }
- PayNotifyLogic::handle('order', $data['out_trade_no'], $extra);
- break;
-
- case 'trade':
- $order_trade = Db::name('order_trade')->where(['t_sn' => $data['out_trade_no']])->find();
- $trade_id = $order_trade['id'];
- $orders = Db::name('order')->where(['trade_id' => $trade_id])->select();
- foreach ($orders as $order) {
- if (!$order || $order['pay_status'] >= PayEnum::ISPAID) {
- return true;
- }
- }
- PayNotifyLogic::handle('trade', $data['out_trade_no'], $extra);
- break;
-
- case 'recharge':
- $order = Db::name('recharge_order')->where(['order_sn' => $data['out_trade_no']])->find();
- if (!$order || $order['pay_status'] >= PayEnum::ISPAID) {
- return true;
- }
- PayNotifyLogic::handle('recharge', $data['out_trade_no'], $extra);
- break;
-
- case 'integral':
- $order = IntegralOrder::where(['order_sn' => $data['out_trade_no']])->find();
- if (!$order || $order['refund_status'] == IntegralOrderEnum::IS_REFUND) {
-
- return true;
- }
- if ($order['order_status'] == IntegralOrderEnum::ORDER_STATUS_DOWN) {
-
- IntegralOrderRefundLogic::refundOrderAmount($order['id']);
- return true;
- }
- if ($order['pay_status'] >= PayEnum::ISPAID) {
- return true;
- }
- PayNotifyLogic::handle('integral', $data['out_trade_no'], $extra);
- break;
- }
-
- return true;
- } catch (\Throwable $e) {
- $record = [
- __CLASS__, __FUNCTION__, $e->getFile(), $e->getLine(), $e->getMessage()
- ];
- Log::record(implode('-', $record));
- return false;
- }
- }
-
-
-
-
- public function checkPay($order_sn) : Collection
- {
- return $this->alipay->find([
- 'out_trade_no' => $order_sn,
- ]);
- }
-
-
-
- public function refund($order_sn, $order_amount, string $out_request_no = '')
- {
- try {
-
- $data = [
- 'out_trade_no' => "{$order_sn}",
- 'refund_amount' => "{$order_amount}",
- ];
-
- if ($out_request_no) {
- $data['out_request_no'] = "{$out_request_no}";
- }
-
- return $this->alipay->refund($data)->toArray();
-
- } catch(InvalidConfigException |InvalidSignException|GatewayException|\Throwable $e) {
- Log::write([ $order_sn, $order_amount, $e->__toString() ], 'ALI_PAY_REFUND');
- return [ 'code' => 0, 'msg' => 'Failed', 'fund_change' => '', 'e' => [ $order_sn, $order_amount ] ];
- }
-
- }
-
-
- }
|