1557492053 1 gadu atpakaļ
vecāks
revīzija
0b6b50c5d2

+ 3 - 6
application/api/controller/Test.php

@@ -3,6 +3,7 @@
 namespace app\api\controller;
 
 use app\api\service\TencentCloudService;
+use app\api\service\WxService;
 use app\common\controller\Api;
 
 
@@ -18,12 +19,8 @@ class Test extends Api
 
     function payment()
     {
-        $params = json_decode(file_get_contents("php://input"), true);
-        dump($params['A']);
-
-//        $service = (new WxService);
-//        $SR = $service->appletPay("oQ_977SN3KhBetHY9OPckGP9DNNc", self::getRandomStr(20), "sbsbsb", 0.01);
-//        $SR->code() ? $this->success($SR->data()) : $this->error($SR->msg());
+        $SR = (new WxService())->appPay("oQ_977SN3KhBetHY9OPckGP9DNNc", "G" . time(), "商品购买", 0.01);
+        $SR->code() ? $this->success($SR->data()) : $this->error($SR->msg());
     }
 
     function sms()

+ 40 - 22
application/api/service/WxService.php

@@ -309,28 +309,46 @@ class WxService extends BaseService
 
     public function appPay($openid, $out_trade_no, $description, $amount)
     {
-        $config = WxService::wxAppConfig();
-        $body = [
-            'appid' => $config["appid"],
-            'mchid' => $config['mch_id'],
-            'description' => $description,
-            'out_trade_no' => $out_trade_no,
-            'notify_url' => "https://pbbaby.xunsoftware.com/api.php/api/callback/wxpay",
-            'amount' => [
-                'total' => fixed2Float($amount * 100),
-                'currency' => 'CNY'
-            ],
-            'payer' => [
-                "openid" => $openid
-            ]
-        ];
-        return $this->ok([
-            'appId' => $config['appid'],
-            'timeStamp' => time(),
-            'nonceStr' => Formatter::nonce(),
-            'package' => 'prepay_id=21435325234534sdfdhasdg123',
-            'signType' => 'RSA'
-        ]);
+        try {
+            $config = WxService::wxAppConfig();
+            $resp = $this->instance("app")
+                ->chain('v3/pay/transactions/app')
+                ->post(['json' => [
+                    'appid' => $config["appid"],
+                    'mchid' => $config['mch_id'],
+                    'description' => $description,
+                    'out_trade_no' => $out_trade_no,
+                    'notify_url' => "https://pbbaby.xunsoftware.com/api.php/api/callback/wxpay",
+                    'amount' => [
+                        'total' => fixed2Float($amount * 100),
+                        'currency' => 'CNY'
+                    ],
+//                    'payer' => [
+//                        "openid" => $openid
+//                    ]
+                ]]);
+            if (200 === $resp->getStatusCode()) {
+                $resp_data = json_decode($resp->getBody()->__toString(), true);
+                $res = [
+                    'appId' => $config['appid'],
+                    'timeStamp' => time(),
+                    'nonceStr' => Formatter::nonce(),
+                    'package' => 'prepay_id=' . $resp_data['prepay_id'],
+                    'signType' => 'RSA'
+                ];
+                $sign = $this->sign($res['appId'] . "\n" . $res["timeStamp"] . "\n" . $res["nonceStr"] . "\n" . $res["package"] . "\n", $config["mch_private_key_file_path"]);
+                $res["paySign"] = $sign;
+                return $this->ok($res);
+            }
+            return $this->fail("发起支付失败!");
+        } catch (\Exception $e) {
+            // 进行错误处理
+            if ($e instanceof \GuzzleHttp\Exception\RequestException && $e->hasResponse()) {
+                $r = $e->getResponse();
+                return $this->fail($r->getBody()->__toString());
+            }
+            return $this->fail($e->getMessage());
+        }
     }
 
     public function appRefund($out_trade_no, $description, $total_amount, $refund_amount)