我正在使用laravel集成paypal api,而不是想要在成功付款后更改pay_status,首先通过表中的url发送数据到paypal创建订单表时返回url更改payment_status相同的ID。
我通过了一笔付款,并使用PaymentId,令牌和PayerID重定向回了paypal_success.php。我将如何使用这些接收有关交易的信息。我希望确保它确实发生了
use Illuminate\Http\Request;
use App\Cart;
use Session;
use App\Order;
use PayPal\Rest\ApiContext;
use PayPal\Auth\OAuthTokenCredential;
use PayPal\Api\Amount;
use PayPal\Api\Details;
use PayPal\Api\Item;
use PayPal\Api\ItemList;
use PayPal\Api\Payer;
use PayPal\Api\Payment;
use PayPal\Api\RedirectUrls;
use PayPal\Api\Transaction;
use PayPal\Api\PaymentExecution;
use App\Http\Requests\StoreOrder;
use DB;
class PaymentController extends Controller{
//
private $apiContext;
private $secret;
private $clientId;
public $id;
public function __construct(){
if(config('paypal.settings.mode') == 'live'){
$this->clientId = config('paypal.live_client_id');
$this->secret = config('paypal.live_secret');
} else{
$this->clientId = config('paypal.sandbox_client_id');
$this->secret = config('paypal.sandbox_secret');
}
$this->apiContext = new ApiContext(new OAuthTokenCredential($this->clientId, $this->secret));
$this->apiContext->setConfig(config('paypal.settings'));
}
public function paywithPaypal(Request $request){
$itemname = $request->input('item_name');
$price = $request->input('amount');
$final_price = $request->input('final');
$payer = new Payer();
$payer->setPaymentMethod("paypal");
$item = new Item();
$item->setName($itemname)
->setCurrency('INR')
->setQuantity(1)
->setDescription("This is item description")
->setPrice($final_price);
$itemList = new ItemList();
$itemList->setItems([$item]);
$amount = new Amount();
$amount->setCurrency("INR")
->setTotal($final_price);
$oldCart = Session::get('cart');
$cart = new Cart($oldCart);
if($request->has('order_number')){
$latestOrder = Order::orderBy('created_at','DESC')->first();
$final = '#'.str_pad($latestOrder->id + 1, 5, "0", STR_PAD_LEFT);
}
$order = Order::create([
'user_id'=>$request->user_id,
'b_name'=>$request->b_name,
'b_email'=>$request->b_email,
'b_address'=>$request->b_address,
'b_city'=>$request->b_city,
'b_state'=>$request->b_state,
'b_postal_code'=>$request->b_postal_code,
'b_country'=>$request->b_country,
'b_notes'=>$request->b_notes,
'status'=>$request->status,
'cart'=>serialize($cart),
'order_number'=>$final,
'total'=>$request->total,
]);
$transaction = new Transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Buy Something from my site");
$redirectUrls = new RedirectUrls();
$redirectUrls->setReturnUrl('http://localhost/work/erp-project-curopt-2/status')
->setCancelUrl('http://localhost/work/erp-project-curopt-2/canceled');
$payment = new Payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
try{
$payment->create($this->apiContext);
}catch(\PayPal\Exception\PPConectionException $ex){
die($ex);
}
$paymentLink = $payment->getApprovalLink();
return redirect($paymentLink);
}
public function status(Request $request) {
if(empty($request->input('PayerID')) || empty($request->input('token'))){
die('Payment Failed');
}
$paymentId = $request->get('paymentId');
$payment = Payment::get($paymentId, $this->apiContext);
$execution = new PaymentExecution();
$execution->setPayerId($request->input('PayerID'));
$result = $payment->execute($execution, $this->apiContext);
$id = $request->id;
Session::forget('cart');
if($result->getState() == 'approved'){
$status = 'approved';
return view('done', compact('id'))->with([
'status'=> $status,
], compact('updated'));
}
echo 'Payment Failed again';
die($result);
}
public function canceled() {
Session::forget('cart');
$status = 'pending';
return view('fail',['products'])->with([
'status' => $status,
]);
}
// public function addToOrdersTables(Request $request){
// $oldCart = Session::get('cart');
// $cart = new Cart($oldCart);
// if($request->has('order_number')){
// $latestOrder = Order::orderBy('created_at','DESC')->first();
// $final = '#'.str_pad($latestOrder->id + 1, 5, "0", STR_PAD_LEFT);
// }
// $order = Order::create([
// 'user_id'=>$request->user_id,
// 'b_name'=>$request->b_name,
// 'b_email'=>$request->b_email,
// 'b_address'=>$request->b_address,
// 'b_city'=>$request->b_city,
// 'b_state'=>$request->b_state,
// 'b_postal_code'=>$request->b_postal_code,
// 'b_country'=>$request->b_country,
// 'b_notes'=>$request->b_notes,
// 'status'=>$request->status,
// 'cart'=>serialize($cart),
// 'order_number'=>$final,
// 'total'=>$request->total,
// ]);
// if($order){
// return redirect('/payment');
// }
// }
}