李帅

1.重构合成脚本。

<?php
namespace App\Console\Commands;
use App\Payment\GooglePayment;
use Illuminate\Console\Command;
class DevGoogle extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'dev:gp';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Command description';
/**
* Create a new command instance.
*
* @return void
*/
public function __construct()
{
parent::__construct();
}
/**
* Execute the console command.
*
* @return int
*/
public function handle()
{
$gp = new GooglePayment();
return 0;
}
}
......@@ -33,11 +33,24 @@ class PayController extends Controller
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
* @param \App\Payment\PaymentFactory $factory
* @return \Illuminate\Http\JsonResponse
*/
public function store(Request $request)
public function store(Request $request, PaymentFactory $factory)
{
// 回调
$order_sn = $request->post('order_sn');
$pay_type = $request->post('pay_type');
$token = $request->post('token');
// $order = Order::query()->where('order_sn', $order_sn)->first();
//
// if ($order->status != Order::UNPAID) return Response::fail('订单错误!');
$payment = $factory->init($pay_type)->notify($order_sn, $token);
if ($payment) return Response::success(['order_sn' => $order_sn], '支付成功');
else return Response::fail('订单错误!');
}
/**
......
......@@ -22,11 +22,11 @@ class GooglePayment implements PaymentInterface
const IS_SANDBOX = true;
public $client;
public function __construct()
{
$this->client = $this->getGoogleClient();
}
......@@ -35,4 +35,122 @@ class GooglePayment implements PaymentInterface
{
// 查询订单对应的产品id
}
private function getGoogleClient()
{
// load our config.json that contains our credentials for accessing google's api as a json string
$configJson = public_path().'/client_secret_724392566830-jv6gqcb7vv8q9vavihhu5siccdit35op.apps.googleusercontent.com.json';
// define an application name
$applicationName = 'Parlando一言临境';
// create the client
$client = new \Google_Client();
$client->setApplicationName($applicationName);
$client->setAuthConfig($configJson);
$client->setAccessType('offline'); // necessary for getting the refresh token
$client->setApprovalPrompt ('force'); // necessary for getting the refresh token
// scopes determine what google endpoints we can access. keep it simple for now.
$client->setScopes(
[
\Google\Service\Oauth2::USERINFO_PROFILE,
\Google\Service\Oauth2::USERINFO_EMAIL,
\Google\Service\Oauth2::OPENID,
\Google_Service_AndroidPublisher::ANDROIDPUBLISHER,
\Google\Service\Drive::DRIVE_METADATA_READONLY // allows reading of google drive metadata
]
);
$client->setIncludeGrantedScopes(true);
// google 服务端默认获得受权
$client->useApplicationDefaultCredentials();
// 设置 google client_email
$client->setSubject("client_email");
return $client;
}
public function notify($order_sn, $token)
{
$order = Order::query()->where('order_sn', $order_sn)->first();
if ($order->status != Order::UNPAID) return false;
$validator =new \Google_Service_AndroidPublisher($this->client);
$packageName = 'pub.yiyan.parlando.Parlando';
$productId = 'test.yiyan.vip.1.month';
// $token = '客户端传过来的 支付凭证';
$optps = array();
/**
* 成功会返回
* 返回字段解释 https://developers.google.com/android-publisher/api-ref/purchases/products
*
* 返回字段解释 中文翻译
* consumptionState int 消费类产品的消费状态 0有待消费1已消耗
* developerPayload string onyx系统生成的唯一ID
* kind sgring 购买的对象
* orderId sgring 客户支付订单ID(google play 订单ID)
* purchaseState int 订单的采购状态 0购买1取消
* purchaseTimeMillis int 时间戳
* purchaseType int 扩展字段 0 测试 1促销
* Array(
* [consumptionState] => 1
* [developerPayload] => 你的订单号
* [kind] => androidpublisher#productPurchase
* [orderId] => google play 订单号
* [purchaseState] => 0
* [purchaseTimeMillis] => 1542187625018
* [purchaseType] =>
* )
*/
try {
$resp = $validator->purchases_products->get($packageName, $productId, $token, $optps);
// 对象转数组
$resp = get_object_vars($resp);
} catch (\Exception $e) {
Log::debug('got error = ' . $e->getMessage() . PHP_EOL);
throw new \Exception('got error = ' . $e->getMessage() . PHP_EOL);
}
if ($resp['consumptionState'] != 1) return false;
/** 修改订单状态*/
$order->pay_time = Carbon::now();
$order->status = Order::PAID;
$order->save();
/** 给用户加会员*/
$goods = MembershipGood::query()->find($order->order_goods->goods_id);
if ($goods->limit_unit == '月') {
$days = intval($goods->limit_days) * 30;// 计算天数
} elseif ($goods->limit_unit == '年') {
$days = intval($goods->limit_days) * 365;// 计算天数
} else {
$days = intval($goods->limit_days) * 1;// 计算天数
}
$user = UserProfile::query()->find($order->user_id);
if ($user->is_vip == 0){
$user->is_vip = 1;
$user->create_vip_time = Carbon::now();
$user->expire_vip_time = Carbon::now()->addDays($days);
}else{
if (Carbon::now()->gte($user->expire_vip_time)){ // 已经过期了
$user->expire_vip_time = Carbon::now()->addDays($days);
}else{
$user->expire_vip_time = Carbon::parse($user->expire_vip_time)->addDays($days);
}
}
$user->buy_number += 1;
$user->buy_amount += $order->pay_amount;
$user->last_buy_time = Carbon::now();
$user->save();
/** 修改订单状态*/
$order->status = Order::DONE;
$order->save();
return true;
}
}
\ No newline at end of file
......
......@@ -8,6 +8,7 @@
"php": "^7.3|^8.0",
"dcat/laravel-admin": "2.*",
"fruitcake/laravel-cors": "^2.0",
"google/apiclient": "^2.0",
"guzzlehttp/guzzle": "^7.0.1",
"jiannei/laravel-enum": "^3.0",
"jiannei/laravel-response": "^4.0",
......
{"web":{"client_id":"724392566830-jv6gqcb7vv8q9vavihhu5siccdit35op.apps.googleusercontent.com","project_id":"pc-api-7482901338487549764-603","auth_uri":"https://accounts.google.com/o/oauth2/auth","token_uri":"https://oauth2.googleapis.com/token","auth_provider_x509_cert_url":"https://www.googleapis.com/oauth2/v1/certs","client_secret":"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","redirect_uris":["https://www.yiyan.pub/auth/google/callback"],"javascript_origins":["https://www.yiyan.pub","http://www.yiyan.pub"]}}
\ No newline at end of file