使用Rest api,首付款金额较低,折扣优惠的Paypal订阅?

时间:2015-11-10 05:37:00

标签: paypal paypal-subscriptions paypal-rest-sdk discount

我们正在与rest api使用结算协议,我们可以在沙箱上成功完成。 现在我们有第一个月的折扣要求, 因此,如果每个月都有100美元的协议,并且当用户申请一些推荐优惠券时,他将在第一个月获得10美元的折扣。即他应该在第一个月收取90美元,在协议期间收取100美元。 除了设置费之外,我看不到任何其他选项,但这只能用于额外的费用,即增加计划金额但不会减少一些。

1 个答案:

答案 0 :(得分:2)

您必须使用paypal express checkout api才能执行此操作。

From paypal api documentation - https://developer.paypal.com/docs/classic/express-checkout/ht_ec-freeTrialRecurringPayments-curl-etc/

使用api方法CreateRecurringPaymentsProfile可以更改:

  1. RIALBILLINGPERIOD:一个试用期的一段时间。例如,一个月。
  2. TRIALBILLINGFREQUENCY:一段时间内的收费频率(如果有)。
  3. TRIALBILLINGFREQUENCY:一段时间内的收费频率(如果有)。
  4. TRIALTOTALBILLINGCYCLES:试用期的长度。也就是说,审判中的期数。
  5. TRIALAMT:试用期间的付款金额。例如,零。
  6. 你必须设置参数$ fields ['INITAMT'] = $ 10;和其他参数定期支付到每月100美元:

    $fields['INITAMT']  = 10;
    $fields['L_PAYMENTREQUEST_0_AMT0'] = 100;
    $fields['AMT'] = 100;

    在这种情况下,您将立即收取10美元,之后每个月将收取100美元的定期付款。

    这是一个简单的(PHP)示例,如何使用多个产品设置交易(也定期付款):

    // Parameters for SetExpressCheckout, which will be sent to PayPal
    $padata['L_BILLINGAGREEMENTDESCRIPTION0'] = 'Product description'; $padata['L_BILLINGAGREEMENTDESCRIPTION0'] = $padata['L_BILLINGAGREEMENTDESCRIPTION0'] . ' $'.$product->price.'/month'; $padata['L_PAYMENTREQUEST_0_DESC0'] = $padata['L_BILLINGAGREEMENTDESCRIPTION0'] . ' $'.$product->price.'/month';
    $padata['PAYMENTREQUEST_0_NOTIFYURL'] = 'http://site_url/paypal/ipn'; $padata['PAYMENTREQUEST_0_DESC'] = $product->name; $padata['RETURNURL'] = 'http://site_url/paypal/returnurl'; $padata['CANCELURL'] = 'http://site_url/paypal/cancelurl';
    $padata['PAYMENTREQUEST_0_CURRENCYCODE'] = 'USD'; $padata['PAYMENTREQUEST_0_PAYMENTACTION'] = 'SALE'; $padata['PAYMENTREQUEST_0_ITEMAMT'] = $product->price;
    $padata['PAYMENTREQUEST_0_AMT'] = $product->price;
    $padata['L_BILLINGTYPE0'] = 'RecurringPayments';
    $padata['L_PAYMENTREQUEST_0_NAME0'] = $product->name;
    $padata['L_PAYMENTREQUEST_0_NUMBER0'] = '322';
    $padata['L_PAYMENTREQUEST_0_QTY0'] = '1';
    $padata['L_PAYMENTREQUEST_0_AMT0'] = $product->price; $padata['L_PAYMENTREQUEST_0_NAME1'] = 'Second Product name';
    $hosteddata['L_PAYMENTREQUEST_0_DESC1'] = 'second product description';
    $hosteddata['L_PAYMENTREQUEST_0_NUMBER1'] = $secondproduct->id;
    $hosteddata['L_PAYMENTREQUEST_0_QTY1'] = '1';
    $hosteddata['L_PAYMENTREQUEST_0_AMT1'] = $secondproduct->price; $paypal_data = http_build_query($padata); $httpParsedResponseAr = $this->PPHttpPost('SetExpressCheckout', $paypal_data); //Respond according to message we receive from Paypal if("SUCCESS" == strtoupper($httpParsedResponseAr["ACK"]) || "SUCCESSWITHWARNING" == strtoupper($httpParsedResponseAr["ACK"])){ //Redirect user to PayPal store with Token received. $paypalurl ='https://www.paypal.com/cgi-bin/webscr?cmd=_express-checkout&token='.$httpParsedResponseAr["TOKEN"].''; header('Location: '.$paypalurl); }else{ echo 'Error : '.urldecode($httpParsedResponseAr["L_LONGMESSAGE0"]).'';
    }

    在returnurl上获取令牌后的脚本:

    $hosteddata['L_BILLINGAGREEMENTDESCRIPTION0']   = 'Recurring Description';
    $hosteddata['L_BILLINGAGREEMENTDESCRIPTION0'] = $hosteddata['L_BILLINGAGREEMENTDESCRIPTION0'] . ' $'.$pr->price.'/month';
    $hosteddata['L_PAYMENTREQUEST_0_NAME0'] = $pr->name;
    $hosteddata['PROFILEREFERENCE'] = $GetExpressCheckoutDetails['L_PAYMENTREQUEST_0_NUMBER0'];
    $hosteddata['PROFILESTARTDATE'] = date('Y-m-d') . 'T' . date('H:i:s').'Z';
    $hosteddata['SUBSCRIBERNAME'] = $GetExpressCheckoutDetails['FIRSTNAME'] . ' ' . $GetExpressCheckoutDetails['LASTNAME'];
    $hosteddata['TOKEN'] = urlencode($_POST['token']);
    $hosteddata['DESC'] = $hosteddata['L_BILLINGAGREEMENTDESCRIPTION0'];
    $hosteddata['AMT'] = $pr->price;
    $hosteddata['BILLINGPERIOD'] = 'Month';
    $hosteddata['BILLINGFREQUENCY'] = '1';
    $hosteddata['TOTALBILLINGCYCLES'] = '12';
    $hosteddata['REGULARTOTALBILLINGCYCLES'] = '1';
    $hosteddata['VERSION'] = '74.0';
    $hosteddata['MAXFAILEDPAYMENTS'] = '1';
    $hosteddata['L_PAYMENTREQUEST_0_QTY0'] = '1';
    $hosteddata['L_BILLINGTYPE0'] = 'RecurringPayments';
    $hosteddata['L_PAYMENTREQUEST_0_ITEMCATEGORY0'] = 'Digital';
    $hosteddata['L_PAYMENTREQUEST_0_AMT0'] = $pr->price;
    $hosteddata['INITAMT'] = $pr->price;
    $hosteddata['L_PAYMENTREQUEST_0_NUMBER0'] = $pr->id;
    $hosteddata['PAYMENTREQUEST_0_NOTIFYURL'] = 'http://site_url/paypal/ipn';
    $padata['L_PAYMENTREQUEST_0_NAME1'] = 'Second Product name'; $hosteddata['L_PAYMENTREQUEST_0_DESC1'] = 'second product description';
    $hosteddata['L_PAYMENTREQUEST_0_NUMBER1'] = $secondproduct->id;
    $hosteddata['L_PAYMENTREQUEST_0_QTY1'] = '1';
    $hosteddata['L_PAYMENTREQUEST_0_AMT1'] = $secondproduct->price; $paypal_data = http_build_query($hosteddata); $hosted_saas_response = $this->PPHttpPost('CreateRecurringPaymentsProfile', $paypal_data);

    我使用了一个单独的方法将参数发布到paypal

    private function PPHttpPost( $methodName_, $nvpStr_ ) {
    $api_username = 'yourpaypal@email.com'; $api_password = 'QWEQWEWQEQWEQEQWE';
    $api_signature = 'WQEQWEQWEQWEWQEQWEQWEQWEQWEQWE.cT';
    $api_endpoint = "https://api-3t.paypal.com/nvp";
    $version = '124.0'; $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $api_endpoint);
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_POST, 1);
    $nvpreq = "METHOD=$methodName_&VERSION=$version&PWD=$api_password&USER=$api_username&SIGNATURE=$api_signature&$nvpStr_";
    curl_setopt($ch, CURLOPT_POSTFIELDS, $nvpreq);
    $httpResponse = curl_exec($ch);

    if(!$httpResponse) { exit("$methodName_ failed: ".curl_error($ch).'('.curl_errno($ch).')');
    }

    // Extract the response details.
    $httpResponseAr = explode("&", $httpResponse);
    $httpParsedResponseAr = array();

    foreach ($httpResponseAr as $i => $value) {
    $tmpAr = explode("=", $value);
    if(sizeof($tmpAr) > 1) {
    $httpParsedResponseAr[$tmpAr[0]] = $tmpAr[1];
    }

    } if((0 == sizeof($httpParsedResponseAr)) || !array_key_exists('ACK', $httpParsedResponseAr)) {
    exit("Invalid HTTP Response for POST request($nvpreq) to $api_endpoint.");
    } return $httpParsedResponseAr;
    }