/**
  * Get the raw data array for this message. The format of this varies from gateway to
  * gateway, but will usually be either an associative array, or a SimpleXMLElement.
  *
  * @return mixed
  */
 public function getData()
 {
     $this->validateParams();
     $params = $this->getParamsToSign();
     $signer = new Signer($params);
     $sign = $signer->signWithRSA($this->privateKey);
     $resp['order_string'] = sprintf('%s&sign="%s"&sign_type="RSA"', $signer->getContentToSign(), urlencode($sign));
     return $resp;
 }
Example #2
0
    public function testConvert()
    {
        $key = 'MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRAFljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQEB/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5KsiNG9zpgmLCUYuLkxpLQIDAQAB';
        $signer = new Signer();
        $key = $signer->convertKey($key, Signer::KEY_TYPE_PUBLIC);
        $this->assertEquals('-----BEGIN PUBLIC KEY-----
MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCnxj/9qwVfgoUh/y2W89L6BkRA
FljhNhgPdyPuBV64bfQNN1PjbCzkIM6qRdKBoLPXmKKMiFYnkd6rAoprih3/PrQE
B/VsW8OoM8fxn67UDYuyBTqA23MML9q1+ilIZwBC2AQ2UBVOrFXfFl75p6/B5Ksi
NG9zpgmLCUYuLkxpLQIDAQAB
-----END PUBLIC KEY-----', $key);
    }
 protected function sign($params, $signType)
 {
     $signer = new Signer($params);
     $signer->setIgnores(['sign']);
     $signType = strtoupper($signType);
     if ($signType == 'RSA') {
         $sign = $signer->signWithRSA($this->getPrivateKey());
     } elseif ($signType == 'RSA2') {
         $sign = $signer->signWithRSA($this->getPrivateKey(), OPENSSL_ALGO_SHA256);
     } else {
         throw new InvalidRequestException('The signType is invalid');
     }
     return $sign;
 }
 protected function verifySignature()
 {
     $signer = new Signer($this->params->all());
     $signer->setSort($this->sort);
     $content = $signer->getContentToSign();
     $sign = $this->params->get('sign');
     $signType = strtoupper($this->params->get('sign_type'));
     if ($signType == 'MD5') {
         if (!$this->getKey()) {
             throw new InvalidRequestException('The `key` is required for `MD5` sign_type');
         }
         $match = (new Signer())->verifyWithMD5($content, $sign, $this->getKey());
     } elseif ($signType == 'RSA') {
         if (!$this->getAlipayPublicKey()) {
             throw new InvalidRequestException('The `alipay_public_key` is required for `RSA` sign_type');
         }
         $match = (new Signer())->verifyWithRSA($content, $sign, $this->getAlipayPublicKey());
     } else {
         throw new InvalidRequestException('The `sign_type` is invalid');
     }
     if (!$match) {
         throw new InvalidRequestException('The signature is not match');
     }
 }
 protected function sign($params, $signType)
 {
     $signer = new Signer($params);
     $signType = strtoupper($signType);
     if ($signType == 'MD5') {
         if (!$this->getKey()) {
             throw new InvalidRequestException('The `key` is required for `MD5` sign_type');
         }
         $sign = $signer->signWithMD5($this->getKey());
     } elseif ($signType == 'RSA') {
         if (!$this->getPrivateKey()) {
             throw new InvalidRequestException('The `private_key` is required for `RSA` sign_type');
         }
         $sign = $signer->signWithRSA($this->getPrivateKey());
     } else {
         throw new InvalidRequestException('The signType is not allowed');
     }
     return $sign;
 }
 protected function verifySignature()
 {
     $signer = new Signer($this->params->all());
     $signer->setSort($this->sort);
     $signer->setEncodePolicy($this->encodePolicy);
     $content = $signer->getContentToSign();
     $sign = $this->params->get('sign');
     $match = (new Signer())->verifyWithRSA($content, $sign, $this->getAlipayPublicKey());
     if (!$match) {
         throw new InvalidRequestException('The signature is not match');
     }
 }
 public function testCompletePurchaseNotifyWithInlineKey()
 {
     $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key.pem';
     $testPublicKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_public_key_inline.pem';
     $this->gateway = new AopAppGateway($this->getHttpClient(), $this->getHttpRequest());
     $this->gateway->setAppId($this->appId);
     $this->gateway->setPrivateKey($this->appPrivateKey);
     $this->gateway->setNotifyUrl('https://www.example.com/notify');
     $str = '{"total_amount":"0.01","buyer_id":"20882025611234567","trade_no":"201609232100100306021234567","refund_fee":"0.00","notify_time":"2016-09-23 19:12:33","subject":"test","sign_type":"RSA","notify_type":"trade_status_sync","out_trade_no":"2016092313071234567","gmt_close":"2016-09-23 19:08:10","trade_status":"TRADE_FINISHED","gmt_payment":"2016-09-23 19:08:10","sign":"vCAj0n6vUVggDzZUqV4P2IucMeguUMaLBl5Uld7PeLHCo74/d3AcWCNCsGDxtW9Jm7+suyo6Y0jRY7OUi0PKZJre84m2q9Oo30AdgbMFRT91uZFYp9miJGWlQWwHhJDo3cU5iAYf5bnPPYgH8073kTFtmDPmrP9pvEUm3lsroUw=","gmt_create":"2016-09-23 19:08:09","app_id":"20151128001234567","seller_id":"20880114661234567","notify_id":"da3e56af64bcb163f167240dc0f781agge"}';
     $str = stripslashes($str);
     $data = json_decode($str, true);
     $signer = new Signer($data);
     $signer->setSort(true);
     $signer->setEncodePolicy(Signer::ENCODE_POLICY_QUERY);
     $data['sign'] = $signer->signWithRSA($testPrivateKey);
     $data['sign_type'] = 'RSA';
     $this->gateway->setAlipayPublicKey($testPublicKey);
     /**
      * @var AopCompletePurchaseResponse $response
      */
     $response = $this->gateway->completePurchase(['params' => $data])->send();
     $this->assertEquals('{"total_amount":"0.01","buyer_id":"20882025611234567","trade_no":"201609232100100306021234567","refund_fee":"0.00","notify_time":"2016-09-23 19:12:33","subject":"test","sign_type":"RSA","notify_type":"trade_status_sync","out_trade_no":"2016092313071234567","gmt_close":"2016-09-23 19:08:10","trade_status":"TRADE_FINISHED","gmt_payment":"2016-09-23 19:08:10","sign":"Xa2NyOsxOBjW\\/q\\/RUFZhii2epa4B3ka+2aGsG8knqkiCD8llXrTDm11QtGkSRVw\\/hbfcgFPiTkuaKnaaDu\\/UfypsVSHToy28PiH5xkBSSd6zHNZCP\\/jvjzOa6GPf4tIpfYNVvjaRMRcbn+TRlOFtHOnMMubjsg7K52P+LCugZIA=","gmt_create":"2016-09-23 19:08:09","app_id":"20151128001234567","seller_id":"20880114661234567","notify_id":"da3e56af64bcb163f167240dc0f781agge"}', json_encode($response->data()));
     $this->assertEquals('2016092313071234567', $response->data('out_trade_no'));
     $this->assertTrue($response->isSuccessful());
     $this->assertTrue($response->isPaid());
     $this->assertEquals('201609232100100306021234567', $response->getData()['trade_no']);
 }
 public function testCompletePurchase()
 {
     $testPrivateKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_private_key.pem';
     $testPublicKey = ALIPAY_ASSET_DIR . '/dist/common/rsa_public_key.pem';
     $this->gateway = new AopF2FGateway($this->getHttpClient(), $this->getHttpRequest());
     $this->gateway->setAppId($this->appId);
     $this->gateway->setPrivateKey($this->appPrivateKey);
     $this->gateway->setNotifyUrl('https://www.example.com/notify');
     $str = 'gmt_payment=2015-06-11 22:33:59&notify_id=42af7baacd1d3746cf7b56752b91edcj34&seller_email=testyufabu07@alipay.com&notify_type=trade_status_sync&sign=kPbQIjX+xQc8F0/A6/AocELIjhhZnGbcBN6G4MM/HmfWL4ZiHM6fWl5NQhzXJusaklZ1LFuMo+lHQUELAYeugH8LYFvxnNajOvZhuxNFbN2LhF0l/KL8ANtj8oyPM4NN7Qft2kWJTDJUpQOzCzNnV9hDxh5AaT9FPqRS6ZKxnzM=&trade_no=2015061121001004400068549373&out_trade_no=21repl2ac2eOutTradeNo322&gmt_create=2015-06-11 22:33:46&seller_id=2088211521646673&notify_time=2015-06-11 22:34:03&subject=FACE_TO_FACE_PAYMENT_PRECREATE中文&trade_status=TRADE_SUCCESS&sign_type=RSA';
     parse_str($str, $data);
     $signer = new Signer($data);
     $signer->setSort(true);
     $signer->setEncodePolicy(Signer::ENCODE_POLICY_QUERY);
     $data['sign'] = $signer->signWithRSA($testPrivateKey);
     $data['sign_type'] = 'RSA';
     $this->gateway->setAlipayPublicKey($testPublicKey);
     /**
      * @var AopCompletePurchaseResponse $response
      */
     $response = $this->gateway->completePurchase(['params' => $data])->send();
     $this->assertEquals('{"gmt_payment":"2015-06-11 22:33:59","notify_id":"42af7baacd1d3746cf7b56752b91edcj34","seller_email":"*****@*****.**","notify_type":"trade_status_sync","sign":"T4JCUXoO5sK\\/7UjupKEfsSQnjDnw\\/1aSJnC6s53SYJyqdjFl+1Lt8dWdNuuXl5yX39leQsYzmk2CDwZx6F\\/YIQWCo1LHZME3DYMqH\\/F5wT5uiSUk2KYsYbLluW9pi7YHtBXRWKB6jtnn73DWWbC2sN3tDky9KySPizL5jQ1Cd0I=","trade_no":"2015061121001004400068549373","out_trade_no":"21repl2ac2eOutTradeNo322","gmt_create":"2015-06-11 22:33:46","seller_id":"2088211521646673","notify_time":"2015-06-11 22:34:03","subject":"FACE_TO_FACE_PAYMENT_PRECREATE\\u4e2d\\u6587","trade_status":"TRADE_SUCCESS","sign_type":"RSA"}', json_encode($response->data()));
     $this->assertEquals('21repl2ac2eOutTradeNo322', $response->data('out_trade_no'));
     $this->assertTrue($response->isSuccessful());
     $this->assertTrue($response->isPaid());
     $this->assertEquals('2015061121001004400068549373', $response->getData()['trade_no']);
 }