李帅

我已参透了PayPal。

......@@ -73,6 +73,8 @@ class OrderController extends Controller
return DB::transaction(function ()use ($user_id, $member_id, $source, $number){
// 获取商品信息
$membership_good = MembershipGood::query()->where('id',$member_id)->first();
if (!$membership_good) throw new \Exception('没有此商品');
$membership = $membership_good->membership()->first();
// 实付金额 = 商品金额
......
......@@ -37,18 +37,25 @@ class PayController extends Controller
*/
public function store(Request $request)
{
//
// 回调
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
* @param Request $request
* @param PaymentFactory $factory
* @return \Illuminate\Http\JsonResponse
* @throws \Exception
*/
public function show($id)
public function show($id, Request $request, PaymentFactory $factory)
{
//
$pay_type = $request->get('pay_type');
$capture = $factory->init($pay_type)->capture($id);
return Response::success($capture);
}
/**
......
......@@ -17,11 +17,10 @@ class PaypalPayment implements PaymentInterface
const IS_SANDBOX = true;
public $authUrl = 'https://api-m.sandbox.paypal.com/v1/oauth2/token';
public $baseUrl = 'https://api-m.paypal.com';
public $paySandboxUrl = 'https://api-m.sandbox.paypal.com';
public $baseUrlSandbox = 'https://api-m.sandbox.paypal.com';
public $payUrl = 'https://api-m.paypal.com';
public $clientId = 'AdDRE91WSp5q1fYLODpJduc2mRjA_v6E205SvkfVSOgvr98xLeyDCHY4OPAaSFMK1SHYOfJ4TksHSX1-';
public $secret = 'EDoy_PVrFyobWt9DzAMRMikJwWCXenkWSx9CGFz0MxHt3a8fs6v-LnORMilIbftb2GwBKxOoTVZNBHNR';
......@@ -38,13 +37,14 @@ class PaypalPayment implements PaymentInterface
$this->accessToken = $access_token;
}else{
$client = new Client([
'base_uri' => $this->baseUrlSandbox,
'headers'=>[
'Content-Type' => 'application/x-www-form-urlencoded',
'Accept'=>'application/json',
]
]);
$response = $client->post($this->authUrl,[
$response = $client->post('/v1/oauth2/token',[
'form_params'=>['grant_type' => 'client_credentials'],
'auth' => [$this->clientId, $this->secret],
]);
......@@ -56,11 +56,87 @@ class PaypalPayment implements PaymentInterface
}
}
/**
* Setting up the JSON request body for creating the Order. The Intent in the
* request body should be set as "CAPTURE" for capture intent flow.
* @param Order $order
* @return array
*/
private static function buildCreateOrderRequestBody(Order $order)
{
return array(
'intent' => 'CAPTURE', //CAPTURE 商家打算在客户付款后立即扣款。 AUTHORIZE 商家打算在客户付款后授权付款并暂停资金
'application_context' =>
array(
'return_url' => 'https://example.com/return', // todo
'cancel_url' => 'https://example.com/cancel', // todo 这里要询问reason app是否有schema,
'brand_name' => 'Parlando', //覆盖 PayPal 网站上 PayPal 帐户中公司名称的标签。
'locale' => 'en-US', //zh-CN
'landing_page' => 'NO_PREFERENCE', //LOGIN / BILLING / NO_PREFERENCE
'shipping_preference' => 'NO_SHIPPING', //运输偏好
'user_action' => 'PAY_NOW',
),
'purchase_units' =>
array(
0 =>
array(
'description' => 'Parlando 3 Month Vip', // 购买说明
'custom_id' => 'osnxxxxxx123', //API 调用者提供的外部 ID 可以理解为order_id osnxxxxxx
'soft_descriptor' => 'PayPal Parlando Vip', // 出现在付款人卡对帐单上的对帐单描述符的动态文本 最大长度:22.
'amount' =>
array(
'currency_code' => 'USD', // CNY
'value' => '220.00', //
),
),
),
);
}
public function prepare(Order $order)
{
// 在PayPal上创建一个订单,它会返回一个订单对象,它有一个订单id
$client = new Client([
'base_uri' => $this->baseUrlSandbox,
'headers'=>[
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->accessToken,
'Prefer' => 'return=representation',
]
]);
$response = $client->post('/v2/checkout/orders',[
'json' => self::buildCreateOrderRequestBody($order),
]);
$body = $response->getBody();
$content = json_decode($body->getContents(),true);
return $content;
}
public function capture($orderId)
{
$client = new Client([
'base_uri' => $this->baseUrlSandbox,
'headers'=>[
'Content-Type' => 'application/json',
'Authorization' => 'Bearer ' . $this->accessToken,
'Prefer' => 'return=representation',
]
]);
$response = $client->post('/v2/checkout/orders/' . $orderId . '/capture');
$body = $response->getBody();
$content = json_decode($body->getContents(),true);
// todo 应该使用队列,异步执行回调程序
return $content;
}
public function notify()
{
return $this->accessToken;
}
}
\ No newline at end of file
......