123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- <?php
- namespace addons\shopro\controller;
- use addons\epay\library\Service;
- use fast\Random;
- use think\addons\Controller;
- use addons\shopro\exception\Exception;
- use addons\shopro\model\Order;
- use addons\shopro\model\User;
- use think\Db;
- use think\Log;
- class Pay extends Base
- {
- protected $noNeedLogin = ['prepay', 'notifyx', 'notifyr', 'alipay'];
- protected $noNeedRight = ['*'];
-
- public function alipay()
- {
- $order_sn = $this->request->get('order_sn');
- $order = Order::where('order_sn', $order_sn)->find();
- try {
- if (!$order) {
- throw new \Exception("订单不存在");
- }
- if ($order->status > 0) {
- throw new \Exception("订单已支付");
- }
- if ($order->status < 0) {
- throw new \Exception("订单已失效");
- }
-
- $notify_url = $this->request->root(true) . '/addons/shopro/pay/notifyx/payment/alipay/platform/H5';
- $pay = new \addons\shopro\library\PayService('alipay', 'url', $notify_url);
-
- $order_data = [
- 'out_trade_no' => $order->order_sn,
- 'total_fee' => $order->total_fee,
- 'subject' => '商城订单支付',
- ];
-
- $result = $pay->create($order_data);
- $result = $result->getContent();
-
- echo $result;
- } catch (\Exception $e) {
- echo $e->getMessage();
- }
-
-
- }
-
- public function prepay()
- {
- $order_sn = $this->request->post('order_sn');
- $payment = $this->request->post('payment');
- $openid = $this->request->post('openid', '');
- $platform = request()->header('platform');
-
- $order = Order::nopay()->where('order_sn', $order_sn)->find();
- if (!$order) {
- $this->error("订单不存在");
- }
- if (!$payment || !in_array($payment, ['wechat', 'alipay', 'wallet'])) {
- $this->error("支付类型不能为空");
- }
- if ($payment == 'wallet') {
-
- $this->walletPay($order, $payment, $platform);
- }
- $order_data = [
- 'out_trade_no' => $order->order_sn,
- 'total_fee' => $order->total_fee,
- ];
-
- if ($payment == 'wechat') {
- if (in_array($platform, ['wxOfficialAccount', 'wxMiniProgram'])) {
- if (isset($openid) && $openid) {
-
- $order_data['openid'] = $openid;
- } else {
-
- $oauth = \addons\shopro\model\UserOauth::where([
- 'user_id' => $order->user_id,
- 'provider' => 'Wechat',
- 'platform' => $platform
- ])->find();
-
- $order_data['openid'] = $oauth ? $oauth->openid : '';
- }
-
- if (empty($order_data['openid'])) {
-
- return $this->success('缺少 openid', 'no_openid');
- }
- }
- $order_data['body'] = '商城订单支付';
- } else {
- $order_data['subject'] = '商城订单支付';
- }
- $notify_url = $this->request->root(true) . '/addons/shopro/pay/notifyx/payment/' . $payment . '/platform/' . $platform;
- $pay = new \addons\shopro\library\PayService($payment, $platform, $notify_url);
- $result = $pay->create($order_data);
- if ($platform == 'App') {
- $result = $result->getContent();
- }
- if ($platform == 'H5' && $payment == 'wechat') {
- $result = $result->getContent();
- }
- return $this->success('获取预付款成功', [
- 'pay_data' => $result,
- 'pay_action' => $pay->method,
- ]);
- }
-
- public function walletPay ($order, $type, $method) {
- $order = Db::transaction(function () use ($order, $type, $method) {
-
- $order = Order::nopay()->where('order_sn', $order->order_sn)->lock(true)->find();
- if (!$order) {
- $this->error("订单已支付");
- }
- $total_fee = $order->total_fee;
-
- $user = User::info();
- if (is_null($user)) {
-
- $this->error(__('Please login first'), null, 401);
- }
- User::money(-$total_fee, $user->id, 'wallet_pay', $order->id, '',[
- 'order_id' => $order->id,
- 'order_sn' => $order->order_sn,
- ]);
-
- $notify = [
- 'order_sn' => $order['order_sn'],
- 'transaction_id' => '',
- 'notify_time' => date('Y-m-d H:i:s'),
- 'buyer_email' => $user->id,
- 'pay_fee' => $order->total_fee,
- 'pay_type' => 'wallet'
- ];
- $notify['payment_json'] = json_encode($notify);
- $order->paymentProcess($order, $notify);
- return $order;
- });
- $this->success('支付成功', $order);
- }
-
- public function notifyx()
- {
- Log::write('notifyx-comein:');
- $payment = $this->request->param('payment');
- $platform = $this->request->param('platform');
- $pay = new \addons\shopro\library\PayService($payment, $platform);
- $result = $pay->notify(function ($data, $pay) use ($payment, $platform) {
- Log::write('notifyx-result:'. json_encode($data));
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- try {
- $out_trade_no = $data['out_trade_no'];
- $out_refund_no = $data['out_biz_no'] ?? '';
-
-
- if ($payment == 'alipay'
- && $data['notify_type'] == 'trade_status_sync'
- && $data['trade_status'] == 'TRADE_CLOSED'
- && $out_refund_no
- ) {
-
- $this->refundFinish($out_trade_no, $out_refund_no);
- return $pay->success()->send();
- }
-
- if ($payment == 'alipay' && $data['trade_status'] != 'TRADE_SUCCESS') {
-
- return $pay->success()->send();
- }
- if ($payment == 'wechat' && ($data['result_code'] != 'SUCCESS' || $data['return_code'] != 'SUCCESS')) {
-
- return false;
- }
-
- $pay_fee = $payment == 'alipay' ? $data['total_amount'] : $data['total_fee'] / 100;
-
- $order = Order::where('order_sn', $out_trade_no)->find();
- if (!$order || $order->status > 0) {
-
- return $pay->success()->send();
- }
- Db::transaction(function () use ($order, $data, $payment, $platform, $pay_fee) {
- $notify = [
- 'order_sn' => $data['out_trade_no'],
- 'transaction_id' => $payment == 'alipay' ? $data['trade_no'] : $data['transaction_id'],
- 'notify_time' => date('Y-m-d H:i:s', strtotime($data['time_end'])),
- 'buyer_email' => $payment == 'alipay' ? $data['buyer_logon_id'] : $data['openid'],
- 'payment_json' => json_encode($data->all()),
- 'pay_fee' => $pay_fee,
- 'pay_type' => $payment
- ];
- $order->paymentProcess($order, $notify);
- });
- return $pay->success()->send();
- } catch (\Exception $e) {
- Log::write('notifyx-error:' . json_encode($e->getMessage()));
- }
- });
- return $result;
- }
-
- public function notifyr()
- {
- Log::write('notifyreturn-comein:');
- $payment = $this->request->param('payment');
- $platform = $this->request->param('platform');
- $pay = new \addons\shopro\library\PayService($payment, $platform);
- $result = $pay->notifyRefund(function ($data, $pay) use ($payment, $platform) {
- Log::write('notifyr-result:' . json_encode($data));
- try {
- $out_refund_no = $data['out_refund_no'];
- $out_trade_no = $data['out_trade_no'];
-
- $this->refundFinish($out_trade_no, $out_refund_no);
-
- return $pay->success()->send();
- } catch (\Exception $e) {
- Log::write('notifyreturn-error:' . json_encode($e->getMessage()));
- }
- });
- return $result;
- }
-
- private function refundFinish($out_trade_no, $out_refund_no) {
- $order = Order::where('order_sn', $out_trade_no)->find();
- $refundLog = \app\admin\model\shopro\order\RefundLog::where('refund_sn', $out_refund_no)->find();
- if (!$order || !$refundLog || $refundLog->status != 0) {
-
- return true;
- }
- $item = \app\admin\model\shopro\order\OrderItem::where('id', $refundLog->order_item_id)->find();
- Db::transaction(function () use ($order, $item, $refundLog) {
- \app\admin\model\shopro\order\Order::refundFinish($order, $item, $refundLog);
- });
- return true;
- }
- }
|