Skip to content

Commit

Permalink
Add sample code for paypal payment type
Browse files Browse the repository at this point in the history
  • Loading branch information
anouarabdsslm authored Nov 24, 2016
1 parent c833e35 commit 64b144f
Showing 1 changed file with 109 additions and 22 deletions.
131 changes: 109 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -125,17 +125,6 @@ If you do not want to use an ini file or want to pick your configuration dynamic

$this->_apiContext = Paypalpayment::apiContext($this->_ClientId, $this->_ClientSecret);

// Uncomment this step if you want to use per request
// dynamic configuration instead of using sdk_config.ini

$this->_apiContext->setConfig(array(
'mode' => 'sandbox',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => __DIR__.'/../PayPal.log',
'log.LogLevel' => 'FINE'
));

}

```
Expand Down Expand Up @@ -183,17 +172,6 @@ class PaypalPaymentController extends BaseController {

$this->_apiContext = Paypalpayment::ApiContext($this->_ClientId, $this->_ClientSecret);

// Uncomment this step if you want to use per request
// dynamic configuration instead of using sdk_config.ini

$this->_apiContext->setConfig(array(
'mode' => 'sandbox',
'service.EndPoint' => 'https://api.sandbox.paypal.com',
'http.ConnectionTimeOut' => 30,
'log.LogEnabled' => true,
'log.FileName' => __DIR__.'/../PayPal.log',
'log.LogLevel' => 'FINE'
));
}

}
Expand All @@ -209,6 +187,7 @@ $this->_apiContext->setConfig($flatConfig);
```

##2-Create Payment
#Credit card payment
Add the `create()` function to the `PaypalPaymentController` Controller

```php
Expand Down Expand Up @@ -335,6 +314,114 @@ Add the `create()` function to the `PaypalPaymentController` Controller
dd($payment);
}
```
#paypal payment
```
public function store()
{
// ### Payer
// A resource representing a Payer that funds a payment
// For paypal account payments, set payment method
// to 'paypal'.
$payer = Paypalpayment::payer();
$payer->setPaymentMethod("paypal");
// ### Itemized information
// (Optional) Lets you specify item wise
// information
$item1 = Paypalpayment::item();
$item1->setName('Ground Coffee 40 oz')
->setDescription('Ground Coffee 40 oz')
->setCurrency('USD')
->setQuantity(1)
->setTax(0.3)
->setPrice(7.50);
$item2 = Paypalpayment::item();
$item2->setName('Granola bars')
->setDescription('Granola Bars with Peanuts')
->setCurrency('USD')
->setQuantity(5)
->setTax(0.2)
->setPrice(2);
$itemList = Paypalpayment::itemList();
$itemList->setItems(array($item1, $item2));
$details = Paypalpayment::details();
$details->setShipping('1.2')
->setTax('1.3')
//total of items prices
->setSubtotal('17.5');
// ### Additional payment details
// Use this optional field to set additional
// payment information such as tax, shipping
// charges etc.
$details = Paypalpayment::details();
$details->setShipping(1.2)
->setTax(1.3)
->setSubtotal(17.50);
// ### Amount
// Lets you specify a payment amount.
// You can also specify additional details
// such as shipping, tax.
$amount = Paypalpayment::amount();
$amount->setCurrency("USD")
->setTotal(20)
->setDetails($details);
// ### Transaction
// A transaction defines the contract of a
// payment - what is the payment for and who
// is fulfilling it.
$transaction = Paypalpayment::transaction();
$transaction->setAmount($amount)
->setItemList($itemList)
->setDescription("Payment description")
->setInvoiceNumber(uniqid());
// ### Redirect urls
// Set the urls that the buyer must be redirected to after
// payment approval/ cancellation.
$baseUrl = "http://localhost:8000";
$redirectUrls = Paypalpayment::redirectUrls();
$redirectUrls->setReturnUrl("{$baseUrl}/callback?success=true")
->setCancelUrl("{$baseUrl}/callback?success=false");
// ### Payment
// A Payment Resource; create one using
// the above types and intent set to 'sale'
$payment = Paypalpayment::payment();
$payment->setIntent("sale")
->setPayer($payer)
->setRedirectUrls($redirectUrls)
->setTransactions(array($transaction));
// ### Create Payment
// Create a payment by calling the 'create' method
// passing it a valid apiContext.
// (See bootstrap.php for more on `ApiContext`)
// The return object contains the state and the
// url to which the buyer must be redirected to
// for payment approval
try {
$payment->create($this->apiContext);
} catch (\Exception $ex) {
\Log::error($ex);
}
// ### Get redirect url
// The API response provides the url that you must redirect
// the buyer to. Retrieve the url from the $payment->getApprovalLink()
// method
$approvalUrl = $payment->getApprovalLink();
echo "Created Payment Using PayPal. Please visit the URL to Approve.Payment <a href={$approvalUrl}>{$approvalUrl}</a>";
var_dump($payment);
}
```

##3-List Payment
Add the `index()` function to the `PaypalPaymentController` Controller
```php
Expand Down

0 comments on commit 64b144f

Please sign in to comment.