├── .gitignore ├── README.md ├── composer.json ├── examples ├── Payment │ ├── callback.php │ └── pay.php ├── Register │ ├── check_user.php │ └── register_user.php └── oAuth2 │ ├── callback.php │ └── redirect.php └── src ├── Authorization.php ├── Laravel ├── Facade │ ├── Authorization.php │ └── Payment.php └── PayPingServiceProvider.php ├── PayPingException.php ├── Payment.php ├── Register.php ├── Scopes.php └── helper.php /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | .idea/ 3 | composer.lock 4 | vendor/ 5 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # payping-client 2 | 3 | payment and oauth2 request library for payping 4 | 5 | ## نحوه استفاده 6 | ### نصب 7 | ``composer require flotfeali/payping-client`` 8 | 9 | ### درخواست پرداخت 10 | ```php 11 | $token = "توکن اختصاصی "; 12 | $args = [ 13 | "amount" => میلغ تراکنش, 14 | "payerIdentity" => "شناسه کاربر در صورت وجود", 15 | "payerName" => "نام کاربر پرداخت کننده", 16 | "description" => "توضیحات", 17 | "returnUrl" => "آدرس برگشتی از سمت درگاه", 18 | "clientRefId" => "شماره فاکتور" 19 | ]; 20 | 21 | $payment = new \PayPing\Payment($token); 22 | 23 | try { 24 | $payment->pay($args); 25 | } catch (Exception $e) { 26 | var_dump($e->getMessage()); 27 | } 28 | //echo $payment->getPayUrl(); 29 | 30 | header('Location: ' . $payment->getPayUrl()); 31 | ``` 32 | 33 | ### تایید پرداخت 34 | ```php 35 | $token = "توکن اختصاصی"; 36 | 37 | $payment = new \PayPing\Payment($token); 38 | 39 | try { 40 | if($payment->verify($_GET['refid'], 100)){ 41 | echo "success"; 42 | }else{ 43 | echo "fail"; 44 | } 45 | } 46 | catch (PayPingException $e) { 47 | foreach (json_decode($e->getMessage(), true) as $msg) { 48 | echo $msg; 49 | } 50 | } 51 | ``` 52 | ### بررسی ثبت نام کاربر بر اساس ایمیل 53 | ```php 54 | 55 | $token = "توکن اختصاصی"; 56 | 57 | $register = new \PayPing\Register($token); 58 | 59 | var_dump($register->checkEmail("ایمیل مورد نظر")); 60 | 61 | ``` 62 | ### ثبت نام کاربر در پی پینگ 63 | ```php 64 | $token = "توکن اختصاصی"; 65 | 66 | $register = new \PayPing\Register($token); 67 | 68 | $params = [ 69 | "UserName" => "نام کاربری", 70 | "Email" => "ایمیل - اجباری", 71 | "FirstName" => "نام کوچک", 72 | "LastName" => "نام خانوادگی", 73 | "PhoneNumber" => "شماره تلفن", 74 | "NationalCode" => "کد ملی", 75 | "BirthDay" => "تاریخ تولد", 76 | "Sheba" => "شناسه شبا" 77 | ]; 78 | try { 79 | $register_id = $register->registerUser("آدرس برگشتی هنگام تکمیل ثبت نام در پی پینگ", $params); 80 | header("Location: ".$register->getRegisterUrl()); 81 | }catch (Exception $e){ 82 | echo $e->getMessage(); 83 | } 84 | 85 | ``` 86 | 87 | ### ورود کاربران توسط oauth2 88 | ```php 89 | $client_id = "client-id"; 90 | $client_secret = "client-secret"; 91 | $codeVerify = 'یک شناسه یکتا به ازای هر کاربر ';//generateVerified(); 92 | $callback = "آدرس برگشتی هنگام ورود در پی پینگ"; 93 | $scope = [ //دسترسی های مورد نیاز از پنل پی پینگ 94 | PayPing\Scopes::OPENID 95 | //, ... 96 | ]; 97 | 98 | $state = ["مقادیری که بعد از ثبت نام و برگشت نیاز دارید"]; 99 | 100 | 101 | try { 102 | $auth = new PayPing\Authorization($client_id, $client_secret, $callback, $codeVerify); 103 | echo 'login with payping' . "
"; 104 | } catch (Exception $exception) { 105 | echo $exception->getMessage(); 106 | } 107 | ``` 108 | 109 | ### تایید ورود در پنل پی پینگ 110 | ```php 111 | 112 | $client_id = "client-id"; 113 | $client_secret = "client-secret"; 114 | $codeVerify = 'یک شناسه یکتا به ازای هر کاربر که در مرحله قبل ساخته شده است '; 115 | $callback = "آدرس برگشتی هنگام ورود در پی پینگ"; 116 | $auth = new PayPing\Authorization($client_id,$client_secret,$callback,$codeVerify); 117 | try { 118 | echo '
';
119 |     print_r($auth->getAccessToken($callback, $codeVerify));
120 | } catch (Exception $exception) {
121 |     echo $exception->getMessage();
122 | }
123 | ```
124 | 


--------------------------------------------------------------------------------
/composer.json:
--------------------------------------------------------------------------------
 1 | {
 2 |   "name": "flotfeali/payping-client",
 3 |   "type": "library",
 4 |   "license": "MIT",
 5 |   "authors": [
 6 |     {
 7 |       "name": "farhad lotfeali",
 8 |       "email": "flotfeali@gmail.com"
 9 |     }
10 |   ],
11 |   "autoload": {
12 |     "psr-4": {
13 |       "PayPing\\": "src/"
14 |     },
15 |     "files": [
16 |       "src/helper.php"
17 |     ]
18 |   },
19 |   "require": {
20 |     "guzzlehttp/guzzle": "^6.2|^7.0",
21 |     "ext-openssl": "*",
22 |     "ext-json": "*"
23 |   },
24 |   "extra": {
25 |     "laravel": {
26 |       "providers": [
27 |         "PayPing\\Laravel\\PayPingServiceProvider"
28 |       ],
29 |       "aliases": {
30 |         "Authorization": "PayPing\\Laravel\\Facade\\Authorization",
31 |         "Payment": "PayPing\\Laravel\\Facade\\Payment"
32 |       }
33 |     }
34 |   },
35 |   "minimum-stability": "dev",
36 |   "version": "0.0.1"
37 | }
38 | 


--------------------------------------------------------------------------------
/examples/Payment/callback.php:
--------------------------------------------------------------------------------
 1 | verify($_GET['refid'], 100)){
10 |         echo "success";
11 |     }else{
12 |         echo "fail";
13 |     }
14 | } catch (Exception $e) {
15 |     echo $e->getMessage();
16 | }


--------------------------------------------------------------------------------
/examples/Payment/pay.php:
--------------------------------------------------------------------------------
 1 |  میلغ تراکنش,
 7 |     "payerIdentity" => "شناسه کاربر در صورت وجود",
 8 |     "payerName" => "نام کاربر پرداخت کننده",
 9 |     "description" => "توضیحات",
10 |     "returnUrl" => "آدرس برگشتی از سمت درگاه",
11 |     "clientRefId" => "شماره فاکتور"
12 | ];
13 | 
14 | $payment = new \PayPing\Payment($token);
15 | 
16 | try {
17 |     $payment->pay($args);
18 | } catch (Exception $e) {
19 |     var_dump($e->getMessage());
20 | }
21 | //echo $payment->getPayUrl();
22 | 
23 | header('Location: ' . $payment->getPayUrl());
24 | 


--------------------------------------------------------------------------------
/examples/Register/check_user.php:
--------------------------------------------------------------------------------
1 | checkEmail("ایمیل مورد نظر"));
9 | 


--------------------------------------------------------------------------------
/examples/Register/register_user.php:
--------------------------------------------------------------------------------
 1 |  "نام کاربری",
10 |     "Email" => "ایمیل - اجباری",   
11 |     "FirstName" => "نام کوچک",
12 |     "LastName" => "نام خانوادگی",
13 |     "PhoneNumber" => "شماره تلفن",
14 |     "NationalCode" => "کد ملی",
15 |     "BirthDay" => "تاریخ تولد",
16 |     "Sheba" => "شناسه شبا"
17 | ];
18 | try {
19 |     $register_id = $register->registerUser("آدرس برگشتی هنگام تکمیل ثبت نام در پی پینگ", $params);
20 |   header("Location: ".$register->getRegisterUrl());
21 | }catch (Exception $e){
22 |     echo $e->getMessage();
23 | }
24 | 


--------------------------------------------------------------------------------
/examples/oAuth2/callback.php:
--------------------------------------------------------------------------------
 1 | ';
16 |     print_r($auth->getAccessToken($callback, $codeVerify));
17 | } catch (Exception $exception) {
18 |     echo $exception->getMessage();
19 | }
20 | 
21 | 


--------------------------------------------------------------------------------
/examples/oAuth2/redirect.php:
--------------------------------------------------------------------------------
 1 | getAuthLink() . '" >login with payping' . "
"; 15 | } catch (Exception $exception) { 16 | echo $exception->getMessage(); 17 | } -------------------------------------------------------------------------------- /src/Authorization.php: -------------------------------------------------------------------------------- 1 | client_id = $client_id; 47 | $this->client_secret = $client_secret; 48 | $this->restCall = new Client([ 49 | 'base_uri' => self::BASE_URL 50 | ]); 51 | 52 | if ($codeVerified) 53 | $this->codeVerified = $codeVerified; 54 | 55 | if ($callback) 56 | $this->callback = $callback; 57 | } 58 | 59 | /** 60 | * @param string $codeVerified 61 | */ 62 | public function setCodeVerified($codeVerified) 63 | { 64 | $this->codeVerified = $codeVerified; 65 | } 66 | 67 | /** 68 | * @return string 69 | */ 70 | public function getCodeVerified() 71 | { 72 | return $this->codeVerified; 73 | } 74 | 75 | /** 76 | * @param string $callback 77 | */ 78 | public function setCallback($callback) 79 | { 80 | $this->callback = $callback; 81 | } 82 | 83 | /** 84 | * @return string 85 | */ 86 | public function getCallback() 87 | { 88 | return $this->callback; 89 | } 90 | 91 | /** 92 | * @param Client $restCall 93 | */ 94 | public function setRestCall($restCall) 95 | { 96 | $this->restCall = $restCall; 97 | } 98 | 99 | /** 100 | * دریافت لینک برای احراز هویت توسط payping 101 | * @param array $scopes 102 | * @param null $state 103 | * @return string 104 | * @throws PayPingException 105 | */ 106 | public function getAuthLink(array $scopes = [Scopes::OPENID], $state = NULL) 107 | { 108 | 109 | if (empty($this->codeVerified)) 110 | throw new PayPingException("empty codeVerified please call setCodeVerified first"); 111 | if (empty($this->callback)) 112 | throw new PayPingException("empty callback please call setCallback first"); 113 | 114 | if (!in_array(Scopes::OPENID, $scopes)) 115 | $scopes[] = Scopes::OPENID; 116 | 117 | $params = [ 118 | 'scope' => implode(' ', $scopes), 119 | 'response_type' => 'code', 120 | 'client_id' => $this->client_id, 121 | 'code_challenge' => generateCodeChallenge($this->getCodeVerified()), 122 | 'code_challenge_method' => 'S256', 123 | 'redirect_uri' => $this->getCallback(), 124 | ]; 125 | 126 | if (!empty($state)) $params['state'] = $state; 127 | 128 | return urldecode(self::BASE_URL . "connect/authorize?" . http_build_query($params, null, '&')); 129 | 130 | } 131 | 132 | /** 133 | * دریافت access_token از payping بوسیله کد احراز شده 134 | * @return mixed|\Psr\Http\Message\ResponseInterface 135 | * @throws PayPingException 136 | */ 137 | public function getAccessToken() 138 | { 139 | if (empty($this->codeVerified)) 140 | throw new PayPingException("empty codeVerified please call setCodeVerified first"); 141 | if (empty($this->callback)) 142 | throw new PayPingException("empty callback please call setCallback first"); 143 | 144 | if (!isset($_GET['code']) || empty($_GET['code'])) 145 | throw new PayPingException("invalid request", '401'); 146 | $token = $_GET['code']; 147 | 148 | $params = [ 149 | 'grant_type' => 'authorization_code', 150 | 'client_id' => $this->client_id, 151 | 'client_secret' => $this->client_secret, 152 | 'code_verifier' => $this->getCodeVerified(), 153 | 'code' => $token, 154 | 'redirect_uri' => $this->getCallback() 155 | ]; 156 | try { 157 | $result = $this->restCall->post('connect/token', ['form_params' => $params]); 158 | $result = json_decode($result->getBody()->getContents(), false); 159 | } catch (\Exception $exception) { 160 | throw new PayPingException($exception->getMessage(), $exception->getCode(), $exception->getPrevious()); 161 | } 162 | 163 | return $result; 164 | 165 | } 166 | } 167 | -------------------------------------------------------------------------------- /src/Laravel/Facade/Authorization.php: -------------------------------------------------------------------------------- 1 | app->singleton('Authorization', function () { 18 | $client_id = config('services.payping.client_id'); 19 | $client_secret = config('services.payping.client_secret'); 20 | $redirect = config('services.payping.redirect'); 21 | return new \PayPing\Authorization($client_id,$client_secret,$redirect); 22 | }); 23 | 24 | $this->app->singleton('Payment', function () { 25 | $token = config('services.payping.token'); 26 | return new \PayPing\Payment($token); 27 | }); 28 | } 29 | 30 | /** 31 | * Publish the plugin configuration. 32 | */ 33 | public function boot() 34 | { 35 | // 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /src/PayPingException.php: -------------------------------------------------------------------------------- 1 | token = $token; 35 | 36 | $headers = [ 37 | 'Content-type' => 'application/json; charset=utf-8', 38 | 'Accept' => 'application/json', 39 | 'Authorization' => 'Bearer ' . $this->token, 40 | ]; 41 | 42 | 43 | $this->restCall = new Client([ 44 | 'base_uri' => self::BASE_URL, 45 | 'headers' => $headers 46 | ]); 47 | } 48 | 49 | /** 50 | * ایجاد یک پرداخت 51 | * @param array $params 52 | * @throws PayPingException 53 | */ 54 | public function pay(array $params) 55 | { 56 | try { 57 | $result = $this->restCall->post(self::BASE_URL . 'pay', ['body' => json_encode($params)]); 58 | $result = json_decode($result->getBody()->getContents(), false); 59 | if (!isset($result->code)) 60 | throw new PayPingException('error in get code from payping', 0); 61 | 62 | $this->payUrl = self::BASE_URL . 'pay/gotoipg/' . $result->code; 63 | } catch (\Exception $exception) { 64 | throw new PayPingException($exception->getMessage(), $exception->getCode(), $exception->getPrevious()); 65 | } 66 | } 67 | 68 | /** 69 | * دریافت ادرس درگاه پرداخت 70 | * @return string 71 | */ 72 | public function getPayUrl() 73 | { 74 | return $this->payUrl; 75 | } 76 | 77 | /** 78 | * تایید یک پرداخت 79 | * اگر exception پرتاب نشود پرداخت درست بوده است 80 | * @param $refId 81 | * @param $amount 82 | * @return bool 83 | * @throws PayPingException 84 | */ 85 | public function verify($refId, $amount) 86 | { 87 | $params = [ 88 | 'refId' => $refId, 89 | 'amount' => $amount 90 | ]; 91 | try { 92 | $result = $this->restCall->post(self::BASE_URL . 'pay/verify', ['body' => json_encode($params)]); 93 | if ($result->getStatusCode() >= 200 && $result->getStatusCode() < 300) { 94 | return true; 95 | } 96 | } catch (RequestException $re) { 97 | throw new PayPingException($re->getResponse()->getBody()->getContents(), $re->getResponse()->getStatusCode(), $re->getPrevious()); 98 | } catch (\Exception $e) { 99 | throw new PayPingException($e->getMessage(), $e->getCode(), $e->getPrevious()); 100 | } 101 | } 102 | 103 | } -------------------------------------------------------------------------------- /src/Register.php: -------------------------------------------------------------------------------- 1 | token = $token; 35 | 36 | $headers = [ 37 | 'Content-type' => 'application/json; charset=utf-8', 38 | 'Accept' => 'application/json', 39 | 'Authorization' => 'Bearer ' . $this->token, 40 | ]; 41 | 42 | 43 | $this->restCall = new Client([ 44 | 'base_uri' => self::BASE_URL, 45 | 'headers' => $headers 46 | ]); 47 | } 48 | 49 | /** 50 | * بررسی وجود یک کاربر در payping بر اساس ایمیل کاربر 51 | * @param $email 52 | * @return bool 53 | * @throws PayPingException 54 | */ 55 | public function checkEmail($email) 56 | { 57 | try { 58 | $result = $this->restCall->get(self::BASE_URL . 'v1/client/EmailExist', ['query' => ['Email' => $email]]); 59 | $result = json_decode($result->getBody()->getContents(), false); 60 | return boolval($result->exist); 61 | } catch (RequestException $re) { 62 | throw new PayPingException($re->getResponse()->getBody()->getContents(), $re->getResponse()->getStatusCode(), $re->getPrevious()); 63 | } catch (\Exception $exception) { 64 | throw new PayPingException($exception->getMessage(), $exception->getCode(), $exception->getPrevious()); 65 | } 66 | } 67 | 68 | /** 69 | * ثبت نام کاربر در payping 70 | * @param $callback 71 | * @param array $params 72 | * @return string 73 | * @throws PayPingException 74 | */ 75 | public function registerUser($callback, array $params) 76 | { 77 | if (!isset($params['Email'])) { 78 | throw new PayPingException("please set email in params this required", 0); 79 | } 80 | 81 | $params['ReturnUrl'] = $callback; 82 | 83 | try { 84 | $result = $this->restCall->post(self::BASE_URL . 'v1/client/ClientRegisterInit', ['body' => json_encode($params)]); 85 | $result = json_decode($result->getBody()->getContents(), false); 86 | if (!isset($result->id) || empty($result->id)) 87 | throw new PayPingException("server error ", 500); 88 | $this->registerUrl = self::BASE_URL . 'Client/ClientRegister?registerId=' . $result->id; 89 | return $result->id; 90 | } catch (RequestException $re) { 91 | throw new PayPingException($re->getResponse()->getBody()->getContents(), $re->getResponse()->getStatusCode(), $re->getPrevious()); 92 | } catch (\Exception $exception) { 93 | throw new PayPingException($exception->getMessage(), $exception->getCode(), $exception->getPrevious()); 94 | } 95 | } 96 | 97 | 98 | /** 99 | * @return string 100 | */ 101 | public function getRegisterUrl() 102 | { 103 | return $this->registerUrl; 104 | } 105 | 106 | } -------------------------------------------------------------------------------- /src/Scopes.php: -------------------------------------------------------------------------------- 1 |