|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Codeboxr\Upay\Managers; |
| 4 | + |
| 5 | +use Illuminate\Support\Facades\Http; |
| 6 | +use Codeboxr\Upay\Exception\UpayException; |
| 7 | + |
| 8 | +class Payment extends BaseApi |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Create payment |
| 12 | + * |
| 13 | + * @param string $amount |
| 14 | + * @param string $invoiceId |
| 15 | + * @param string $txnId |
| 16 | + * @param string $date |
| 17 | + * |
| 18 | + * @return \Illuminate\Http\JsonResponse |
| 19 | + * @throws UpayException |
| 20 | + */ |
| 21 | + public function createPayment($amount, $invoiceId, $txnId, $date) |
| 22 | + { |
| 23 | + $upayResponse = Http::withHeaders($this->headers()) |
| 24 | + ->post($this->baseUrl . "payment/merchant-payment-init/", [ |
| 25 | + "date" => $date, |
| 26 | + "txn_id" => $txnId, |
| 27 | + "invoice_id" => $invoiceId, |
| 28 | + "amount" => $amount, |
| 29 | + "merchant_id" => config('upay.merchant_id'), |
| 30 | + "merchant_name" => config('upay.merchant_name'), |
| 31 | + "merchant_code" => config('upay.merchant_code'), |
| 32 | + "merchant_country_code" => "BD", |
| 33 | + "merchant_city" => config("upay.merchant_city"), |
| 34 | + "merchant_category_code" => config("upay.merchant_category_code"), |
| 35 | + "merchant_mobile" => config("upay.merchant_mobile"), |
| 36 | + "transaction_currency_code" => "BD", |
| 37 | + "redirect_url" => config("upay.redirect_url"), |
| 38 | + ]); |
| 39 | + |
| 40 | + $result = json_decode($upayResponse->body()); |
| 41 | + if ($upayResponse->failed()) { |
| 42 | + throw new UpayException($result->message, $upayResponse->status()); |
| 43 | + } |
| 44 | + |
| 45 | + return response()->json([ |
| 46 | + "code" => $result->code, |
| 47 | + "session_id" => optional($result->data)->session_id, |
| 48 | + "txn_id" => optional($result->data)->txn_id, |
| 49 | + "trx_id" => optional($result->data)->trx_id, |
| 50 | + "invoice_id" => optional($result->data)->invoice_id, |
| 51 | + "gateway_url" => optional($result->data)->gateway_url, |
| 52 | + ]); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Redirect payment page |
| 58 | + * |
| 59 | + * @param string $amount |
| 60 | + * @param string $invoiceId |
| 61 | + * @param string $txnId |
| 62 | + * @param string $date |
| 63 | + * |
| 64 | + * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector|void |
| 65 | + * @throws UpayException |
| 66 | + */ |
| 67 | + public function executePayment($amount, $invoiceId, $txnId, $date) |
| 68 | + { |
| 69 | + $data = $this->createPayment($amount, $invoiceId, $txnId, $date)->getData(); |
| 70 | + return redirect($data->gateway_url); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Single Payment Details |
| 75 | + * |
| 76 | + * @param string $txnId |
| 77 | + * |
| 78 | + * @return \Illuminate\Http\JsonResponse |
| 79 | + * @throws UpayException |
| 80 | + */ |
| 81 | + public function queryPayment(string $txnId) |
| 82 | + { |
| 83 | + $upayResponse = Http::withHeaders($this->headers()) |
| 84 | + ->get($this->baseUrl . "payment/single-payment-status/{$txnId}/"); |
| 85 | + |
| 86 | + $result = json_decode($upayResponse->body()); |
| 87 | + if ($upayResponse->failed()) { |
| 88 | + throw new UpayException($result->message, $upayResponse->status()); |
| 89 | + } |
| 90 | + |
| 91 | + return response()->json([ |
| 92 | + "code" => $result->code, |
| 93 | + "session_id" => optional($result->data)->session_id, |
| 94 | + "txn_id" => optional($result->data)->txn_id, |
| 95 | + "trx_id" => optional($result->data)->trx_id, |
| 96 | + "invoice_id" => optional($result->data)->invoice_id, |
| 97 | + "status" => optional($result->data)->status, |
| 98 | + "amount" => optional($result->data)->amount, |
| 99 | + "merchant_name" => optional($result->data)->merchant_name, |
| 100 | + "date" => optional($result->data)->date, |
| 101 | + ]); |
| 102 | + } |
| 103 | + |
| 104 | + /** |
| 105 | + * Multiple Payment Status |
| 106 | + * |
| 107 | + * @param array $txnIds |
| 108 | + * |
| 109 | + * @return \Illuminate\Http\JsonResponse |
| 110 | + * @throws UpayException |
| 111 | + */ |
| 112 | + public function getMultiStatus(array $txnIds) |
| 113 | + { |
| 114 | + $upayResponse = Http::withHeaders($this->headers()) |
| 115 | + ->post($this->baseUrl . "payment/bulk-payment-status/", [ |
| 116 | + "txn_id_list" => $txnIds |
| 117 | + ]); |
| 118 | + |
| 119 | + $result = json_decode($upayResponse->body()); |
| 120 | + if ($upayResponse->failed()) { |
| 121 | + throw new UpayException($result->message, $upayResponse->status()); |
| 122 | + } |
| 123 | + |
| 124 | + return response()->json($result->data); |
| 125 | + } |
| 126 | +} |
0 commit comments