Conditions | 3 |
Paths | 3 |
Total Lines | 87 |
Code Lines | 56 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
48 | public function run(User $user, $amount, $currency = 'USD') |
||
49 | { |
||
50 | |||
51 | // TODO: |
||
52 | // this is code sample from the package readme. |
||
53 | // this does not seem to be compatible with paypal |
||
54 | // I need to remove this package (Paypalpayment) and replace with something else |
||
55 | // was checking the Payum laravel package seems compatible with Paypal Express Checkout |
||
56 | |||
57 | $card = $this->paypalPayment->creditCard(); |
||
58 | $card->setType("visa") |
||
59 | ->setNumber("4758411877817150") |
||
60 | ->setExpireMonth("05") |
||
61 | ->setExpireYear("2019") |
||
62 | ->setCvv2("456") |
||
63 | ->setFirstName("Joe") |
||
64 | ->setLastName("Shopper"); |
||
65 | |||
66 | $fi = $this->paypalPayment->fundingInstrument(); |
||
67 | $fi->setCreditCard($card); |
||
68 | |||
69 | $payer = $this->paypalPayment->payer(); |
||
70 | $payer->setPaymentMethod("credit_card") |
||
71 | ->setFundingInstruments(array($fi)); |
||
72 | |||
73 | $item1 = $this->paypalPayment->item(); |
||
74 | $item1->setName('Ground Coffee 40 oz') |
||
75 | ->setDescription('Ground Coffee 40 oz') |
||
76 | ->setCurrency('USD') |
||
77 | ->setQuantity(1) |
||
78 | ->setTax(0.3) |
||
79 | ->setPrice(7.50); |
||
80 | |||
81 | $item2 = $this->paypalPayment->item(); |
||
82 | $item2->setName('Granola bars') |
||
83 | ->setDescription('Granola Bars with Peanuts') |
||
84 | ->setCurrency('USD') |
||
85 | ->setQuantity(5) |
||
86 | ->setTax(0.2) |
||
87 | ->setPrice(2); |
||
88 | |||
89 | $itemList = $this->paypalPayment->itemList(); |
||
90 | $itemList->setItems(array($item1, $item2)); |
||
91 | |||
92 | $details = $this->paypalPayment->details(); |
||
93 | $details->setShipping("1.2") |
||
94 | ->setTax("1.3") |
||
95 | //total of items prices |
||
96 | ->setSubtotal("17.5"); |
||
97 | |||
98 | //Payment Amount |
||
99 | $amount = $this->paypalPayment->amount(); |
||
100 | $amount->setCurrency("USD") |
||
101 | // the total is $17.8 = (16 + 0.6) * 1 ( of quantity) + 1.2 ( of Shipping). |
||
102 | ->setTotal("20") |
||
103 | ->setDetails($details); |
||
104 | |||
105 | $transaction = $this->paypalPayment->transaction(); |
||
106 | $transaction->setAmount($amount) |
||
107 | ->setItemList($itemList) |
||
108 | ->setDescription("Payment description") |
||
109 | ->setInvoiceNumber(uniqid()); |
||
110 | |||
111 | $payment = $this->paypalPayment->payment(); |
||
112 | |||
113 | $payment->setIntent("sale") |
||
114 | ->setPayer($payer) |
||
115 | ->setTransactions(array($transaction)); |
||
116 | |||
117 | try { |
||
118 | |||
119 | $response = $payment->create($this->paypalApi); |
||
120 | |||
121 | } catch (Exception $e) { |
||
122 | throw (new PaypalApiErrorException('Paypal API error (payment)'))->debug($e->getMessage(), true); |
||
123 | } |
||
124 | |||
125 | if ($response['state'] != 'approved') { |
||
126 | throw new PaypalApiErrorException('Paypal response status not succeeded (payment)'); |
||
127 | } |
||
128 | |||
129 | // this data will be stored on the pivot table (user credits) |
||
130 | return [ |
||
131 | 'payment_method' => 'paypal', |
||
132 | 'description' => $response['id'], // Pay ID |
||
133 | ]; |
||
134 | } |
||
135 | |||
149 |