setSignType('RSA2'); $gateway->setAppId($partner_id); // $gateway->setSellerId($partner_id); $gateway->setPrivateKey($app_private_key); $gateway->setAlipayPublicKey($alipay_public_key); $gateway->setNotifyUrl($notify_url); $this->gateway = $gateway; $this->love_app_id = '2021001147602775'; $this->love_pid = "2088731994488878"; $this->love_private_key = $app_private_key; } public function purchase($subject, $out_trade_no, $total_fee) { $total_fee = number_format($total_fee, 2); $request = $this->gateway->purchase(); // $request->setBizContent([ // 'subject' => $subject, // 'out_trade_no' => $out_trade_no, // 'total_amount' => $total_fee, // 'product_code' => 'QUICK_MSECURITY_PAY', // ]); $request->setBizContent([ 'subject' => $subject, 'out_trade_no' => $out_trade_no, //date('YmdHis') . mt_rand(1000, 9999), 'total_amount' => $total_fee, 'product_code' => 'QUICK_MSECURITY_PAY', ]); /** * @var LegacyAppPurchaseResponse $response */ $response = $request->send(); $orderString = $response->getOrderString(); return $orderString; // if ($orderString) { // $result = $this->alipayReturn($orderString); // return $result; // } } public function alipayReturn(Request $request) { $data = $request->input('data'); $request = $this->gateway->completePurchase(); $request->setParams([ 'memo' => '', 'result' => $data, 'resultStatus' => '9000' ]); /** * @var LegacyCompletePurchaseResponse $response */ try { $response = $request->send(); if($response->isPaid()){ /** * Payment is successful */ }else{ /** * Payment is not successful */ } } catch (Exception $e) { /** * Payment is not successful */ } } public function notify(Request $request) { // $tade_no = $request-> /** * @var LegacyCompletePurchaseResponse $response */ try { $response = $this->gateway->completePurchase()->setParams($_POST)->send(); } catch (InvalidRequestException $e) { /** * Payment is not successful */ die('fail'); } if ($response->isPaid()) { /** Payment is successful */ die('success'); //The response should be 'success' only } else { /** Payment is not successful */ die('fail'); } } /** * InfoStr APP登录需要的的infostr * * @return String */ public function infoStr() { $infoStr = http_build_query([ 'apiname' => 'com.alipay.account.auth', 'method' => 'alipay.open.auth.sdk.code.get', 'app_id' => $this->love_app_id, 'app_name' => '福恋智能', 'biz_type' => 'openservice', 'pid' => $this->love_pid, 'product_id' => 'APP_FAST_LOGIN', 'scope' => 'kuaijie', 'target_id' => mt_rand(999, 99999), //商户标识该次用户授权请求的ID,该值在商户端应保持唯一 'auth_type' => 'AUTHACCOUNT', // AUTHACCOUNT代表授权;LOGIN代表登录 'sign_type' => 'RSA2', ]); $infoStr .= '&sign='.$this->enRSA2($infoStr); return $infoStr; } /** * enRSA2 RSA加密 * * @param String $data * @return String */ private function enRSA2($data) { $str = chunk_split(trim($this->love_private_key), 64, "\n"); $key = "-----BEGIN RSA PRIVATE KEY-----\n$str-----END RSA PRIVATE KEY-----\n"; // $key = file_get_contents(storage_path('rsa_private_key.pem')); 为文件时这样引入 $signature = ''; $signature = openssl_sign($data, $signature, $key, OPENSSL_ALGO_SHA256)?base64_encode($signature):NULL; return $signature; } }