composer require paypal/paypal-checkout-sdk
<?php namespace app\controller; use app\BaseController; use PayPalCheckoutSdk\Core\PayPalHttpClient; use PayPalCheckoutSdk\Core\SandboxEnvironment; use PayPalCheckoutSdk\Orders\OrdersCreateRequest; use PayPalCheckoutSdk\Orders\OrdersCaptureRequest; use PayPalCheckoutSdk\Orders\OrdersGetRequest; use PayPalCheckoutSdk\Payments\CapturesRefundRequest; use PayPalCheckoutSdk\Core\ProductionEnvironment; use Sample\PayPalClient; use think\facade\Request; class Index extends BaseController { // 支付申请 public function test() { $clientId = ""; $clientSecret = ""; // 沙箱环境 $environment = new SandboxEnvironment($clientId, $clientSecret); // 正式环境 // $environment = new ProductionEnvironment($clientId, $clientSecret); $client = new PayPalHttpClient($environment); $request = new OrdersCreateRequest(); $request->prefer('return=representation'); $request->body = [ "intent" => "CAPTURE", "purchase_units" => [[ "reference_id" => "order_num_12345", "amount" => [ "value" => "10.00", "currency_code" => "USD" ] ]], "application_context" => [ // "cancel_url" => "https://example.com/cancel", // "return_url" => "https://example.com/return", "cancel_url" => "http://www.demo.com/index/cancel", // 取消地址 "return_url" => "http://www.demo.com/index/callBack?order_num=order_num_12345", // 同步通知地址 ] ]; try { // Call API with your client and get a response for your call $response = $client->execute($request); // If call returns body in response, you can get the deserialized version from the result attribute of the response echo "
"; print_r($response); // 返回跳转链接地址 if ($response->statusCode == 201) { return $response->result->links[1]->href ?? ""; } } catch (HttpException $ex) { echo $ex->statusCode; print_r($ex->getMessage()); } } // 同步通知,确认支付 public function callBack() { // $request = new OrdersCaptureRequest("APPROVED-ORDER-ID"); // $request->prefer('return=representation'); $token = trim($_GET['token']); $clientId = ""; $clientSecret = ""; // 沙箱环境 $environment = new SandboxEnvironment($clientId, $clientSecret); // 正式环境 // $environment = new ProductionEnvironment($clientId, $clientSecret); $client = new PayPalHttpClient($environment); //先判断订单当前状态-防止订单已经是已完成状态,再次扣款 $request = new OrdersGetRequest($token); $response = $client->execute($request); try { $status = $response->result->status; switch ($status) { case 'COMPLETED': //订单已经完成 // break; case 'APPROVED': //获取支付金额 $captureRequest = new OrdersCaptureRequest($token); $captureRequest->prefer('return=representation'); //简洁参数-获取更多参数用return=representation $captureResponse = $client->execute($captureRequest); // 可以在此根据返回结果处理业务逻辑也可以在异步通知中处理 echo "
"; print_r($captureResponse); $response = json_decode(json_encode($captureResponse), true); if (isset($response['result']['status']) && $response['result']['status'] == "COMPLETED") { echo 1; echo "\n\r"; echo $response['result']['purchase_units']['0']['payments']['captures']['0']['id']; } break; default: //其他 break; } // header("Location: https://www.mrchip.cn/"); // exit; return 'success'; } catch (HttpException $ex) { echo $ex->statusCode; print_r($ex->getMessage()); } return 'fail'; } public function cancel() { return 'cancel'; } // 异步通知 public function notifyCallback() { $res = Request::param(); apiLog('异步:' . json_encode($res)); } // 退款 public function refund() { // token 同步通知结果下 result->purchase_units->0->payments->captures->0->id // 异步通知结果下 resource->payments->captures->id $token = '62205066254602043'; // $request = new CapturesRefundRequest($token); $clientId = ""; $clientSecret = ""; // 沙箱环境 $environment = new SandboxEnvironment($clientId, $clientSecret); // 正式环境 // $environment = new ProductionEnvironment($clientId, $clientSecret); $client = new PayPalHttpClient($environment); // $response = $client->execute($request); // echo "
"; // print_r($response); $request = new CapturesRefundRequest($token); $body = array( 'amount' => array( 'value' => '1.00', 'currency_code' => 'USD' ) ); $request->body = $body; // $client = PayPalClient::client(); $response = $client->execute($request); $responseArray = json_decode(json_encode($response), true); // 退款成功 if (isset($responseArray['result']['status']) && $responseArray['result']['status'] == 'COMPLETED') { // 处理业务逻辑 return json(['code' => 1, 'msg' => "退款成功", 'data' => []]); } return json(['code' => 0, 'msg' => "退款失败", 'data' => []]); } }
发表评论 取消回复