request->post(); $rule = new Validate([ "article_id"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } if(isset($data['type'])&&$data['type']==2){ $payType = 2; }else{ $payType = 0; } $frag = []; $article_info = $this->article_obj->where("article_id",$data['article_id'])->find(); if($article_info['is_buy']==1){ return jsonError("paid"); } $journal_info = $this->journal_obj->where("journal_id",$article_info['journal_id'])->find(); $check = $this->order_obj->where("user_id",$article_info['user_id'])->where("article_id",$data['article_id'])->whereIn("state",[0,1])->find(); if($check){ if($payType==0){ $ii = $this->createPaypalOrder($check['real_fee']); // if(!isset($ii['jsonResponse']['status'])||$ii['jsonResponse']['status']!="CREATED"){ if(!isset($ii['jsonResponse']['status'])){ return jsonError("system error!"); }else{ $check['paypal_order_id'] = $ii['jsonResponse']['id']; } $check['pay_type'] = 0; $this->order_obj->update($check); $re['detail'] = $check; $re['paypal'] = $ii; $re['paystation'] = null; return jsonSuccess($re); }else{ $cache_sn = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8))); $accessToken = createPayStationToken(); $data_array = [ 'paystation_id' => Env::get("paystation.client_id"), 'gateway_id' => "PAYSTATION",//GATEWAY_ID, "order_name" => $cache_sn, "amount" =>100, "test_mode"=>true ]; $data = json_encode($data_array); $purchase = postPayStationQuery('v1/payme/purchases', $accessToken, $data); $paystation_res = object_to_array(json_decode($purchase)); $check['pay_type'] = 2; $check['paystation_url'] = $paystation_res['payme_url']; $this->order_obj->update($check); $re['detail'] = $check; $re['paypal'] = null; $re['paystation'] = $paystation_res; return jsonSuccess($re); } } if($payType==0){//支付方式为paypal $insert['order_sn'] = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8))); $insert['user_id'] = $article_info['user_id']; $insert['type'] = 0; $insert["article_id"] = $data['article_id']; $insert['order_fee'] = $journal_info['fee']; $insert['real_fee'] = $journal_info['fee']; $frag["paypal"] = $this->createPaypalOrder($insert['real_fee']); if(!isset($paypal['jsonResponse']['status'])||$paypal['jsonResponse']['status']!="CREATED"){ return jsonError("system error"); }else{ $insert['paypal_order_id'] = $paypal['jsonResponse']['id']; } $insert['ctime'] = time(); $id = $this->order_obj->insertGetId($insert); $frag['paystation'] = null; }elseif ($payType==2){//支付方式为paystation $ca_sn = 'TMR'.date('Ymd') . strtoupper(bin2hex(random_bytes(8))); $insert['order_sn'] = $ca_sn; $insert['user_id'] = $article_info['user_id']; $insert['type'] = 2; $insert["article_id"] = $data['article_id']; $insert['order_fee'] = $journal_info['fee']; $insert['real_fee'] = $journal_info['fee']; $accessToken = createPayStationToken(); $data_array = [ 'paystation_id' => Env::get("paystation.client_id"), 'gateway_id' => "PAYSTATION",//GATEWAY_ID, "order_name" => $ca_sn, "amount" =>100, "test_mode"=>true ]; $data = json_encode($data_array); $purchase = postPayStationQuery('v1/payme/purchases', $accessToken, $data); $paystation_res = object_to_array(json_decode($purchase)); $insert['paystation_url'] = $paystation_res['payme_url']; $insert['ctime'] = time(); $id = $this->order_obj->insertGetId($insert); $frag['paystation'] = $paystation_res; $frag["paypal"] = null; }else{//暂时不处理,其他情况 return jsonError("pay_type error"); } $frag['detail'] = $this->order_obj->where("order_id",$id)->find(); return jsonSuccess($frag); } public function getUserOrder(){ $data = $this->request->post(); $rule = new Validate([ "user_id"=>"require", "state"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $list = $this->order_obj->where("user_id",$data['user_id'])->where("state",$data['state'])->select(); foreach ($list as $k=>$v){ $article = $this->article_obj->where("article_id",$v['article_id'])->find(); $list[$k]['article_detail'] = $article; $list[$k]['journal_detail'] = $this->journal_obj->where("journal_id",$article['journal_id'])->find(); } $re['list'] = $list; return jsonSuccess($re); } private function handleResponse($response) { $jsonResponse = json_decode($response->getBody(), true); return [ "jsonResponse" => $jsonResponse, "httpStatusCode" => $response->getStatusCode(), ]; } public function preOrderDetail(){ $data = $this->request->post(); $rule = new Validate([ "article_id"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $article_info = $this->article_obj->where("article_id",$data['article_id'])->find(); $journal_info = $this->journal_obj->where("journal_id",$article_info['journal_id'])->find(); $user_info = $this->user_obj->where("user_id",$article_info['user_id'])->find(); $re['article_detail'] = $article_info; $re['journal_detail'] = $journal_info; $re['user_detail'] = $user_info; return jsonSuccess($re); } public function mytt(){ $re = $this->createPaypalOrder(600); return jsonSuccess($re); } private function createPaypalOrder($fee) { $client = $this->createClient(); $orderBody = [ "body" => OrderRequestBuilder::init("CAPTURE", [ PurchaseUnitRequestBuilder::init( AmountWithBreakdownBuilder::init("USD", $fee)->build() )->build(), ]) ->paymentSource( PaymentSourceBuilder::init()->paypal( PaypalWalletBuilder::init()->experienceContext( PaypalWalletExperienceContextBuilder::init()->returnUrl("https://www.baidu.com")->build() )->build() )->build() ) ->build(), ]; $apiResponse = $client->getOrdersController()->ordersCreate($orderBody); return $this->handleResponse($apiResponse); } public function testCreat(){ $client = PaypalServerSdkClientBuilder::init() ->clientCredentialsAuthCredentials( ClientCredentialsAuthCredentialsBuilder::init( "AeTurTFJvsivep-zB7vigyuBHX_cyzLqY5K0GCHLaOrs0eHaXA5V-fwJoVEFjlvN1jK_IqYoEbKEBKiH", "EA-F_RYl1oJBTjHYn87L7vnNv-mWp5yRyTjmkVCOD_bu5T9nfm0E6rfNRKALj1n6AH70QKqB-mmO6tsE" ) ) ->environment(Environment::SANDBOX) ->build(); $orderBody = [ "body"=> OrderRequestBuilder::init("CAPTURE",[ ] )->build() ]; } public function completeOrder(){ $data = $this->request->post(); $rule = new Validate([ "order_id"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $order_info = $this->order_obj->where("order_id",$data['order_id'])->find(); $this->captureOrder($order_info['paypal_order_id']); $this->article_obj->where("article_id",$order_info['article_id'])->update(['is_buy'=>1]); $this->order_obj->where("order_id",$data['order_id'])->update(['state'=>1]); return jsonSuccess([]); } public function mytest(){ $data = $this->request->post(); $rule = new Validate([ "order_id"=>"require" ]); if(!$rule->check($data)){ return jsonError($rule->getError()); } $info = $this->order_obj->where("order_id",$data['order_id'])->find(); $re = $this->getOrderStatus($info['paypal_order_id']); return jsonSuccess($re); } private function getOrderStatus($orderId){ $client = $this->createClient(); return $client->getOrdersController()->ordersGet(["id"=>$orderId]); } private function createClient(){ return PaypalServerSdkClientBuilder::init() ->clientCredentialsAuthCredentials( ClientCredentialsAuthCredentialsBuilder::init( $this->PAYPAL_CLIENT_ID, $this->PAYPAL_CLIENT_SECRET ) ) ->environment(Environment::SANDBOX) ->build(); } private function captureOrder($orderID) { $client = $this->createClient(); $captureBody = [ "id" => $orderID, ]; $apiResponse = $client->getOrdersController()->ordersCapture($captureBody); return $this->handleResponse($apiResponse); } }