paypal / PayPal-PHP-SDK

PHP SDK for PayPal RESTful APIs
https://developer.paypal.com/docs/api/
Other
27 stars 100 forks source link

How I can implement recurring payments with credit cards? #567

Closed malik-umair closed 8 years ago

malik-umair commented 8 years ago

I'm using paypal rest API sdk in laravel and want to know how I can implement recurring payment implementation in it with Credit cards? I want to create user's recurring profile where i can set frequency and other thing to set next billing date for this same credit card. Unfortunately i didn't found any example for this. Below is my code in which I've implemented payments through credit cards, it's working but I want it to modify so that I can set set next billing date on which next payment will be deducted.

$addr = new Address();
    $addr->setLine1($dataPaypal['addressLineOne']);
    $addr->setCity($dataPaypal['city']);
    $addr->setCountryCode('US');
    $addr->setPostalCode($dataPaypal['postalCode']);
    $addr->setState($dataPaypal['state_']);

    $card = new CreditCard();
    $card->setNumber($dataPaypal['ccNumber']);
    $card->setType($dataPaypal['ccType']);
    $card->setExpireMonth($dataPaypal['ccExpMonth']);
    $card->setExpireYear($dataPaypal['ccExpYear']);
    $card->setCvv2($dataPaypal['ccCode']);
    $card->setFirstName($dataPaypal['firstName']);
    $card->setLastName($dataPaypal['lastName']);
    $card->setBillingAddress($addr);

    $fi = new FundingInstrument();
    $fi->setCreditCard($card);

    $payer = new Payer();
    $payer->setPaymentMethod('credit_card');
    $payer->setFundingInstruments(array($fi));

    $amount = new Amount();
    $amount->setCurrency('USD');
    $amount->setTotal($dataPaypal['transactionAmount']);

    $transaction = new Transaction();
    $transaction->setAmount($amount);
    $transaction->setDescription('This is the payment transaction description for payment id - ' . $paymentId);

    $payment = new Payment();
    $payment->setIntent('sale');
    $payment->setPayer($payer);
    $payment->setTransactions(array($transaction));

    try {
        $payment->create($this->_api_context);
        if ($payment->getState() == 'approved') {
            if ($paymentType == 'fullPayment') {
                $dataPayment = array(
                    'transactionNo' => $payment->transactions[0]->related_resources[0]->sale->id,
                    'paypalTransactionDate' => $createdDate,
                    'paypalStatus' => 'completed'
                );
            }
        } else {
            $dataPayment = array(
                'transactionNo' => $payment->transactions[0]->related_resources[0]->sale->id,
                'paypalTransactionDate' => $createdDate,
                'paypalStatus' => $payment->getState()
            );
        }
        $dataPayment['paymentId'] = $paymentId;
        $id = Payments::UpdatePaymentPaypal($dataPayment);
        return true;
    } 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');
        }
    }
jaypatel512 commented 8 years ago

Do you mean this ? http://paypal.github.io/PayPal-PHP-SDK/sample/#3c000030aaacb17311c98442b5ecb341

malik-umair commented 8 years ago

yes I think, I never implemented recurring payments before that's why I'm not sure. I tried the example given in your link http://paypal.github.io/PayPal-PHP-SDK/sample/doc/billing/CreateBillingAgreementWithCreditCard.html

I've few questions regarding it please guide me what to do:

1- here it include "UpdatePlan.php" file what's that and where I'll find that? 2- it get plan id from the above file $createdPlan->getId(), I removed that as I don't have that file and give hard coded id "12345" to it. 3- it gives following error after running the script:

{"name":"TEMPLATE_ID_INVALID","details":[{"field":"template id is invalid","issue":"Incorrect Template Id."}

My requirements are:

1- create billing plan for payments so that person is billed after specific time with Credit Card. 2- Page should not be redirected to paypal page, it should stay on my website.

Thanks!

malik-umair commented 8 years ago

Ok I've managed to fix this issue and now plan is creating and I'm also updating it's status to "ACTIVE". But now it's giving another type of error which is:

{"name":"DPRP_DISABLED","message":"DPRP is disabled for this merchant.","information_link":"https://developer.paypal.com/webapps/developer/docs/api/#DPRP_DISABLED","debug_id":"3c022479a74de"}

what's that now? I've followed the reference link you provided above.

Thanks!

braebot commented 8 years ago

@malik-umair, sorry for the late response. Were you able to get past the DPRP_DISABLED error? If not, did you enable recurring payments in the apps portal? If you're having problems with that, I recommend contacting MTS, as they can fix your account/app settings.

malik-umair commented 8 years ago

Everything is enabled. It's creating user profiles, it's taking initial payments but it's not executing recurring payments on mentioned date. Everything is working except recurring functionality. A lot of days has passed but it's not working properly. I've questioned this issue many times here but there is no proper time estimation about when it will be fixed. I've to implement it in my project and it's deadline is very close... Please suggest me anything else as an alternative if your API need more time to be fixed.

randstraw commented 8 years ago

@malik-umair please update https://github.com/paypal/PayPal-PHP-SDK/issues/572 with any questions regarding the delays in recurring payments in the sadnbox.