在何处查找结算协议ID,然后在结帐时使用

时间:2017-02-15 13:04:27

标签: php paypal paypal-sandbox

大家好,我很困惑在用户批准结算方案后,在哪里可以找到结算协议ID(BAID)。

使用REST-API

到目前为止,paypal在执行协议后返回此内容。

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 个答案:

答案 0 :(得分:1)

是的,执行协议后返回对象中的ID将是结算协议ID,可用于稍后查找有关协议的信息。

以下是另一个可能有用的示例:https://github.com/paypal/PayPal-PHP-SDK/blob/master/sample/billing/ExecuteAgreement.php