Advertisement
Guest User

CreatePaymentUsingSavedCard.php

a guest
Mar 21st, 2014
2,573
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.48 KB | None | 0 0
  1. <?php
  2.  
  3. // # Create payment using a saved credit card
  4. // This sample code demonstrates how you can process a
  5. // Payment using a previously saved credit card.
  6. // API used: /v1/payments/payment
  7.  
  8. require __DIR__ . '/../bootstrap.php';
  9. use PayPal\Api\Address;
  10. use PayPal\Api\Amount;
  11. use PayPal\Api\CreditCard;
  12. use PayPal\Api\CreditCardToken;
  13. use PayPal\Api\Payer;
  14. use PayPal\Api\Payment;
  15. use PayPal\Api\FundingInstrument;
  16. use PayPal\Api\RedirectUrls;
  17. use PayPal\Api\Transaction;
  18. use PayPal\Auth\OAuthTokenCredential;
  19. use PayPal\Rest\ApiContext;
  20.  
  21. // ### Credit card token
  22. // Saved credit card id from a previous call to
  23. // CreateCreditCard.php
  24. $creditCardId = 'CARD-27P64607T3535701WKMTOWIQ';
  25. $creditCardToken = new CreditCardToken();
  26. $creditCardToken->setCredit_card_id($creditCardId);
  27.  
  28. // ### FundingInstrument
  29. // A resource representing a Payer's funding instrument.
  30. // Use a Payer ID (A unique identifier of the payer generated
  31. // and provided by the facilitator. This is required when
  32. // creating or using a tokenized funding instrument)
  33. // and the `CreditCardDetails`
  34. $fi = new FundingInstrument();
  35. $fi->setCredit_card_token($creditCardToken);
  36.  
  37. // ### Payer
  38. // A resource representing a Payer that funds a payment
  39. // Use the List of `FundingInstrument` and the Payment Method
  40. // as 'credit_card'
  41. $payer = new Payer();
  42. $payer->setPayment_method("credit_card");
  43. $payer->setFunding_instruments(array($fi));
  44.  
  45.  
  46. $item1 = new \PayPal\Api\Item();
  47. $item1->setName('Ground Coffee 40 oz');
  48. $item1->setCurrency('USD');
  49. $item1->setQuantity(1);
  50. $item1->setPrice('7.50');
  51.  
  52. $item2 = new \PayPal\Api\Item();
  53. $item2->setName('Granola bars');
  54. $item2->setCurrency('USD');
  55. $item2->setQuantity(5);
  56. $item2->setPrice('2.00');
  57.  
  58. $itemList = new \PayPal\Api\ItemList();
  59. $itemList->setItems(array($item1, $item2));
  60.  
  61. $details = new \PayPal\Api\AmountDetails();
  62. $details->setShipping('1.10');
  63. $details->setTax('1.30');
  64. $details->setSubtotal('17.50');
  65.  
  66. // ### Amount
  67. // Let's you specify a payment amount.
  68. $amount = new Amount();
  69. $amount->setCurrency("USD");
  70. $amount->setTotal("19.90");
  71. $amount->setDetails($details);
  72.  
  73. // ### Transaction
  74. // A transaction defines the contract of a
  75. // payment - what is the payment for and who
  76. // is fulfilling it. Transaction is created with
  77. // a `Payee` and `Amount` types
  78. $transaction = new Transaction();
  79. $transaction->setAmount($amount);
  80. $transaction->setItem_list($itemList);
  81. $transaction->setDescription("This is the payment description.");
  82.  
  83. // ### Payment
  84. // A Payment Resource; create one using
  85. // the above types and intent as 'sale'
  86. $payment = new Payment();
  87. $payment->setIntent("sale");
  88. $payment->setPayer($payer);
  89. $payment->setTransactions(array($transaction));
  90.  
  91. // ### Api Context
  92. // Pass in a `ApiContext` object to authenticate
  93. // the call and to send a unique request id
  94. // (that ensures idempotency). The SDK generates
  95. // a request id if you do not pass one explicitly.
  96. $apiContext = new ApiContext($cred, 'Request' . time());
  97.  
  98. // ###Create Payment
  99. // Create a payment by posting to the APIService
  100. // using a valid apiContext
  101. // The return object contains the status;
  102. try {
  103.     $payment->create($apiContext);
  104. } catch (\PPConnectionException $ex) {
  105.     echo "Exception: " . $ex->getMessage() . PHP_EOL;
  106.     var_dump($ex->getData());  
  107.     exit(1);
  108. }
  109. ?>
  110. <html>
  111. <body>
  112.     <div>
  113.         Created payment:
  114.         <?php echo $payment->getId();?>
  115.     </div>
  116.     <pre><?php var_dump($payment->toArray());?></pre>
  117.     <a href='../index.html'>Back</a>
  118. </body>
  119. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement