Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

沙箱模式的Notify总是出错:Invalid request payloads. #1045

Closed
exsystem opened this issue Dec 10, 2017 · 1 comment
Closed

沙箱模式的Notify总是出错:Invalid request payloads. #1045

exsystem opened this issue Dec 10, 2017 · 1 comment

Comments

@exsystem
Copy link

exsystem commented Dec 10, 2017

我用的环境

PHP 版本:7.0
overtrue/wechat 版本:3.3.20
是否使用了框架?框架名称:XFramework

问题及现象

公众号支付(JSAPI)沙箱模式的Notify总是提示:“Invalid request payloads.”,然而正式模式却一切正常。

发起支付:

$app     = $this->getEasyWechatApp();
        $payment = $app->payment;
        $payment->sandboxMode($this->FConfig->GetBoolean('Application.Wechat.payment.sandbox_mode')); //如果是false一切正常,true则失败
        try {
            $attributes = [
                'trade_type'   => 'JSAPI',
                'body'         => '****',
                'detail'       => TUuid::ToString($mOrderId, ''),
                'out_trade_no' => TUuid::ToString($mOrderId, ''),
                'total_fee'    => 1, //$mTotalFee, //如果是沙箱模式,我这里写的是101,对应微信的测试的case
                'openid'       => $this->getWechatOpenId(),
            ];

            $order  = new Order($attributes);
            $result = $payment->prepare($order);
            if ($result['return_code'] == 'SUCCESS' && $result['result_code'] == 'SUCCESS') {
                $prepayId = $result['prepay_id'];
                $config   = $payment->configForJSSDKPayment($prepayId);
            }
        }
        catch (EInvalidParameter $e) {
            //NOP
        }
        return $config;

NotifyHandler:

try {
            $mModel = new TOrderModel($this->FConfig, '');
            $payment = $this->getEasyWechatApp()->payment;
            $payment->sandboxMode($this->FConfig->GetBoolean('Application.Wechat.payment.sandbox_mode'));
            $response = $payment->handleNotify(function ($notify, $successful) use ($mModel) {
                TLogger::LogVariable($notify);
                $order = $mModel->GetOrder(TUuid::FromString($notify['out_trade_no']));
                if ($order === null) {
                    return 'Order not exist.';
                }
                if ($order->OrderStatus == TOrderStatus::ePaid()) {
                    return true;
                }
                if ($successful) {
                    $order->OrderStatus = TOrderStatus::ePaid();
                    TLogger::LogMessage('PAY SUCC');
                    TLogger::LogMessage($notify['total_fee']);
                }
                $mModel->UpdateOrderStatus($order->OrderId, $order->OrderStatus);
                return true;
            });
            TLogger::LogMessage('response = ');
            TLogger::LogMessage($response->getContent());
            $response->send();
        }
        catch (\Exception $e) {
            TLogger::LogMessage($e->getMessage());
        }
        finally {
            Framework::Free($mModel);
        }
@mingyoung
Copy link
Collaborator

PR #1036

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants