2017-02-15 61 views
1

大家好,我很困惑在用戶批准帳單計劃之後到哪裏查找帳單協議ID(BAID)。在哪裏可以找到帳單協議ID,然後在結帳時使用

使用REST的API

這之後我執行該協議至今貝寶的回報。

Agreement {#346 ▼ 
    -_propMap: array:9 [▼ 
    "id" => "I-N61SE6562BGN" 
    "state" => "Active" 
    "description" => "This is a subscription fee for using Talent Scout. It will be use to maintain the site and make users secure." 
    "payer" => Payer {#349 ▶} 
    "plan" => Plan {#350 ▶} 
    "links" => array:5 [▶] 
    "start_date" => "2019-06-17T07:00:00Z" 
    "shipping_address" => Address {#364 ▶} 
    "agreement_details" => AgreementDetails {#365 ▶} 
    ] 
} 

如果上述代碼中的ID是BAID,我很困惑。如果是我正在查找的BAID,那麼我想將其插入到結帳中,以便用戶在將來結賬時不必再次登錄。

代碼,當用戶選擇,並檢查出

$payer = new Payer(); 
    $payer->setPaymentMethod('paypal'); 
    $total = 0; 
    Session::set('duration', $request['duration']); 
    if($request['duration'] == '1'){ 
     $item_1 = new Item(); 
     $item_1->setName('1 week duration') // item name 
     ->setCurrency('PHP') 
     ->setQuantity(1) 
     ->setPrice($request['hiddenprice'][0]); // unit price 
     $total = $request['hiddenprice'][0]; 
    } 
    elseif($request['duration'] == '2'){ 
     $item_1 = new Item(); 
     $item_1->setName('2 weeks duration') // item name 
     ->setCurrency('PHP') 
     ->setQuantity(1) 
     ->setPrice($request['hiddenprice'][1]); // unit price 
     $total = $request['hiddenprice'][1]; 
    } 
    elseif ($request['duration'] == '3') { 
     $item_1 = new Item(); 
     $item_1->setName('3 weeks duration') // item name 
     ->setCurrency('PHP') 
     ->setQuantity(1) 
     ->setPrice($request['hiddenprice'][2]); // unit price 
     $total = $request['hiddenprice'][2]; 
    } 

    // add item to list 
    $item_list = new ItemList(); 
    $item_list->setItems(array($item_1)); 
    $amount = new Amount(); 
    $amount->setCurrency('PHP') 
     ->setTotal($total); 
    $transaction = new Transaction(); 
    $transaction->setAmount($amount) 
     ->setItemList($item_list) 
     ->setDescription('Your transaction description'); 
    $redirect_urls = new RedirectUrls(); 
    $redirect_urls->setReturnUrl(\URL::route('payment.status')) 
     ->setCancelUrl(\URL::route('payment.status')); 
    $payment = new Payment(); 
    $payment->setIntent('Sale') 
     ->setPayer($payer) 
     ->setRedirectUrls($redirect_urls) 
     ->setTransactions(array($transaction)); 
    try { 
     $payment->create($this->_api_context); 
    } catch (\PayPal\Exception\PPConnectionException $ex) { 
     if (\Config::get('app.debug')) { 
      echo "Exception: " . $ex->getMessage() . PHP_EOL; 
      $err_data = json_decode($ex->getData(), true); 
      exit; 
     } else { 
      die('Some error occur, sorry for inconvenient'); 
     } 
    } 
    foreach($payment->getLinks() as $link) { 
     if($link->getRel() == 'approval_url') { 
      $redirect_url = $link->getHref(); 
      break; 
     } 
    } 
    // add payment ID to session 
    Session::put('paypal_payment_id', $payment->getId()); 
    if(isset($redirect_url)) { 
     // redirect to paypal 
     return Redirect::away($redirect_url); 
    } 
    Session::flash('failed', 'Something went wrong!'); 
    return Redirect::to('/home') 
     ->with('error', 'Unknown error occurred'); 
    } 

代碼時,付款成功

// Get the payment ID before session clear 
    $payment_id = Session::get('paypal_payment_id'); 
    // clear the session payment ID 
    Session::forget('paypal_payment_id'); 
    if (empty(Input::get('PayerID')) || empty(Input::get('token'))) { 
     Session::flash('failed', 'Something went wrong!'); 
     return Redirect::to('/paymentprocess') 
      ->with('error', 'Payment failed'); 
    } 
    $payment = Payment::get($payment_id, $this->_api_context); 
    // PaymentExecution object includes information necessary 
    // to execute a PayPal account payment. 
    // The payer_id is added to the request query parameters 
    // when the user is redirected from paypal back to your site 
    $execution = new PaymentExecution(); 
    $execution->setPayerId(Input::get('PayerID')); 

    //Execute the payment 
    $result = $payment->execute($execution, $this->_api_context); 
    // echo '<pre>';print_r($result);echo '</pre>';exit; // DEBUG RESULT, remove it later 
    // dd($result->id); 
    if ($result->getState() == 'approved') { // payment made 
     $data = new Paypalpayment; 
     $user = User::find(Session::get('id')); 
     $data->id = null; 
     $data->payment_id = $result->id; 
     $data->user_id = Session::get('id'); 
     $data->firstname = $user['firstname']; 
     $data->lastname = $user['lastname']; 
     $data->state = 'pending'; 
     $data->duration = Session::get('duration'); 
     $data->save(); 
     Session::forget('duration'); 
     Session::flash('success', 'Payment Successful!'); 
     return Redirect::to('/paymentprocess') 
      ->with('success', 'Payment success'); 
    } 
    Session::flash('failed', 'Payment failed!!'); 
    return Redirect::to('/paymentprocess') 
     ->with('error', 'Payment failed'); 

回答

1

是,該ID在返回的對象執行將協議後是賬單協議ID,可用於稍後查詢有關協議的信息。

下面是可能有所幫助的另一個示例:https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/billing/ExecuteAgreement.php

+0

我明白了。 Paypal-REST API是否有引用事務?我似乎無法找到它。我打算使用這個函數https://developer.paypal.com/docs/classic/api/merchant/DoReferenceTransaction_API_Operation_NVP/ –

+0

是的,你會想看看計費計劃(https://developer.paypal.com/docs/integration/direct/billing-plans /)和結算協議(https://developer.paypal.com/docs/integration/direct/billing-agreements/)。計費計劃設置了訂閱的架構,並且該協議將一個或多個用戶綁定到計費計劃詳細信息 –

+0

是的,我做了兩個,但是我通過https://github.com/paypal/PayPal-PHP -SDK,他們說它不存在。我被迫採取替代方式來解決我的問題。謝謝 –

相關問題