jeudi 23 février 2017

PayPal SDK refund part of the transaction to credit card automatically

Is there a way to refund part of a transaction made via PayPal SDK with credit_card as the payment method?

For example, if someone paid $50 with their credit card can I at end of that flow somehow refund $20 bucks to that credit_card via PHP SDK?

    $card = new PaymentCard();
    $card->setType("visa")
        ->setNumber("41111111111111")
        ->setExpireMonth("06")
        ->setExpireYear("2020")
        ->setCvv2("123")
        ->setFirstName("Jon")
        ->setBillingCountry("US")
        ->setLastName("Smith");

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

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

    $price = '50.00'; 

    if($price == 0) { 
        return Redirect::to('/');
    }

    $item_1 = new Item();
    $item_1->setName('My Item')
        ->setCurrency('USD')
        ->setQuantity(1)
        ->setPrice($price);

    $item_list = new ItemList();
    $item_list->setItems(array($item_1));

    $amount = new Amount();
    $amount->setCurrency('USD')
        ->setTotal($price); 

    $transaction = new Transaction();
    $transaction->setAmount($amount)
        ->setItemList($item_list)
        ->setDescription('Dummy Desc');

    $redirect_urls = new RedirectUrls();
    $redirect_urls->setReturnUrl(URL::to('/funds/status'))
        ->setCancelUrl(URL::to('/funds/cancel'));

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

    try {
        $payment->create($this->_api_context);
    } catch (PayPalConnectionException $ex) {
        echo "Exception: " . $ex->getMessage() . PHP_EOL;
        $err_data = json_decode($ex->getData(), true);
        exit;
    }

    foreach($payment->getLinks() as $link) {
        if($link->getRel() == 'approval_url') {
            $redirect_url = $link->getHref();
            break;
        }
    }

That's a test payment code I use basically.



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2l3RFKk
via IFTTT

Aucun commentaire:

Enregistrer un commentaire