SandPayFast.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace common\components\payment;
  3. use addons\SandPay\common\expand\sdk\PCCashier;
  4. use common\helpers\FormatHelper;
  5. use common\traits\ErrorTrait;
  6. use yii\helpers\Json;
  7. class SandPayFast
  8. {
  9. use ErrorTrait;
  10. public $config = [];
  11. public $url = 'https://api.joinpay.com/fastpay';
  12. public $refund_url = 'https://api.joinpay.com/refund';
  13. /**
  14. * @var $client PCCashier
  15. */
  16. public $client;
  17. public function __construct(array $config)
  18. {
  19. $this->config = $config;
  20. $this->client = new PCCashier();
  21. }
  22. public function pay($order)
  23. {
  24. try {
  25. // 参数
  26. $this->client->body = $order ;
  27. $apiMap=$this->client->apiMap();
  28. $form = '';
  29. $postData = $this->client->postData($this->config, $apiMap['index']['method']);
  30. $url = $this->config['apiUrl'] . $apiMap['index']['url'];
  31. $form = '<form action="' . $url . '" method="post">';
  32. foreach ($postData as $k => $v) {
  33. $form .= "{$k} <p><input type='text' name='{$k}' value='{$v}'></p>";
  34. }
  35. $form .= '<input type="submit" value="提交"></form>';
  36. return $form;
  37. } catch (\Exception $e) {
  38. \Yii::error(FormatHelper::exception($e, __METHOD__));
  39. $this->error = $e->getMessage();
  40. return false;
  41. }
  42. }
  43. public function notify($post)
  44. {
  45. try{
  46. $this->client->verify($this->config, $post['data'], $post['sign']);
  47. return [
  48. 'out_trade_no' => $post['data']['orderCode'], //商户订单号
  49. 'trade_no' => $post['data']['tradeNo'], //汇聚平台流水号
  50. 'pay_fee' => bcmul(intval($post['data']['totalAmount']), 0.01, 2), // 000000000001
  51. ];
  52. }catch(\Exception $e){
  53. \Yii::error(FormatHelper::exception($e, __METHOD__));
  54. $this->error = $e->getMessage();
  55. return false;
  56. }
  57. }
  58. public function refund($info)
  59. {
  60. $amount = str_pad((string) intval(bcmul($info['refund_amount'], 100)), 12, '0', STR_PAD_LEFT);
  61. $this->client->body = [
  62. "orderCode" => $info['out_trade_no'],
  63. "oriOrderCode" => $info['refund_order_no'],
  64. "refundAmount" => $amount,
  65. "notifyUrl" => \Yii::getAlias('@apiUrl').'/notify/sandpay-fast-refund',
  66. "refundReason" => mb_substr($info['refund_reason'],0,60),
  67. "refundMarketAmount" => "",
  68. "extend" => ""
  69. ];
  70. try {
  71. $method = 'orderRefund';
  72. $ret = $this->client->request($this->config, $method);
  73. // $apiMap =$this->client->apiMap();
  74. $this->client->verify($this->config, $ret['data'], $ret['sign']);
  75. // $postData = $this->client->postData($this->config, $apiMap[$method]['method']);
  76. // $postData['data']= Json::decode($postData['data']);
  77. $data = Json::decode($ret['data']);
  78. if($data["respCode"] == '000000'){
  79. //退款失败
  80. throw new \Exception($data['respMsg'] ?? $ret);
  81. }
  82. return true;
  83. } catch (\Exception $e) {
  84. throw new \Exception($e->getMessage().'请稍后重新发起交易,或联系汇聚客服');
  85. }
  86. }
  87. }