paypal rest api订阅/计费方案和即时付款登录

时间:2018-06-19 06:39:45

标签: php rest api paypal

美好的一天,

请帮助我解决我的问题,希望有人可以。

我有一个电子商务网站,一次接受即时付款和订阅。

请点击链接查看img: https://gyazo.com/fca7c2d2f166537f6077001f62f06417

我确实有PHP代码,可以单独支付即时付款和订阅付款。 但两者都确实单独登录。

即时付款代码:

public static function restNormalPay($aParam){

    define('BASE_URL', Router::getUrl( 'buy-credit' ));
    $clientid = 'xxx';
    $clientsecret = 'xxx';
    $apiContext = new ApiContext(new OAuthTokenCredential($clientid, $clientsecret));
    // $apiContext = self::getApiContext();

    $product = 'Credit';
    $price = (float) 1.00;
    $quant = (float)$aParam['quantity'];
    $shipping = (float) 0.00;
    $tax = (float) 0.00;
    $subtotal = $price * $quant;
    $total = $subtotal + $shipping + $tax;

    $payer = new Payer();
    $payer->setPaymentMethod('paypal');

    $item = new Item();
    $item->setName($product);
    $item->setCurrency('USD');
    $item->setQuantity($quant);
    $item->setPrice($price);

    $itemList = new ItemList();
    $itemList->setItems([$item]);

    $details = new Details();
    $details->setTax($tax);
    $details->setShipping($shipping);
    $details->setSubtotal($price * $quant);

    $amount = new Amount();
    $amount->setCurrency('USD');
    $amount->setTotal($total);
    $amount->setDetails($details);

    $transaction = new Transaction();
    $transaction->setItemList($itemList);
    $transaction->setAmount($amount);
    $transaction->setDescription('Sessions');
    $transaction->setCustom('resNor-'.Session::get('idUser'));

    $redirectUrls = new RedirectUrls();
    $redirectUrls->setReturnUrl(Router::getUrl( 'pay', array( 'GET' => array( 'success' => 'true' ) ) ));
    $redirectUrls->setCancelUrl(Router::getUrl( 'pay', array( 'GET' => array( 'success' => 'false' ) ) ));

    $payment = new Payment();
    $payment->setIntent('sale');
    $payment->setPayer($payer);
    $payment->setRedirectUrls($redirectUrls);
    $payment->setTransactions([$transaction]);

    try{

        $payment->create($apiContext);
    } catch (PayPal\Exception\PayPalConnectionException $ex) {
        echo $ex->getCode();
        echo $ex->getData();
    } catch (Exception $ex) {
        die($ex);
    }

    $approvalUrl = $payment->getApprovalLink();
    header("Location: {$approvalUrl}");

    // iva paypal pay normal button
        // Core::loadComponents('Rest');
        // $result = Rest::restNormalPay(Router::$aPost);
}

订阅/结算方案代码

public static function restCreateRecurring($aParam){
    define('BASE_URL', Router::getUrl( 'buy-credit' ));
    $clientid = 'xxx';
    $clientsecret = 'xxx';
    $apiContext = new ApiContext(new OAuthTokenCredential($clientid, $clientsecret));
    // $apiContext = self::getApiContext();

    $plan = new Plan();
    $plan->setName($aParam["planName"])
        ->setDescription(Session::get('idUser'))
        ->setType('fixed');

    $paymentDefinition = new PaymentDefinition();
    $paymentDefinition->setName('Regular Payments')
        ->setType('REGULAR')
        ->setFrequency('Day')
        ->setFrequencyInterval("1")
        ->setCycles("12")
        ->setAmount(new Currency(array('value' => $aParam["planAmount"], 'currency' => 'USD')));

    $chargeModel = new ChargeModel();
    $chargeModel->setType('SHIPPING')
        ->setAmount(new Currency(array('value' => 0, 'currency' => 'USD')));

    $paymentDefinition->setChargeModels(array($chargeModel));

    $merchantPreferences = new MerchantPreferences();
    // $baseUrl = getBaseUrl();
    $merchantPreferences->setReturnUrl(Router::getUrl( 'paypal-view', array( 'GET' => array( 'success' => 'true' ) ) ))
        ->setCancelUrl(Router::getUrl( 'paypal-view', array( 'GET' => array( 'success' => 'false' ) ) ))
        ->setAutoBillAmount("yes")
        ->setInitialFailAmountAction("CONTINUE")
        ->setMaxFailAttempts("0")
        ->setSetupFee(new Currency(array('value' => $aParam["planAmount"], 'currency' => 'USD')));

    $plan->setPaymentDefinitions(array($paymentDefinition));
    $plan->setMerchantPreferences($merchantPreferences);
        try {
            $output = $plan->create($apiContext);
        } catch (PayPal\Exception\PayPalConnectionException $ex) {
            echo $ex->getCode();
            echo $ex->getData();
        }catch (Exception $ex) {
            exit(1);
        }

        try {
            $patch = new Patch();

            $value = new PayPalModel('{
                "state":"ACTIVE"
            }');

            $patch->setOp('replace')
                ->setPath('/')
                ->setValue($value);

            $patchRequest = new PatchRequest();
            $patchRequest->addPatch($patch);

            $output->update($patchRequest, $apiContext);

            $plan = Plan::get($output->getId(), $apiContext);
        } catch (PayPal\Exception\PayPalConnectionException $ex) {
            echo $ex->getCode();
            echo $ex->getData();
        }catch (Exception $ex) {
            exit(1);
        }

        $gDate =  date(DATE_ATOM);
        if(substr($gDate, -11, 2) == 60){
            $add1 = 01;
        }else{
            $add1 = substr($gDate, -11, 2) + 1;
        }

        $rDate = substr_replace($gDate, $add1, -11, 2);

        $agreement = new Agreement();
        $agreement->setName('Basic Agreement')
            ->setDescription('resSub-'.Session::get('idUser'))
            ->setStartDate($rDate);

        // Add Plan ID
        // Please note that the plan Id should be only set in this case.
        $plan = new Plan();
        $plan->setId($output->getId());
        $agreement->setPlan($plan);

        // Add Payer
        $payer = new Payer();
        $payer->setPaymentMethod('paypal');
        $agreement->setPayer($payer);

        // Add Shipping Address
        $shippingAddress = new ShippingAddress();
        $shippingAddress->setLine1($aParam["txtstreet"])
            ->setCity($aParam["txtcity"])
            ->setState($aParam["txtState"])
            ->setPostalCode($aParam["postalCode"])
            ->setCountryCode($aParam["countryCode"]);
        $agreement->setShippingAddress($shippingAddress);

        // ### Create Agreement
        try {
            $agreement = $agreement->create($apiContext);
            $approvalUrl = $agreement->getApprovalLink();
        } catch (PayPal\Exception\PayPalConnectionException $ex) {
            echo $ex->getCode();
            echo $ex->getData();
        }catch (Exception $ex) {
            exit(1);
        }

        header("Location: {$approvalUrl}");

        // iva paypal create recurring button
            // Core::loadComponents('Rest');
            // $result = Rest::restCreateRecurring(Router::$aPost);
}

我想只在一次登录中执行,但我找不到它的任何php示例代码。

请帮帮我:(我还在学习编码。 请耐心等待。

此致 范。

0 个答案:

没有答案