Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[paypal][rest] example #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/config/parameters.yml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ parameters:
paypal.pro_checkout.partner: EDIT ME
paypal.pro_checkout.vendor: EDIT ME

paypal.rest.client_id: EDIT ME
paypal.rest.client_secret: EDIT ME

authorize_net.login_id: EDIT ME
authorize_net.transaction_key: EDIT ME

Expand Down
27 changes: 27 additions & 0 deletions app/config/paypal_rest_sdk_config.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@

;Connection Information
[Http]
http.ConnectionTimeOut = 30
http.Retry = 1
;http.Proxy=http://[username:password]@hostname[:port][/path]


;Service Configuration
[Service]
mode=sandbox

;Logging Information
[Log]

log.LogEnabled=false

# When using a relative path, the log file is created
# relative to the .php file that is the entry point
# for this request. You can also provide an absolute
# path here
log.FileName=../logs/PayPal.log

# Logging level can be one of FINE, INFO, WARN or ERROR
# Logging is most verbose in the 'FINE' level and
# decreases as you proceed towards ERROR
log.LogLevel=FINE
6 changes: 6 additions & 0 deletions app/config/payum.yml
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ payum:
tender: "foo"
sandbox: true

paypal_rest:
factory: paypal_rest
client_id: %paypal.rest.client_id%
client_secret: %paypal.rest.client_secret%
config_path: '%kernel.root_dir%/config/paypal_rest_sdk_config.ini'

authorize_net:
factory: authorize_net_aim
login_id: %authorize_net.login_id%
Expand Down
96 changes: 96 additions & 0 deletions src/Acme/PaymentBundle/Controller/PaypalRestController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php
namespace Acme\PaymentBundle\Controller;

use Acme\PaymentBundle\Entity\Payout;
use Payum\Core\Payum;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\Form\Form;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Validator\Constraints\Email;
use Symfony\Component\Validator\Constraints\Range;
use Sensio\Bundle\FrameworkExtraBundle\Configuration as Extra;

class PaypalRestController extends Controller
{
/**
* @Extra\Route("/paypal/rest/prepare", name="acme_paypal_rest_prepare")
*/
public function prepareAction(Request $request)
{
$gatewayName = 'paypal_rest';

$form = $this->createPurchasePlusCreditCardForm();
$form->handleRequest($request);
if ($form->isValid()) {
$data = $form->getData();

$storage = $this->get('payum')->getStorage('MyApp\PaymentBundle\Entity\PaymentDetails');

$payment = $storage->create();
$storage->update($payment);

$payer = new Payer();
$payer->payment_method = "paypal";

$amount = new Amount();
$amount->currency = "USD";
$amount->total = "1.00";

$transaction = new Transaction();
$transaction->amount = $amount;
$transaction->description = "This is the payment description.";

$captureToken = $this->get('payum')->getTokenFactory()->createCaptureToken(
$gatewayName,
$payment,
'payment_done' // the route to redirect after capture
);

$redirectUrls = new RedirectUrls();
$redirectUrls->return_url = $captureToken->getTargetUrl();
$redirectUrls->cancel_url = $captureToken->getTargetUrl();

$payment->intent = "sale";
$payment->payer = $payer;
$payment->redirect_urls = $redirectUrls;
$payment->transactions = array($transaction);
$storage->update($payment);

return $this->redirect($captureToken->getTargetUrl());

return $this->redirect($captureToken->getTargetUrl());
}

return $this->render('AcmePaymentBundle::prepare.html.twig', [
'form' => $form->createView()
]);
}

/**
* @return Form
*/
protected function createPurchasePlusCreditCardForm()
{
return $this->createFormBuilder()
->add('amount', null, [
'data' => 150,
'constraints' => [new Range(['max' => 200])]
])
->add('currency', null, ['data' => 'USD'])
->add('recipient_email', null, [
'data' => $this->container->getParameter('paypal.express_checkout.usd_testuser_login'),
'constraints' => [new Email]
])

->getForm()
;
}

/**
* @return Payum
*/
protected function getPayum()
{
return $this->get('payum');
}
}